Last active
January 16, 2019 20:53
-
-
Save da-moon/5999c3fcc28a1b501e380d44ad48d872 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
<div id="count"></div> | |
<button onclick="state.count++">++</button> | |
<script> | |
let currentEffect | |
class Dep { | |
constructor() { | |
this.subscribers = new Set() | |
} | |
depend() { | |
if (currentEffect) { | |
this.subscribers.add(currentEffect) | |
} | |
} | |
notify() { | |
this.subscribers.forEach(sub => { | |
sub() | |
}) | |
} | |
} | |
function effect(runner) { | |
currentEffect = runner | |
runner() | |
currentEffect = null | |
} | |
// --- | |
function observable(obj) { | |
Object.keys(obj).forEach(key => { | |
let value = obj[key] | |
const dep = new Dep() | |
Object.defineProperty(obj, key, { | |
get() { | |
dep.depend() | |
return value | |
}, | |
set(newValue) { | |
value = newValue | |
dep.notify() | |
} | |
}) | |
}) | |
return obj | |
} | |
const state = observable({ | |
count: 0 | |
}) | |
effect(() => { | |
document.getElementById('count').textContent = state.count | |
}) | |
state.count++ | |
</script> |
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
https://github.com/nuxt/nuxt.js/issues/2680 |
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
https://stackoverflow.com/questions/46805455/how-use-own-js-as-plugin-using-nuxt-js |
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
http://vuejs-templates.github.io/webpack/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment