Skip to content

Commit 59e2046

Browse files
authored
refactor: replace store with createClassyStore across the codebase (#4)
* refactor: replace `store` with `createClassyStore` across the codebase * refactor: standardize selector function parameter naming for `useStore` across codebase
1 parent a6d0054 commit 59e2046

26 files changed

Lines changed: 352 additions & 306 deletions

.changeset/tiny-knives-drop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@codebelt/classy-store": minor
3+
---
4+
5+
replace `store` with `createClassyStore` across the codebase

examples/rendering/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"dev": "bun --hot src/index.ts",
7+
"dev": "bun --hot --port 3001 src/index.ts",
88
"start": "NODE_ENV=production bun src/index.ts",
99
"build": "bun run build.ts"
1010
},

examples/rendering/src/AsyncDemo.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {postStore} from './stores';
55
import {useRenderCount} from './useRenderCount';
66

77
function LoadingIndicator() {
8-
const loading = useStore(postStore, (s) => s.loading);
8+
const loading = useStore(postStore, (store) => store.loading);
99
const renders = useRenderCount();
1010
return (
1111
<div className="flex items-center justify-between bg-amber-500/10 border border-amber-500/20 rounded-lg px-3 py-2.5">
@@ -51,7 +51,7 @@ function LoadingIndicator() {
5151
}
5252

5353
function ErrorDisplay() {
54-
const error = useStore(postStore, (s) => s.error);
54+
const error = useStore(postStore, (store) => store.error);
5555
const renders = useRenderCount();
5656
return (
5757
<div className="flex items-center justify-between bg-red-500/10 border border-red-500/20 rounded-lg px-3 py-2.5">
@@ -71,7 +71,7 @@ function ErrorDisplay() {
7171
}
7272

7373
function PostList() {
74-
const posts = useStore(postStore, (s) => s.posts);
74+
const posts = useStore(postStore, (store) => store.posts);
7575
const renders = useRenderCount();
7676
return (
7777
<div className="bg-emerald-500/10 border border-emerald-500/20 rounded-lg px-3 py-2.5">
@@ -102,7 +102,7 @@ function PostList() {
102102
}
103103

104104
function PostCount() {
105-
const count = useStore(postStore, (s) => s.count);
105+
const count = useStore(postStore, (store) => store.count);
106106
const renders = useRenderCount();
107107
return (
108108
<div className="flex items-center gap-2">

examples/rendering/src/CollectionsDemo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ function TagList() {
107107
}
108108

109109
function CountDisplay() {
110-
const userCount = useStore(collectionStore, (s) => s.userCount);
111-
const tagCount = useStore(collectionStore, (s) => s.tagCount);
110+
const userCount = useStore(collectionStore, (store) => store.userCount);
111+
const tagCount = useStore(collectionStore, (store) => store.tagCount);
112112
const renders = useRenderCount();
113113

114114
return (

examples/rendering/src/ReactiveFundamentalsDemo.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import {store} from '@codebelt/classy-store';
1+
import {createClassyStore} from '@codebelt/classy-store';
22
import {useStore} from '@codebelt/classy-store/react';
33
import {useState} from 'react';
44
import {Panel} from './Panel';
55
import {RenderBadge} from './RenderBadge';
66
import {CounterStore, counterStore} from './stores';
77
import {useRenderCount} from './useRenderCount';
88

9-
const batchStore = store(new CounterStore());
9+
const batchStore = createClassyStore(new CounterStore());
1010

1111
const names = ['World', 'Bun', 'React', 'Store'];
1212
let nameIndex = 0;
1313

1414
function CountCard() {
15-
const count = useStore(counterStore, (s) => s.count);
15+
const count = useStore(counterStore, (store) => store.count);
1616
const renders = useRenderCount();
1717
return (
1818
<div className="flex-1 flex items-center justify-between bg-indigo-500/10 border border-indigo-500/20 rounded-lg px-3 py-2.5">
@@ -30,7 +30,7 @@ function CountCard() {
3030
}
3131

3232
function NameCard() {
33-
const name = useStore(counterStore, (s) => s.name);
33+
const name = useStore(counterStore, (store) => store.name);
3434
const renders = useRenderCount();
3535
return (
3636
<div className="flex-1 flex items-center justify-between bg-violet-500/10 border border-violet-500/20 rounded-lg px-3 py-2.5">
@@ -48,7 +48,7 @@ function NameCard() {
4848
}
4949

5050
function BatchCard() {
51-
const count = useStore(batchStore, (s) => s.count);
51+
const count = useStore(batchStore, (store) => store.count);
5252
const renders = useRenderCount();
5353
return (
5454
<div className="flex-1 flex items-center justify-between bg-amber-500/10 border border-amber-500/20 rounded-lg px-3 py-2.5">

examples/rendering/src/StructuralSharingDemo.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,33 @@ function SnapshotTree() {
8585
<div className="text-xs text-zinc-400 uppercase tracking-wide mb-3">
8686
Snapshot Reference Tree
8787
</div>
88-
<TreeNode label="root" getValue={(s) => s}>
89-
<TreeNode label=".metadata" getValue={(s) => s.metadata} depth={1} />
90-
<TreeNode label=".content" getValue={(s) => s.content} depth={1}>
88+
<TreeNode label="root" getValue={(store) => store}>
89+
<TreeNode
90+
label=".metadata"
91+
getValue={(store) => store.metadata}
92+
depth={1}
93+
/>
94+
<TreeNode
95+
label=".content"
96+
getValue={(store) => store.content}
97+
depth={1}
98+
>
9199
<TreeNode
92100
label=".sections[0]"
93-
getValue={(s) => s.content.sections[0]}
101+
getValue={(store) => store.content.sections[0]}
94102
depth={2}
95103
/>
96104
<TreeNode
97105
label=".sections[1]"
98-
getValue={(s) => s.content.sections[1]}
106+
getValue={(store) => store.content.sections[1]}
99107
depth={2}
100108
/>
101109
</TreeNode>
102-
<TreeNode label=".settings" getValue={(s) => s.settings} depth={1} />
110+
<TreeNode
111+
label=".settings"
112+
getValue={(store) => store.settings}
113+
depth={1}
114+
/>
103115
</TreeNode>
104116
</div>
105117
);

examples/rendering/src/persistStores.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import {reactiveMap, reactiveSet, store} from '@codebelt/classy-store';
1+
import {
2+
createClassyStore,
3+
reactiveMap,
4+
reactiveSet,
5+
} from '@codebelt/classy-store';
26
import {persist} from '@codebelt/classy-store/utils';
37

48
// ── Simple Store ────────────────────────────────────────────────────────────
@@ -43,7 +47,7 @@ export class PreferencesStore {
4347
}
4448
}
4549

46-
export const preferencesStore = store(new PreferencesStore());
50+
export const preferencesStore = createClassyStore(new PreferencesStore());
4751

4852
export const preferencesHandle = persist(preferencesStore, {
4953
name: 'preferences',
@@ -145,7 +149,7 @@ export class KitchenSinkStore {
145149
}
146150
}
147151

148-
export const kitchenSinkStore = store(new KitchenSinkStore());
152+
export const kitchenSinkStore = createClassyStore(new KitchenSinkStore());
149153

150154
export const kitchenSinkHandle = persist(kitchenSinkStore, {
151155
name: 'kitchen-sink',

examples/rendering/src/stores.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import {reactiveMap, reactiveSet, store} from '@codebelt/classy-store';
1+
import {
2+
createClassyStore,
3+
reactiveMap,
4+
reactiveSet,
5+
} from '@codebelt/classy-store';
26

37
export class CounterStore {
48
count = 0;
@@ -84,7 +88,7 @@ export class DocumentStore {
8488
}
8589

8690
updateSectionBody(id: number, body: string) {
87-
const section = this.content.sections.find((s) => s.id === id);
91+
const section = this.content.sections.find((store) => store.id === id);
8892
if (section) section.body = body;
8993
}
9094

@@ -145,7 +149,7 @@ export class CollectionStore {
145149
}
146150
}
147151

148-
export const counterStore = store(new CounterStore());
149-
export const postStore = store(new PostStore());
150-
export const documentStore = store(new DocumentStore());
151-
export const collectionStore = store(new CollectionStore());
152+
export const counterStore = createClassyStore(new CounterStore());
153+
export const postStore = createClassyStore(new PostStore());
154+
export const documentStore = createClassyStore(new DocumentStore());
155+
export const collectionStore = createClassyStore(new CollectionStore());

src/collections/collections.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {describe, expect, test} from 'bun:test';
2-
import {store, subscribe} from '../core/core';
2+
import {createClassyStore, subscribe} from '../core/core';
33
import {snapshot} from '../snapshot/snapshot';
44
import {reactiveMap, reactiveSet} from './collections';
55

@@ -87,7 +87,7 @@ describe('reactiveMap()', () => {
8787
});
8888

8989
test('triggers store subscription on set', async () => {
90-
const s = store({users: reactiveMap<string, string>()});
90+
const s = createClassyStore({users: reactiveMap<string, string>()});
9191
let count = 0;
9292
subscribe(s, () => count++);
9393

@@ -98,7 +98,7 @@ describe('reactiveMap()', () => {
9898
});
9999

100100
test('triggers store subscription on delete', async () => {
101-
const s = store({
101+
const s = createClassyStore({
102102
users: reactiveMap([['id1', 'Alice']] as [string, string][]),
103103
});
104104
let count = 0;
@@ -111,7 +111,7 @@ describe('reactiveMap()', () => {
111111
});
112112

113113
test('triggers store subscription on clear', async () => {
114-
const s = store({
114+
const s = createClassyStore({
115115
users: reactiveMap([
116116
['a', 1],
117117
['b', 2],
@@ -127,7 +127,9 @@ describe('reactiveMap()', () => {
127127
});
128128

129129
test('snapshot captures map data', async () => {
130-
const s = store({m: reactiveMap([['k', 'v']] as [string, string][])});
130+
const s = createClassyStore({
131+
m: reactiveMap([['k', 'v']] as [string, string][]),
132+
});
131133
const snap = snapshot(s);
132134
expect(snap.m._entries).toEqual([['k', 'v']]);
133135
// Snapshot is frozen
@@ -188,7 +190,7 @@ describe('reactiveSet()', () => {
188190
});
189191

190192
test('triggers store subscription on add', async () => {
191-
const s = store({tags: reactiveSet<string>()});
193+
const s = createClassyStore({tags: reactiveSet<string>()});
192194
let count = 0;
193195
subscribe(s, () => count++);
194196

@@ -199,7 +201,7 @@ describe('reactiveSet()', () => {
199201
});
200202

201203
test('triggers store subscription on delete', async () => {
202-
const s = store({tags: reactiveSet(['a', 'b'])});
204+
const s = createClassyStore({tags: reactiveSet(['a', 'b'])});
203205
let count = 0;
204206
subscribe(s, () => count++);
205207

@@ -210,7 +212,7 @@ describe('reactiveSet()', () => {
210212
});
211213

212214
test('triggers store subscription on clear', async () => {
213-
const s = store({tags: reactiveSet(['a', 'b'])});
215+
const s = createClassyStore({tags: reactiveSet(['a', 'b'])});
214216
let count = 0;
215217
subscribe(s, () => count++);
216218

@@ -221,7 +223,7 @@ describe('reactiveSet()', () => {
221223
});
222224

223225
test('snapshot captures set data', async () => {
224-
const s = store({t: reactiveSet([1, 2, 3])});
226+
const s = createClassyStore({t: reactiveSet([1, 2, 3])});
225227
const snap = snapshot(s);
226228
expect(snap.t._items).toEqual([1, 2, 3]);
227229
expect(Object.isFrozen(snap.t)).toBe(true);
@@ -245,7 +247,7 @@ describe('collections inside class store', () => {
245247
}
246248

247249
test('class methods mutate collections reactively', async () => {
248-
const s = store(new ProjectStore());
250+
const s = createClassyStore(new ProjectStore());
249251
let count = 0;
250252
subscribe(s, () => count++);
251253

src/collections/collections.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {PROXYABLE} from '../utils/internal/internal';
33
// ── ReactiveMap ───────────────────────────────────────────────────────────────
44

55
/**
6-
* A Map-like class backed by a plain array so `store()` can proxy mutations.
6+
* A Map-like class backed by a plain array so `createClassyStore()` can proxy mutations.
77
*
88
* Native `Map` uses internal slots that ES6 Proxy can't intercept, so mutations
99
* like `.set()` would be invisible to the store. `ReactiveMap` solves this by
1010
* storing entries in a plain array (`_entries`) that the proxy can track.
1111
*
1212
* Usage:
1313
* ```ts
14-
* const myStore = store({ users: reactiveMap<string, User>() });
14+
* const myStore = createClassyStore({ users: reactiveMap<string, User>() });
1515
* myStore.users.set('id1', { name: 'Alice' }); // reactive
1616
* ```
1717
*/
@@ -106,15 +106,15 @@ export class ReactiveMap<K, V> {
106106
// ── ReactiveSet ───────────────────────────────────────────────────────────────
107107

108108
/**
109-
* A Set-like class backed by a plain array so `store()` can proxy mutations.
109+
* A Set-like class backed by a plain array so `createClassyStore()` can proxy mutations.
110110
*
111111
* Native `Set` uses internal slots that ES6 Proxy can't intercept, so mutations
112112
* like `.add()` would be invisible to the store. `ReactiveSet` solves this by
113113
* storing items in a plain array (`_items`) that the proxy can track.
114114
*
115115
* Usage:
116116
* ```ts
117-
* const myStore = store({ tags: reactiveSet<string>(['urgent']) });
117+
* const myStore = createClassyStore({ tags: reactiveSet<string>(['urgent']) });
118118
* myStore.tags.add('bug'); // reactive
119119
* ```
120120
*/
@@ -198,7 +198,7 @@ export class ReactiveSet<T> {
198198

199199
/**
200200
* Creates a reactive Map-like collection backed by a plain array.
201-
* Wrap the parent object with `store()` for full reactivity.
201+
* Wrap the parent object with `createClassyStore()` for full reactivity.
202202
*
203203
* @param initial - Optional iterable of `[key, value]` pairs.
204204
*/
@@ -210,7 +210,7 @@ export function reactiveMap<K, V>(
210210

211211
/**
212212
* Creates a reactive Set-like collection backed by a plain array.
213-
* Wrap the parent object with `store()` for full reactivity.
213+
* Wrap the parent object with `createClassyStore()` for full reactivity.
214214
*
215215
* @param initial - Optional iterable of values.
216216
*/

0 commit comments

Comments
 (0)