Skip to content

Commit

Permalink
chore(example): refactor example
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Dec 26, 2024
1 parent 60226b2 commit d6fe20c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
25 changes: 1 addition & 24 deletions examples/vanilla-base/src/counter.ts
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down
38 changes: 30 additions & 8 deletions examples/vanilla-base/src/store.ts
Original file line number Diff line number Diff line change
@@ -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<
{
Expand All @@ -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
>;
14 changes: 0 additions & 14 deletions examples/vanilla-base/src/worker.ts

This file was deleted.

0 comments on commit d6fe20c

Please sign in to comment.