diff --git a/examples/vanilla-base/src/counter.ts b/examples/vanilla-base/src/counter.ts index 90ff14f..01bac4e 100644 --- a/examples/vanilla-base/src/counter.ts +++ b/examples/vanilla-base/src/counter.ts @@ -1,27 +1,4 @@ -import { create } from 'coaction'; -import { logger } from '@coaction/logger'; - -import { counter, type Counter } from './store'; - -const worker = new SharedWorker(new URL('./worker.ts', import.meta.url), { - type: 'module' -}); - -export const useWorkerStore = create<{ - counter: Counter; -}>( - { - counter - }, - { - worker, - middlewares: [ - logger({ - collapsed: false - }) - ] - } -); +import { useWorkerStore } from './store'; export function setupCounter(element: HTMLButtonElement) { useWorkerStore.subscribe(() => { diff --git a/examples/vanilla-base/src/store.ts b/examples/vanilla-base/src/store.ts index 716d745..95a94ef 100644 --- a/examples/vanilla-base/src/store.ts +++ b/examples/vanilla-base/src/store.ts @@ -1,4 +1,5 @@ -import type { Slices } from 'coaction'; +import logger from '@coaction/logger'; +import { create, type Slices, AsyncStore, SliceState } from 'coaction'; export type Counter = Slices< { @@ -10,11 +11,32 @@ export type Counter = Slices< 'counter' >; -export const counter: Counter = (set) => ({ - count: 0, - increment() { - set((draft) => { - draft.counter.count += 1; - }); +const worker = globalThis.SharedWorker + ? new SharedWorker(new URL('./store.ts', import.meta.url), { + type: 'module' + }) + : undefined; + +const store = create<{ + counter: Counter; +}>( + { + counter: (set) => ({ + count: 0, + increment() { + set((draft) => { + draft.counter.count += 1; + }); + } + }) + }, + { + ...(worker ? { worker } : {}), + middlewares: [logger({ collapsed: false })] } -}); +); + +export const useWorkerStore = store as AsyncStore< + SliceState<{ counter: Counter }>, + true +>; diff --git a/examples/vanilla-base/src/worker.ts b/examples/vanilla-base/src/worker.ts deleted file mode 100644 index 09ab71b..0000000 --- a/examples/vanilla-base/src/worker.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { create } from 'coaction'; -import { logger } from '@coaction/logger'; -import { type Counter, counter } from './store'; - -const store = create<{ - counter: Counter; -}>( - { - counter - }, - { - middlewares: [logger({ collapsed: false })] - } -);