Created
January 29, 2018 01:53
-
-
Save Millsky/060ba0627bf67bfd3ac8f74a91e381e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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