Last active
September 3, 2019 03:49
-
-
Save zzzgit/25fdfc136ccc65fbb1cc56289d6c13f4 to your computer and use it in GitHub Desktop.
watcher implements in lit-element
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
attributeChangedCallback(attr, oldValue, newValue) { | |
super.attributeChangedCallback(attr, oldValue, newValue) | |
if (!this.watch || !this.watch.apply) { | |
return null | |
} | |
if (!this._watch) { | |
this._watch = this.watch() | |
} | |
if (this._watch.hasOwnProperty(attr)) { | |
const watcher = this._watch[attr] | |
watcher.bind(this, newValue, oldValue)() | |
} | |
} |
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
watch(targetNode, prop, cback) { | |
const callback = function (mutationsList, observer) { | |
const mutation = mutationsList.find(item => item.attributeName === prop) | |
if (mutation) { | |
cback(mutation) | |
} | |
} | |
// observer.disconnect() | |
const observer = new MutationObserver(callback) | |
observer.observe(targetNode, { attributes: true, childList: false, subtree: false }) | |
} |
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
updated(changedProperties) { | |
if (!this.watch || !this.watch.apply) { | |
return null | |
} | |
if (!this._watch) { | |
this._watch = this.watch() | |
} | |
changedProperties.forEach((oldValue, attr) => { | |
if (this._watch.hasOwnProperty(attr)) { | |
const watcher = this._watch[attr] | |
watcher.bind(this, this[attr], oldValue)() | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment