Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion 2019/Aug/Week4/reactive-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
*/
module.exports = class ReactiveVariables {
constructor(variables) {

const obj = {};
const variablesObj = {};
Object.keys(variables).forEach(key => {
if(typeof variables[key] === 'function') {
Object.defineProperty(obj, key, {
get: () => {
const value = variables[key].call(obj);
if(typeof variablesObj[key] === 'object') {
let shouldNew = false;
Object.keys(value).forEach(k => {
if(variablesObj[key][k] !== value[k]) {
shouldNew = true;
}
});
if(shouldNew) {
variablesObj[key] = value;
}
}else {
variablesObj[key] = value;
}
return variablesObj[key];
}
});
}else {
obj[key] = variables[key];
}
});
return obj;
}
}