From 13c7fa46ab86861645451c513a0eaac5a18585db Mon Sep 17 00:00:00 2001 From: Broda Noel Date: Wed, 3 Jun 2020 14:28:51 +0100 Subject: [PATCH] Bump 3.0.0 --- CHANGELOG.md | 6 +++--- build.js | 55 +++++++++++++++++++++++++++++++++++++++++++---- package-lock.json | 2 +- package.json | 2 +- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75e2c2c..074e402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ -### master (3.0.0) +### 3.0.0 -- Clone the object received on `set` -- Clone the object sent to `subscribers` on change event +- Clone the object received on `set` (immutable) +- Clone the object sent to `subscribers` on change event (immutable) ### 2.0.2 diff --git a/build.js b/build.js index 468b743..a00133c 100644 --- a/build.js +++ b/build.js @@ -41,6 +41,53 @@ return false; }; + /** + * Clone + * + * @param {*} - element to be cloned + * @returns {*} - cloned element + * + * @todo It'll work adequately as long as the data in the + * objects and arrays form a tree structure. That is, there + * isn't more than one reference to the same data in the + * object. + * + * @see https://stackoverflow.com/q/728360/1815449 + */ + function clone(obj) { + var copy; + + // Handle the 3 simple types, and null or undefined + if (null == obj || 'object' != typeof obj) return obj; + + // Handle Date + if (obj instanceof Date) { + copy = new Date(); + copy.setTime(obj.getTime()); + return copy; + } + + // Handle Array + if (obj instanceof Array) { + copy = []; + for (var i = 0, len = obj.length; i < len; i++) { + copy[i] = clone(obj[i]); + } + return copy; + } + + // Handle Object + if (obj instanceof Object) { + copy = {}; + for (var attr in obj) { + if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]); + } + return copy; + } + + throw new Error("Unable to copy obj! Its type isn't supported."); + } + let store = {}; /** * Manage a variable accross multiple files @@ -57,7 +104,7 @@ if (!store[key]) { // New key, let's create it and that's all. - store[key] = { value: newValue, subscribers: [] }; + store[key] = { value: clone(newValue), subscribers: [] }; return; } @@ -67,9 +114,9 @@ // then we don't notify return; } - store[key].value = newValue; + store[key].value = clone(newValue); - store[key].subscribers.forEach(callback => callback(newValue, prevValue)); + store[key].subscribers.forEach(callback => callback(clone(newValue), clone(prevValue))); }, /** @@ -77,7 +124,7 @@ * @param {*} key */ get(key) { - return !store[key] ? undefined : store[key].value; + return store[key] ? clone(store[key].value) : undefined; }, /** diff --git a/package-lock.json b/package-lock.json index 1a62c7d..6ca5dde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "duix", - "version": "2.0.2", + "version": "3.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 491e39f..265f392 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "duix", - "version": "2.0.2", + "version": "3.0.0", "author": { "name": "Broda Noel", "email": "brodanoel@gmail.com"