From e56953c6b610456313751f42910e16f027cac94e Mon Sep 17 00:00:00 2001 From: mrhut10 Date: Thu, 9 Apr 2020 02:04:49 +1000 Subject: [PATCH 1/6] add isBroswer varable --- idb.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/idb.js b/idb.js index 7e34c22..fcf86a4 100644 --- a/idb.js +++ b/idb.js @@ -1,3 +1,5 @@ +const isBrowser = typeof window !== 'undefined'; + const dbp = new Promise((resolve, reject) => { const openreq = window.indexedDB.open('use-idb', 1) openreq.onerror = () => reject(openreq.error) From fbee62c507721a1e50fd49c0764d70de4529209b Mon Sep 17 00:00:00 2001 From: mrhut10 Date: Thu, 9 Apr 2020 02:20:16 +1000 Subject: [PATCH 2/6] make browser safe --- idb.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/idb.js b/idb.js index fcf86a4..6041840 100644 --- a/idb.js +++ b/idb.js @@ -1,26 +1,33 @@ const isBrowser = typeof window !== 'undefined'; const dbp = new Promise((resolve, reject) => { - const openreq = window.indexedDB.open('use-idb', 1) - openreq.onerror = () => reject(openreq.error) - openreq.onsuccess = () => resolve(openreq.result) - openreq.onupgradeneeded = () => openreq.result.createObjectStore('idb') + if (isBrowser){ + const openreq = window.indexedDB.open('use-idb', 1) + openreq.onerror = () => reject(openreq.error) + openreq.onsuccess = () => resolve(openreq.result) + openreq.onupgradeneeded = () => openreq.result.createObjectStore('idb') + } else { + resolve(undefined) + } }) export const call = async (type, method, ...args) => { - const db = await dbp - const transaction = db.transaction('idb', type) - const store = transaction.objectStore('idb') - + const db = await dbp; return new Promise((resolve, reject) => { - const req = store[method](...args) - transaction.oncomplete = () => resolve(req) - transaction.onabort = transaction.onerror = () => reject(transaction.error) - }) -} + if (isBrowser){ + const transaction = db.transaction('idb', type); + const store = transaction.objectStore('idb'); + const req = store[method](...args); + transaction.oncomplete = () => resolve(req); + transaction.onabort = transaction.onerror = () => reject(transaction.error); + } else { + resolve(undefined); + } + }); +}; -export const get = async key => (await call('readonly', 'get', key)).result +export const get = async key => (await call('readonly', 'get', key)).result; export const set = (key, value) => value === undefined ? call('readwrite', 'delete', key) - : call('readwrite', 'put', value, key) + : call('readwrite', 'put', value, key); From aa35ecd69a1b2c1534adbb667e8b0e16f6ebbfc3 Mon Sep 17 00:00:00 2001 From: mrhut10 Date: Thu, 9 Apr 2020 03:00:39 +1000 Subject: [PATCH 3/6] remove semicolons --- idb.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/idb.js b/idb.js index 6041840..df83af0 100644 --- a/idb.js +++ b/idb.js @@ -1,4 +1,4 @@ -const isBrowser = typeof window !== 'undefined'; +const isBrowser = typeof window !== 'undefined' const dbp = new Promise((resolve, reject) => { if (isBrowser){ @@ -12,22 +12,22 @@ const dbp = new Promise((resolve, reject) => { }) export const call = async (type, method, ...args) => { - const db = await dbp; + const db = await dbp return new Promise((resolve, reject) => { if (isBrowser){ - const transaction = db.transaction('idb', type); - const store = transaction.objectStore('idb'); - const req = store[method](...args); - transaction.oncomplete = () => resolve(req); - transaction.onabort = transaction.onerror = () => reject(transaction.error); + const transaction = db.transaction('idb', type) + const store = transaction.objectStore('idb') + const req = store[method](...args) + transaction.oncomplete = () => resolve(req) + transaction.onabort = transaction.onerror = () => reject(transaction.error) } else { - resolve(undefined); + resolve(undefined) } - }); -}; + }) +} -export const get = async key => (await call('readonly', 'get', key)).result; +export const get = async key => (await call('readonly', 'get', key)).result export const set = (key, value) => value === undefined ? call('readwrite', 'delete', key) - : call('readwrite', 'put', value, key); + : call('readwrite', 'put', value, key) From 24c146694f51e8e539ac6e618427444e9440c41b Mon Sep 17 00:00:00 2001 From: mrhut10 Date: Thu, 9 Apr 2020 03:07:28 +1000 Subject: [PATCH 4/6] change when isBrowser is checked --- idb.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/idb.js b/idb.js index df83af0..6cb772a 100644 --- a/idb.js +++ b/idb.js @@ -1,14 +1,13 @@ const isBrowser = typeof window !== 'undefined' const dbp = new Promise((resolve, reject) => { - if (isBrowser){ - const openreq = window.indexedDB.open('use-idb', 1) - openreq.onerror = () => reject(openreq.error) - openreq.onsuccess = () => resolve(openreq.result) - openreq.onupgradeneeded = () => openreq.result.createObjectStore('idb') - } else { - resolve(undefined) + if (!isBrowser) { + return resolve(undefined) } + const openreq = window.indexedDB.open('use-idb', 1) + openreq.onerror = () => reject(openreq.error) + openreq.onsuccess = () => resolve(openreq.result) + openreq.onupgradeneeded = () => openreq.result.createObjectStore('idb') }) export const call = async (type, method, ...args) => { From cf3ca57f3549f77b057edba3ddf7d89119db8fee Mon Sep 17 00:00:00 2001 From: mrhut10 Date: Tue, 12 May 2020 03:26:29 +1000 Subject: [PATCH 5/6] testing direct replacment of isBrowser with hasIDB --- idb.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/idb.js b/idb.js index 6cb772a..447c480 100644 --- a/idb.js +++ b/idb.js @@ -1,7 +1,7 @@ -const isBrowser = typeof window !== 'undefined' +const hasIDB = typeof indexedDB !== 'undefined' const dbp = new Promise((resolve, reject) => { - if (!isBrowser) { + if (!hasIDB) { return resolve(undefined) } const openreq = window.indexedDB.open('use-idb', 1) @@ -13,7 +13,7 @@ const dbp = new Promise((resolve, reject) => { export const call = async (type, method, ...args) => { const db = await dbp return new Promise((resolve, reject) => { - if (isBrowser){ + if (hasIDB){ const transaction = db.transaction('idb', type) const store = transaction.objectStore('idb') const req = store[method](...args) From 81ffbe3f002756c55d85631f3d95ef90acc2e97e Mon Sep 17 00:00:00 2001 From: mrhut10 Date: Tue, 12 May 2020 03:51:18 +1000 Subject: [PATCH 6/6] hasIDB dirrect replacement for isBroswer --- idb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idb.js b/idb.js index 447c480..c722cf6 100644 --- a/idb.js +++ b/idb.js @@ -1,4 +1,4 @@ -const hasIDB = typeof indexedDB !== 'undefined' +const hasIDB = typeof window !== 'undefined' const dbp = new Promise((resolve, reject) => { if (!hasIDB) {