mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 02:11:56 +00:00
chore: Add in the vault
This commit is contained in:
44
lib/util/getVault.ts
Normal file
44
lib/util/getVault.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Effects, EnsureStorePath } from "../types"
|
||||
|
||||
export class GetVault<Vault> {
|
||||
constructor(readonly effects: Effects, readonly key: keyof Vault & string) {}
|
||||
|
||||
/**
|
||||
* Returns the value of Store at the provided path. Restart the service if the value changes
|
||||
*/
|
||||
const() {
|
||||
return this.effects.vault.get({
|
||||
key: this.key,
|
||||
callback: this.effects.restart,
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Returns the value of Store at the provided path. Does nothing if the value changes
|
||||
*/
|
||||
once() {
|
||||
return this.effects.vault.get({
|
||||
key: this.key,
|
||||
callback: () => {},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the value of Store at the provided path. Takes a custom callback function to run whenever the value changes
|
||||
*/
|
||||
async *watch() {
|
||||
while (true) {
|
||||
let callback: () => void
|
||||
const waitForNext = new Promise<void>((resolve) => {
|
||||
callback = resolve
|
||||
})
|
||||
yield await this.effects.vault.get({
|
||||
key: this.key,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
}
|
||||
}
|
||||
export function getVault<Vault>(effects: Effects, key: keyof Vault & string) {
|
||||
return new GetVault<Vault>(effects, key)
|
||||
}
|
||||
Reference in New Issue
Block a user