Created
March 28, 2019 15:23
-
-
Save Akiyamka/b9bdcb6ba69fa4116c789ba099b3544e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export default class Watcher { | |
constructor() { | |
this._mem = []; | |
} | |
_compareFunction(val1, val2) { | |
return Object.is(val1, val2); | |
} | |
setCallbacks(callbacks) { | |
callbacks.forEach((cb, i) => { | |
this._mem[i] = cb(this._mem[i]); | |
}); | |
} | |
whenChange(variables, callback, customCompare) { | |
return prevVariables => { | |
if (prevVariables === undefined) { | |
callback(...variables); | |
return variables; | |
} | |
const nothingChanges = variables.every( | |
(currVar, i) => typeof customCompare === 'function' | |
? customCompare(currVar, prevVariables[i]) | |
: this._compareFunction(currVar, prevVariables[i]) | |
); | |
if (nothingChanges) return variables; | |
callback(...variables); | |
return variables; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.