Skip to content

Commit

Permalink
fix: hide symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Jun 5, 2023
1 parent 4e850ea commit 2f26541
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/three-kangaroos-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'stable-store': patch
---

Add JSDocs comments for `onGet` and `onSet`.

Hide the internal `brandedSymbol`.
9 changes: 9 additions & 0 deletions packages/stable-store/ts/store.on_get.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
5 changes: 2 additions & 3 deletions packages/stable-store/ts/store.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 14 additions & 2 deletions packages/stable-store/ts/store.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@ export type Store<V> = {
* 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
}
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f26541

Please sign in to comment.