Add subscribe method to state class

This makes the State class fully compatible with svelte stores
This commit is contained in:
Talia 2024-01-17 11:30:35 +01:00
parent 69a2aa1ca3
commit 035eeb8fc0
2 changed files with 12 additions and 1 deletions

View file

@ -1,6 +1,6 @@
{
"name": "skooma",
"version": "1.6.1",
"version": "1.7.0",
"author": "darkwiiplayer",
"license": "Unlicense",
"main": "skooma.js",

View file

@ -160,6 +160,17 @@ export class State extends SimpleState {
return this.#target[prop]
}
subscribe(prop, callback) {
if (!callback) return this.subscribe("value", prop)
const controller = new AbortController()
this.addEventListener("change", ({final}) => {
if (final.has(prop)) return callback(final.get(prop))
}, {signal: controller.signal})
callback(this.value)
return () => controller.abort()
}
// Backwards compatibility
get proxy() { return this.values }
}