From 2f26541c77a39ce1e7adc10d49d96920cebe56f1 Mon Sep 17 00:00:00 2001 From: unional Date: Mon, 5 Jun 2023 01:00:37 -0700 Subject: [PATCH] fix: hide symbol --- .changeset/three-kangaroos-act.md | 7 +++++++ packages/stable-store/ts/store.on_get.spec.ts | 9 +++++++++ packages/stable-store/ts/store.ts | 5 ++--- packages/stable-store/ts/store.types.ts | 16 ++++++++++++++-- pnpm-lock.yaml | 2 +- 5 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 .changeset/three-kangaroos-act.md create mode 100644 packages/stable-store/ts/store.on_get.spec.ts diff --git a/.changeset/three-kangaroos-act.md b/.changeset/three-kangaroos-act.md new file mode 100644 index 0000000..5c16a95 --- /dev/null +++ b/.changeset/three-kangaroos-act.md @@ -0,0 +1,7 @@ +--- +'stable-store': patch +--- + +Add JSDocs comments for `onGet` and `onSet`. + +Hide the internal `brandedSymbol`. diff --git a/packages/stable-store/ts/store.on_get.spec.ts b/packages/stable-store/ts/store.on_get.spec.ts new file mode 100644 index 0000000..3b5c05e --- /dev/null +++ b/packages/stable-store/ts/store.on_get.spec.ts @@ -0,0 +1,9 @@ +import { createStore } from './store.js' +import { expect, it } from '@jest/globals' + +it('listen to get calls', () => { + expect.assertions(1) + const store = createStore('with-get-listener', { a: 1 }) + store.onGet(v => expect(v).toEqual({ a: 1 })) + store.get() +}) diff --git a/packages/stable-store/ts/store.ts b/packages/stable-store/ts/store.ts index 324cb51..216d91d 100644 --- a/packages/stable-store/ts/store.ts +++ b/packages/stable-store/ts/store.ts @@ -1,8 +1,7 @@ import { idAssertions, storeMap } from './store.ctx.js' -import { Store } from './store.types.js' +import { type Store } from './store.types.js' - -export const brandedSymbol = Symbol('internal branded symbol') +const brandedSymbol = Symbol('internal branded symbol') /** * Init value is required. diff --git a/packages/stable-store/ts/store.types.ts b/packages/stable-store/ts/store.types.ts index ea0635e..824638b 100644 --- a/packages/stable-store/ts/store.types.ts +++ b/packages/stable-store/ts/store.types.ts @@ -6,10 +6,22 @@ export type Store = { * Get the current value. */ get(): V - onGet(fn: (value: V) => void): () => void + /** + * Adds a listener function to be called whenever the value is retrieved. + * + * @param {function} listener - A callback function to be called whenever the value is retrieved. + * It should take in one parameter, the retrieved value. + * @return {function} A function that can be called to remove the listener from the list of listeners. + */ + onGet(listener: (value: V) => void): () => void /** * Set the value. */ set(value: V): void - onSet(fn: (value: V) => void): () => void + /** + * Registers a listener to be called when the value is set. + * + * @returns {() => void} An unregister l to remove the listener. + */ + onSet(listener: (value: V) => void): () => void } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 99e679b..59f6399 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -74,7 +74,7 @@ importers: specifier: workspace:^ version: link:../library global-store: - specifier: workspace:1.0.0-beta.21 + specifier: workspace:1.0.0-beta.22 version: link:../../packages/global-store react: specifier: ^18.0.0