Skip to content

Commit

Permalink
Bump 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BrodaNoel committed May 30, 2020
1 parent 3866e1b commit b0f5029
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 36 deletions.
20 changes: 12 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
### 2.0.0

- Replaced `callMeNow` for `fireImmediately`
- Now we only call the subscriber when the value actually changed

### 1.1.0
* Add ESM support

### 1.0.5
### 1.0.4
### 1.0.3
### 1.0.2
### 1.0.1
* New Documentation
- Add ESM support

### 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5

- New Documentation

### 1.0.0
* The Library

- The Library
89 changes: 62 additions & 27 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.duix = factory());
}(this, function () {
'use strict';
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.duix = factory());
}(this, (function () { 'use strict';

// Thanks to https://stackoverflow.com/a/25456134/1954789
const deepEqual = (x, y) => {
// TODO: this will not handle cyclical references !
// TODO: this does not check class name !

if (x === y) {
return true;
}

if (typeof x === 'object' && x !== null && typeof y === 'object' && y !== null) {
if (Object.keys(x).length !== Object.keys(y).length) {
return false;
}

for (const prop in x) {
if (!y.hasOwnProperty(prop)) {
return false;
}
if (!deepEqual(x[prop], y[prop])) {
return false;
}
}

return true;
}

return false;
};

/**
* {
Expand All @@ -17,27 +45,25 @@
*/
let store = {};

const _callsubscribers = (key, newValue, prevValue) => {
store[key].subscribers.forEach(callback => {
if (typeof callback === 'function') {
callback(newValue, prevValue);
}
});
};

var index = {
set(key, value) {
let currentValue = undefined;
set(key, newValue) {
let prevValue = undefined;

if (!store[key]) {
store[key] = { value, subscribers: [] };
} else {
currentValue = store[key].value;
store[key].value = value;
// New key, let's create it and that's all.
store[key] = { value: newValue, subscribers: [] };
return;
}

// TODO: Call callback only if the value really changed
_callsubscribers(key, value, currentValue);
prevValue = store[key].value;
if (deepEqual(newValue, prevValue)) {
// If we have a previous value, and if it is the same,
// then we don't notify
return;
}
store[key].value = newValue;

store[key].subscribers.forEach(callback => callback(newValue, prevValue));
},

get(key) {
Expand All @@ -47,23 +73,32 @@
subscribe(key, callback, options = {}) {
let index = 1;

if (typeof callback !== 'function') {
console.error(`Registering in duix for '${key}': Callback is not a function: `, callback);
}

// Set the default options
options = {
fireImmediately: false,
...options,
};

if (!store[key]) {
store[key] = { value: undefined, subscribers: [callback] };
} else {
index = store[key].subscribers.push(callback);
store[key] = { value: undefined, subscribers: [] };
}
index = store[key].subscribers.push(callback);

if (options.callMeNow) {
callback(this.get(key));
if (options.fireImmediately) {
callback(this.get(key), undefined);
}

// This returns the unsubscribe handler
return () => {
delete store[key].subscribers[index - 1];
};
}
},
};

return index;

}));
})));
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": "1.1.0",
"version": "2.0.0",
"author": {
"name": "Broda Noel",
"email": "brodanoel@gmail.com"
Expand Down

0 comments on commit b0f5029

Please sign in to comment.