From ead45312d5aa755cdde20e9a05e1db273644c33b Mon Sep 17 00:00:00 2001 From: Piotr Jaworski Date: Thu, 2 Aug 2018 17:10:51 +0200 Subject: [PATCH 1/5] Added persistance loading and automatic expiry --- atom.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/atom.js b/atom.js index b908851..1180112 100644 --- a/atom.js +++ b/atom.js @@ -573,8 +573,32 @@ me.bind = me.on; me.unbind = me.off; + if (config.persistencePeriod) { + const persistedData = config.persistenceLib.get(config.modelName) || {}; + const lastStorageUpdateTimestamp = persistedData.lastStorageUpdateTimestamp; + if (lastStorageUpdateTimestamp && Date.now() - lastStorageUpdateTimestamp > config.persistencePeriod) { + config.persistenceLib.remove(config.modelName); + } + } + if (args.length) { args[2] = false; // disable initial validation + + var finalMap = {}; + + if (me.persistenceLib) { + Object.assign( + finalMap, + config.persistenceLib.get(config.modelName), + ) + } + + if (isObject(keyOrMap)) { + Object.assign(finalMap, keyOrMap); + } else { + finalMap[args[0]] = args[1]; + } + me.set.apply(me, args); } From 7e053f070a8d2db7d311dcc130e444d6413cbc72 Mon Sep 17 00:00:00 2001 From: Piotr Jaworski Date: Thu, 2 Aug 2018 17:11:50 +0200 Subject: [PATCH 2/5] Added readme entry --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 58102ff..3c08e50 100644 --- a/README.md +++ b/README.md @@ -389,6 +389,9 @@ car.set({ engine: '746cc', brand: 'Syrena' }); NOTE: One extra parameter is always added to modelData: `lastStorageUpdateTimestamp`. Feel free to use it for syncing data between tabs. +## SETUP: `persistencePeriod: number` + +A number of milliseconds that the model should be kept. It will be removed when trying to load it from storage for the first time after expiration ## SETUP: `onChange: function` `onChange(key, value)` is triggered for every change in a model From 8c88c57b3d252efa3139080df540a85d80036d76 Mon Sep 17 00:00:00 2001 From: Piotr Jaworski Date: Thu, 2 Aug 2018 17:16:30 +0200 Subject: [PATCH 3/5] Added a config check --- atom.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/atom.js b/atom.js index 1180112..63f69a2 100644 --- a/atom.js +++ b/atom.js @@ -262,6 +262,11 @@ throw new Error('`persistenceLib` property in atom-js model needs to expose `set` method.'); } } + if (config.persistencePeriod) { + if (!config.persistenceLib) { + throw new Error('Persistence expiration needs a persistece library. Please provide a "persistenceLib" property.'); + } + } // Execute the next function in the async queue. function doNext() { if (q) { From 9db6aaf1ad8ec8e20b39b3057ef880a75df01d21 Mon Sep 17 00:00:00 2001 From: Piotr Jaworski Date: Thu, 2 Aug 2018 17:17:05 +0200 Subject: [PATCH 4/5] Fixed typo --- atom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom.js b/atom.js index 63f69a2..8b58293 100644 --- a/atom.js +++ b/atom.js @@ -264,7 +264,7 @@ } if (config.persistencePeriod) { if (!config.persistenceLib) { - throw new Error('Persistence expiration needs a persistece library. Please provide a "persistenceLib" property.'); + throw new Error('Persistence expiration needs a persistence library. Please provide a "persistenceLib" property.'); } } // Execute the next function in the async queue. From 7092e23322dc408256a6cc3358de3f40bcee3660 Mon Sep 17 00:00:00 2001 From: Piotr Jaworski Date: Thu, 2 Aug 2018 20:43:41 +0200 Subject: [PATCH 5/5] Removed trailing comma --- atom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom.js b/atom.js index 8b58293..cc3a40a 100644 --- a/atom.js +++ b/atom.js @@ -594,7 +594,7 @@ if (me.persistenceLib) { Object.assign( finalMap, - config.persistenceLib.get(config.modelName), + config.persistenceLib.get(config.modelName) ) }