From 33dc7007cf13c3f78c95766be57aa9cc139a7d99 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 22 Feb 2022 16:42:52 -1000 Subject: [PATCH 1/2] Test naive blocking synchronous write queue idea --- lib/storage/index.js | 48 +++++++++++++++++++++++++++- lib/storage/providers/LocalForage.js | 43 ++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/lib/storage/index.js b/lib/storage/index.js index f5a621f46..eaa724f87 100644 --- a/lib/storage/index.js +++ b/lib/storage/index.js @@ -5,4 +5,50 @@ const Storage = Platform.select({ native: () => require('./NativeStorage').default, })(); -export default Storage; +class SynchronousWriteQueue { + constructor() { + this.isWriting = false; + this.queue = []; + } + + push(item) { + this.queue.push(item); + this.process(); + } + + process() { + if (this.isWriting) { + return; + } + + this.isWriting = true; + const { + key, value, resolve, reject, + } = this.queue.shift(); + Storage.setItem(key, value) + .then(resolve) + .then(() => { + this.isWriting = false; + this.process(); + }) + .catch(reject); + } +} + +const pairQueue = new SynchronousWriteQueue(); + +const StorageWithWriteQueue = { + ...Storage, + setItem(key, value) { + return new Promise((resolve, reject) => { + pairQueue.push({ + key, + value, + resolve, + reject + }); + }); + }, +}; + +export default StorageWithWriteQueue; diff --git a/lib/storage/providers/LocalForage.js b/lib/storage/providers/LocalForage.js index 3a8c4634a..431f3a24b 100644 --- a/lib/storage/providers/LocalForage.js +++ b/lib/storage/providers/LocalForage.js @@ -12,6 +12,43 @@ localforage.config({ name: 'OnyxDB' }); +class SynchronousWriteQueue { + constructor() { + this.isWriting = false; + this.queue = []; + } + + push(item) { + this.queue.push(item); + this.process(); + } + + process() { + if (this.isWriting) { + return; + } + + this.isWriting = true; + const next = this.queue.shift(); + if (!next) { + this.isWriting = false; + return; + } + + const { + key, value, resolve, reject, + } = next; + localforage.setItem(key, value) + .then(resolve) + .catch(reject) + .finally(() => { + this.isWriting = false; + this.process(); + }); + } +} + +const pairQueue = new SynchronousWriteQueue(); const provider = { /** * Get multiple key-value pairs for the give array of keys in a batch @@ -90,7 +127,11 @@ const provider = { * @param {*} value * @return {Promise} */ - setItem: localforage.setItem, + setItem: (key, value) => new Promise((resolve, reject) => { + pairQueue.push({ + key, value, resolve, reject, + }); + }), }; export default provider; From 2314d8e68896698c54f6c8608677d6a322b19f20 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 22 Feb 2022 16:44:38 -1000 Subject: [PATCH 2/2] remove other code --- lib/storage/index.js | 48 +------------------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/lib/storage/index.js b/lib/storage/index.js index eaa724f87..f5a621f46 100644 --- a/lib/storage/index.js +++ b/lib/storage/index.js @@ -5,50 +5,4 @@ const Storage = Platform.select({ native: () => require('./NativeStorage').default, })(); -class SynchronousWriteQueue { - constructor() { - this.isWriting = false; - this.queue = []; - } - - push(item) { - this.queue.push(item); - this.process(); - } - - process() { - if (this.isWriting) { - return; - } - - this.isWriting = true; - const { - key, value, resolve, reject, - } = this.queue.shift(); - Storage.setItem(key, value) - .then(resolve) - .then(() => { - this.isWriting = false; - this.process(); - }) - .catch(reject); - } -} - -const pairQueue = new SynchronousWriteQueue(); - -const StorageWithWriteQueue = { - ...Storage, - setItem(key, value) { - return new Promise((resolve, reject) => { - pairQueue.push({ - key, - value, - resolve, - reject - }); - }); - }, -}; - -export default StorageWithWriteQueue; +export default Storage;