Add some helpful comments to state.js
This commit is contained in:
parent
2e540dbc6f
commit
8d9dc8ae7f
3 changed files with 7 additions and 2 deletions
|
@ -81,7 +81,7 @@
|
|||
// outputs: "meep moop"
|
||||
</code-block>
|
||||
|
||||
No, this has nothing to do with playing audio.
|
||||
No, this still has nothing to do with playing audio.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
<a class="back" href="..">Module Index</a>
|
||||
|
||||
<h1 class="js module">listener.js</h1>
|
||||
<h1 class="js module">state.js</h1>
|
||||
|
||||
<code-block>import {State} from 'state.js'</code-block>
|
||||
|
||||
|
|
5
state.js
5
state.js
|
@ -25,6 +25,8 @@ export class State extends EventTarget {
|
|||
|
||||
this.addEventListener
|
||||
|
||||
// Try running a "<name>Changed" method for every changed property
|
||||
// Can be disabled to maybe squeeze out some performance
|
||||
if (options.methods ?? true) {
|
||||
this.addEventListener("change", ({changes}) => {
|
||||
new Map(changes).forEach((value, prop) => {
|
||||
|
@ -34,9 +36,11 @@ export class State extends EventTarget {
|
|||
}
|
||||
}
|
||||
|
||||
// When you only need one value, you can skip the proxy.
|
||||
set value(value) { this.proxy.value = value }
|
||||
get value() { return this.proxy.value }
|
||||
|
||||
// Anounces that a prop has changed
|
||||
emit(prop, value) {
|
||||
if (this.#options.defer ?? true) {
|
||||
if (!this.#queue) {
|
||||
|
@ -84,6 +88,7 @@ export class StoredState extends State {
|
|||
this.emit(key, value)
|
||||
}
|
||||
|
||||
// Listen for changes from other windows
|
||||
addEventListener("storage", event => {
|
||||
let prop = event.key
|
||||
if (prop === this.#valueKey) prop = 'value'
|
||||
|
|
Loading…
Reference in a new issue