From 035eeb8fc04f766aac3eabcc2a1073f571d4de1c Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Wed, 17 Jan 2024 11:30:35 +0100 Subject: [PATCH] Add subscribe method to state class This makes the State class fully compatible with svelte stores --- package.json | 2 +- state.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e57ba03..0427f39 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "skooma", - "version": "1.6.1", + "version": "1.7.0", "author": "darkwiiplayer", "license": "Unlicense", "main": "skooma.js", diff --git a/state.js b/state.js index 00c993d..b4306e1 100644 --- a/state.js +++ b/state.js @@ -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 } }