minor sdk tweaks (#2828)

This commit is contained in:
Aiden McClelland
2025-02-12 15:08:13 -07:00
committed by GitHub
parent 6dc9a11a89
commit 890c31ba74
7 changed files with 214 additions and 19 deletions

View File

@@ -12,7 +12,7 @@ export class GetStore<Store, StoreValue> {
) {}
/**
* Returns the value of Store at the provided path. Restart the service if the value changes
* Returns the value of Store at the provided path. Reruns the context from which it has been called if the underlying value changes
*/
const() {
return this.effects.store.get<Store, StoreValue>({
@@ -32,7 +32,7 @@ export class GetStore<Store, StoreValue> {
}
/**
* Watches the value of Store at the provided path. Takes a custom callback function to run whenever the value changes
* Watches the value of Store at the provided path. Returns an async iterator that yields whenever the value changes
*/
async *watch() {
while (true) {
@@ -48,6 +48,33 @@ export class GetStore<Store, StoreValue> {
await waitForNext
}
}
/**
* Watches the value of Store at the provided path. Takes a custom callback function to run whenever the value changes
*/
onChange(
callback: (value: StoreValue | null, error?: Error) => void | Promise<void>,
) {
;(async () => {
for await (const value of this.watch()) {
try {
await callback(value)
} catch (e) {
console.error(
"callback function threw an error @ GetStore.onChange",
e,
)
}
}
})()
.catch((e) => callback(null, e))
.catch((e) =>
console.error(
"callback function threw an error @ GetStore.onChange",
e,
),
)
}
}
export function getStore<Store, StoreValue>(
effects: Effects,