Skip to content

Instantly share code, notes, and snippets.

@Millsky
Created January 29, 2018 01:53
Show Gist options
  • Save Millsky/060ba0627bf67bfd3ac8f74a91e381e5 to your computer and use it in GitHub Desktop.
Save Millsky/060ba0627bf67bfd3ac8f74a91e381e5 to your computer and use it in GitHub Desktop.
function observer() {
const data = {};
const callbacksObj = {};
const set = (property, value) => {
data[property] = value;
notify(property);
};
const notify = (property) => {
if(!property) {
throw new TypeError('Property "property" is not type string')
}
if (callbacksObj[property]) {
callbacksObj[property]
.forEach((callback) => {
callback();
});
}
return true;
};
const subscribe = (property, callback) => {
if(!property && typeof property !== 'string') {
throw new TypeError('Property "property" is not type string')
}
if(!callback) {
throw new TypeError('Property Callback is not type function');
}
if(callbacksObj[property]) {
callbacksObj[property].push(callback.bind(null, data));
}
callbacksObj[property] = [];
callbacksObj[property].push(callback.bind(null, data));
};
return {
subscribe,
set,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment