Skip to content

Commit

Permalink
feat: add devtools support (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brentlok authored Jul 30, 2024
1 parent 5828cd1 commit c73109d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/tests/createStore.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,19 @@ describe('storage + computed', () => {
expect(getState().computedTest).toEqual(1)
})
})

describe('devtools', () => {
createStore({
test: 0,
})
// @ts-expect-error
const stores = globalThis['__stan-js__']
const { store, listen, updateStore } = stores.at(-1)

expect(store).toBeDefined()
expect(listen).toBeDefined()
expect(updateStore).toBeDefined()
updateStore({ test: 1 })

expect(store.test).toEqual(1)
})
16 changes: 16 additions & 0 deletions src/vanilla/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ export const createStore = <TState extends object>(stateRaw: TState) => {
}
}

Object.defineProperty(globalThis, '__stan-js__', {
// @ts-expect-error - Inject store to globalThis
value: [...(globalThis['__stan-js__'] ?? []), {
store: state,
listen: subscribe(storeKeys),
updateStore: (store: TState) =>
batchUpdates(() =>
Object.entries(store)
.forEach(([key, value]) => getAction(key as TKey)?.(value))
),
}],
configurable: true,
enumerable: false,
writable: true,
})

storeKeys.forEach(key => {
if (Object.getOwnPropertyDescriptor(stateRaw, key)?.get === undefined) {
return
Expand Down

0 comments on commit c73109d

Please sign in to comment.