Skip to content

Commit

Permalink
fix: use null object as store
Browse files Browse the repository at this point in the history
fixes optimization #211
  • Loading branch information
unional committed May 3, 2024
1 parent 89dc1c7 commit 1626da8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/stable-store/ts/store.create_store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { storeMap } from './store.ctx.js'

afterEach(() => {
idAssertions.splice(0, idAssertions.length)
storeMap.clear()
Object.keys(storeMap).forEach((k) => {
delete storeMap[k]
})
})

it('can create store with string key', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/stable-store/ts/store.ctx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { StoreKey } from './store.internal.types.js'
import type { Store } from './store.types.js'

export const storeMap = new Map<StoreKey, [store: Store<unknown>, assertion?: ((id: string) => void) | undefined]>()
export const storeMap: Record<StoreKey, [store: Store<unknown>, assertion?: ((id: string) => void) | undefined]> =
Object.create(null)
4 changes: 3 additions & 1 deletion packages/stable-store/ts/store.get_store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ it('returns the same store if the key is the same string', () => {

afterEach(() => {
idAssertions.splice(0, idAssertions.length)
storeMap.clear()
Object.keys(storeMap).forEach((k) => {
delete storeMap[k]
})
})

it('can assert against string key', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/stable-store/ts/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function createStore<V>(
options?: StoreOptions<V> | undefined
): Store<V> {
assertID(id)
var c = storeMap.get(id)
var c = storeMap[id]
if (c) {
// biome-ignore lint/correctness/noInnerDeclarations: on purpose
var [s, a] = c
Expand Down Expand Up @@ -108,7 +108,7 @@ export function createStore<V>(
var onSet = listenerAdder<V>(setListeners)

var store = { get, onGet, set, onSet }
storeMap.set(id, [store, options?.idAssertion])
storeMap[id] = [store, options?.idAssertion]
return store
}

Expand Down Expand Up @@ -146,7 +146,7 @@ function listenerAdder<V>(listeners: Array<(value: V) => void>) {
*/
export function getStore<V>(id: string | symbol): Store<V> {
assertID(id)
var c = storeMap.get(id)
var c = storeMap[id]
if (!c) throw new Error(`Store ${id.toString()} not found`)
var [s, a] = c
if (a) assertIDInternal(id, a)
Expand Down

0 comments on commit 1626da8

Please sign in to comment.