Skip to content

Commit

Permalink
Bump 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BrodaNoel committed Jun 3, 2020
1 parent 69109b3 commit 13c7fa4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
55 changes: 51 additions & 4 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -67,17 +114,17 @@
// 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)));
},

/**
* Return the data associated to the key
* @param {*} key
*/
get(key) {
return !store[key] ? undefined : store[key].value;
return store[key] ? clone(store[key].value) : undefined;
},

/**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "duix",
"version": "2.0.2",
"version": "3.0.0",
"author": {
"name": "Broda Noel",
"email": "[email protected]"
Expand Down

0 comments on commit 13c7fa4

Please sign in to comment.