Skip to content

Commit 09736cb

Browse files
committed
docs: normalize code style in examples and tutorial files
1 parent 59e2046 commit 09736cb

File tree

5 files changed

+108
-105
lines changed

5 files changed

+108
-105
lines changed

examples/rendering/src/stores.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export class PostStore {
4949
loading = false;
5050
error: string | null = null;
5151

52+
get count() {
53+
return this.posts.length;
54+
}
55+
5256
async fetchPosts() {
5357
this.loading = true;
5458
this.error = null;
@@ -67,10 +71,6 @@ export class PostStore {
6771
this.posts = [];
6872
this.error = null;
6973
}
70-
71-
get count() {
72-
return this.posts.length;
73-
}
7474
}
7575

7676
export class DocumentStore {

website/docs/PERSIST_TUTORIAL.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
### 1. Create a store
88

99
```ts
10-
import { createClassyStore } from '@codebelt/classy-store';
10+
import {createClassyStore} from '@codebelt/classy-store';
1111

1212
class TodoStore {
1313
todos: { text: string; done: boolean }[] = [];
1414
filter: 'all' | 'active' | 'done' = 'all';
1515

16+
get remaining() {
17+
return this.todos.filter((todo) => !todo.done).length;
18+
}
19+
1620
addTodo(text: string) {
17-
this.todos.push({ text, done: false });
21+
this.todos.push({text, done: false});
1822
}
1923

2024
toggle(index: number) {
2125
this.todos[index]!.done = !this.todos[index]!.done;
2226
}
23-
24-
get remaining() {
25-
return this.todos.filter(t => !t.done).length;
26-
}
2727
}
2828

2929
export const todoStore = createClassyStore(new TodoStore());
@@ -32,7 +32,7 @@ export const todoStore = createClassyStore(new TodoStore());
3232
### 2. Persist it
3333

3434
```ts
35-
import { persist } from '@codebelt/classy-store/utils';
35+
import {persist} from '@codebelt/classy-store/utils';
3636

3737
const handle = persist(todoStore, {
3838
name: 'todo-store',
@@ -125,14 +125,14 @@ On restore, `deserialize` converts the ISO string back into a `Date` object befo
125125
`reactiveMap()` instances are backed by internal arrays that aren't directly JSON-serializable:
126126

127127
```ts
128-
import { createClassyStore, reactiveMap } from '@codebelt/classy-store';
129-
import { persist } from '@codebelt/classy-store/utils';
128+
import {createClassyStore, reactiveMap} from '@codebelt/classy-store';
129+
import {persist} from '@codebelt/classy-store/utils';
130130

131131
class UserStore {
132132
users = reactiveMap<string, { name: string; role: string }>();
133133

134134
addUser(id: string, name: string, role: string) {
135-
this.users.set(id, { name, role });
135+
this.users.set(id, {name, role});
136136
}
137137
}
138138

@@ -212,13 +212,13 @@ persist(todoStore, {
212212
migrate: (state, oldVersion) => {
213213
if (oldVersion === 0) {
214214
// v0 stored todos as `items`
215-
return { ...state, todos: state.items, items: undefined };
215+
return {...state, todos: state.items, items: undefined};
216216
}
217217
if (oldVersion === 1) {
218218
// v1 todos were plain strings, v2 todos are objects
219219
return {
220220
...state,
221-
todos: (state.todos as string[]).map(text => ({ text, done: false })),
221+
todos: (state.todos as string[]).map((text) => ({text, done: false})),
222222
};
223223
}
224224
return state;
@@ -275,13 +275,13 @@ For full control, pass a function that receives `(persistedState, currentState)`
275275
persist(settingsStore, {
276276
name: 'settings',
277277
merge: (persisted, current) => {
278-
const merged = { ...current };
278+
const merged = {...current};
279279
for (const key of Object.keys(persisted)) {
280280
const persistedValue = persisted[key];
281281
const currentValue = current[key];
282282
// Deep merge one level for nested objects
283283
if (persistedValue && currentValue && typeof persistedValue === 'object' && typeof currentValue === 'object') {
284-
merged[key] = { ...currentValue, ...persistedValue };
284+
merged[key] = {...currentValue, ...persistedValue};
285285
} else {
286286
merged[key] = persistedValue;
287287
}
@@ -387,7 +387,7 @@ const handle = persist(todoStore, {
387387
Then hydrate manually in a `useEffect` (which only runs on the client):
388388

389389
```tsx
390-
import { useEffect } from 'react';
390+
import {useEffect} from 'react';
391391

392392
function App() {
393393
useEffect(() => {
@@ -449,7 +449,7 @@ persist(uiStore, {
449449
### Stop persisting
450450

451451
```ts
452-
const handle = persist(todoStore, { name: 'todo-store' });
452+
const handle = persist(todoStore, {name: 'todo-store'});
453453

454454
// Later: stop all persistence (e.g., on logout)
455455
handle.unsubscribe();
@@ -482,7 +482,7 @@ async function resetToDefaults() {
482482
todoStore.filter = 'all';
483483

484484
// Optionally re-enable persistence:
485-
const newHandle = persist(todoStore, { name: 'todo-store' });
485+
const newHandle = persist(todoStore, {name: 'todo-store'});
486486
}
487487
```
488488

@@ -512,8 +512,8 @@ persist(todoStore, {
512512
Here's a complete example combining several features:
513513

514514
```ts
515-
import { createClassyStore, reactiveMap } from '@codebelt/classy-store';
516-
import { persist } from '@codebelt/classy-store/utils';
515+
import {createClassyStore, reactiveMap} from '@codebelt/classy-store';
516+
import {persist} from '@codebelt/classy-store/utils';
517517

518518
class AppStore {
519519
theme: 'light' | 'dark' = 'light';
@@ -530,7 +530,7 @@ class AppStore {
530530
}
531531

532532
addBookmark(id: string, title: string, url: string) {
533-
this.bookmarks.set(id, { title, url });
533+
this.bookmarks.set(id, {title, url});
534534
}
535535
}
536536

@@ -560,7 +560,7 @@ const handle = persist(appStore, {
560560
migrate: (state, oldVersion) => {
561561
if (oldVersion === 0) {
562562
// v0 didn't have language
563-
return { ...state, language: 'en' };
563+
return {...state, language: 'en'};
564564
}
565565
return state;
566566
},
@@ -575,10 +575,10 @@ console.log(`Welcome back! Theme: ${appStore.theme}, Bookmarks: ${appStore.bookm
575575

576576
| What you want | How to do it |
577577
|---|---|
578-
| Persist everything | `persist(myStore, { name: 'key' })` |
578+
| Persist everything | `persist(myStore, {name: 'key'})` |
579579
| Persist specific properties | `properties: ['count', 'name']` |
580-
| Handle Dates | `{ key: 'date', serialize: d => d.toISOString(), deserialize: s => new Date(s) }` |
581-
| Handle ReactiveMap | `{ key: 'map', serialize: m => [...m.entries()], deserialize: s => reactiveMap(s) }` |
580+
| Handle Dates | `{key: 'date', serialize: (date) => date.toISOString(), deserialize: (stored) => new Date(stored)}` |
581+
| Handle ReactiveMap | `{key: 'map', serialize: (map) => [...map.entries()], deserialize: (stored) => reactiveMap(stored)}` |
582582
| Debounce writes | `debounce: 500` |
583583
| Migrate schema changes | `version: 2, migrate: (state, old) => { ... }` |
584584
| SSR support | `skipHydration: true` + `handle.rehydrate()` in `useEffect` |

website/docs/TUTORIAL.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
```ts
1010
// stores.ts
11-
import { createClassyStore } from '@codebelt/classy-store';
11+
import {createClassyStore} from '@codebelt/classy-store';
1212

1313
class Counter {
1414
count = 0;
@@ -25,8 +25,8 @@ export const counterStore = createClassyStore(new Counter());
2525

2626
```tsx
2727
// Counter.tsx
28-
import { useStore } from '@codebelt/classy-store/react';
29-
import { counterStore } from './stores';
28+
import {useStore} from '@codebelt/classy-store/react';
29+
import {counterStore} from './stores';
3030

3131
export function Counter() {
3232
const count = useStore(counterStore, (store) => store.count);
@@ -41,33 +41,33 @@ That's it. No Provider wrapping your app. The store is a module-level singleton
4141
A store is any class instance wrapped with `createClassyStore()`. State lives as properties, mutations are plain assignments, and computed values are `get` accessors.
4242

4343
```ts
44-
import { createClassyStore } from '@codebelt/classy-store';
44+
import {createClassyStore} from '@codebelt/classy-store';
4545

4646
class TodoStore {
4747
items: { id: number; text: string; done: boolean }[] = [];
4848
filter: 'all' | 'active' | 'done' = 'all';
4949

5050
get remaining() {
51-
return this.items.filter((i) => !i.done).length;
51+
return this.items.filter((item) => !item.done).length;
5252
}
5353

5454
get filtered() {
55-
if (this.filter === 'active') return this.items.filter((i) => !i.done);
56-
if (this.filter === 'done') return this.items.filter((i) => i.done);
55+
if (this.filter === 'active') return this.items.filter((item) => !item.done);
56+
if (this.filter === 'done') return this.items.filter((item) => item.done);
5757
return this.items;
5858
}
5959

6060
add(text: string) {
61-
this.items.push({ id: Date.now(), text, done: false });
61+
this.items.push({id: Date.now(), text, done: false});
6262
}
6363

6464
toggle(id: number) {
65-
const item = this.items.find((i) => i.id === id);
65+
const item = this.items.find((todo) => todo.id === id);
6666
if (item) item.done = !item.done;
6767
}
6868

6969
remove(id: number) {
70-
this.items = this.items.filter((i) => i.id !== id);
70+
this.items = this.items.filter((todo) => todo.id !== id);
7171
}
7272
}
7373

@@ -122,22 +122,22 @@ The problem shows up when your selector **derives a new value** — calling `.fi
122122
Without `shallowEqual``.filter()` creates a new array every time the snapshot updates, even if the todo items haven't changed:
123123

124124
```ts
125-
import { useStore } from '@codebelt/classy-store/react';
125+
import {useStore} from '@codebelt/classy-store/react';
126126

127127
// ❌ New array reference every snapshot → unnecessary re-renders
128-
const active = useStore(todoStore, (store) => store.items.filter((i) => !i.done));
128+
const active = useStore(todoStore, (store) => store.items.filter((item) => !item.done));
129129
```
130130

131131
With `shallowEqual` — compares the array contents, not the reference:
132132

133133
```ts
134-
import { shallowEqual } from '@codebelt/classy-store';
135-
import { useStore } from '@codebelt/classy-store/react';
134+
import {shallowEqual} from '@codebelt/classy-store';
135+
import {useStore} from '@codebelt/classy-store/react';
136136

137137
// ✅ Only re-renders when the filtered items actually change
138138
const active = useStore(
139139
todoStore,
140-
(store) => store.items.filter((i) => !i.done),
140+
(store) => store.items.filter((item) => !item.done),
141141
shallowEqual,
142142
);
143143
```
@@ -196,8 +196,8 @@ Mutations separated by an `await` land in different microtasks and trigger separ
196196
`snapshot()` creates a deeply-frozen, immutable copy of the store's current state.
197197

198198
```ts
199-
import { snapshot } from '@codebelt/classy-store';
200-
import { counterStore } from './stores';
199+
import {snapshot} from '@codebelt/classy-store';
200+
import {counterStore} from './stores';
201201

202202
const snap = snapshot(counterStore);
203203
console.log(snap.count); // read-only
@@ -213,8 +213,8 @@ Snapshots use structural sharing: unchanged sub-trees return the same frozen ref
213213
`subscribe()` registers a callback that fires once per batched mutation. It returns an unsubscribe function.
214214

215215
```ts
216-
import { subscribe, snapshot } from '@codebelt/classy-store';
217-
import { counterStore } from './stores';
216+
import {subscribe, snapshot} from '@codebelt/classy-store';
217+
import {counterStore} from './stores';
218218

219219
const unsub = subscribe(counterStore, () => {
220220
const snap = snapshot(counterStore);
@@ -232,10 +232,10 @@ Deep objects and arrays are automatically reactive. You don't need to do anythin
232232

233233
```ts
234234
class DocStore {
235-
metadata = { title: 'Untitled', author: 'Anonymous' };
235+
metadata = {title: 'Untitled', author: 'Anonymous'};
236236
sections = [
237-
{ id: 1, heading: 'Intro', body: 'Hello' },
238-
{ id: 2, heading: 'Methods', body: 'We used proxies.' },
237+
{id: 1, heading: 'Intro', body: 'Hello'},
238+
{id: 2, heading: 'Methods', body: 'We used proxies.'},
239239
];
240240
}
241241

@@ -257,7 +257,7 @@ Structural sharing means that when you mutate `metadata.title`, the snapshot for
257257
Native `Map` and `Set` aren't plain objects — the proxy can't intercept their internal methods. Use `reactiveMap()` and `reactiveSet()` instead.
258258

259259
```ts
260-
import { createClassyStore, reactiveMap } from '@codebelt/classy-store';
260+
import {createClassyStore, reactiveMap} from '@codebelt/classy-store';
261261

262262
class UserStore {
263263
users = reactiveMap<string, { name: string; role: string }>();
@@ -267,7 +267,7 @@ class UserStore {
267267
}
268268

269269
addUser(id: string, name: string) {
270-
this.users.set(id, { name, role: 'viewer' });
270+
this.users.set(id, {name, role: 'viewer'});
271271
}
272272

273273
removeUser(id: string) {
@@ -287,7 +287,7 @@ export const userStore = createClassyStore(new UserStore());
287287
Subclasses work out of the box — no special API or configuration needed. Methods, getters, and properties from all inheritance levels are fully reactive.
288288

289289
```ts
290-
import { createClassyStore } from '@codebelt/classy-store';
290+
import {createClassyStore} from '@codebelt/classy-store';
291291

292292
class BaseStore {
293293
loading = false;
@@ -309,22 +309,22 @@ class BaseStore {
309309
class UserStore extends BaseStore {
310310
users: { id: string; name: string }[] = [];
311311

312+
get activeCount() {
313+
return this.users.length;
314+
}
315+
312316
async fetchUsers() {
313317
this.setLoading(true); // base method — reactive
314318
this.setError(null);
315319
try {
316320
const res = await fetch('/api/users');
317321
this.users = await res.json();
318-
} catch (err) {
319-
this.setError(err instanceof Error ? err.message : 'Failed');
322+
} catch (error) {
323+
this.setError(error instanceof Error ? error.message : 'Failed');
320324
} finally {
321325
this.setLoading(false);
322326
}
323327
}
324-
325-
get activeCount() {
326-
return this.users.length;
327-
}
328328
}
329329

330330
export const userStore = createClassyStore(new UserStore());
@@ -356,8 +356,8 @@ class PostStore {
356356
const res = await fetch('/api/posts');
357357
if (!res.ok) throw new Error(`HTTP ${res.status}`);
358358
this.posts = await res.json(); // no notification yet — batched with finally
359-
} catch (e) {
360-
this.error = e instanceof Error ? e.message : 'Unknown error';
359+
} catch (error) {
360+
this.error = error instanceof Error ? error.message : 'Unknown error';
361361
} finally {
362362
this.loading = false; // notification 2: hides spinner, shows data or error
363363
}
@@ -389,7 +389,7 @@ Destructuring copies the primitive value at that moment and breaks the proxy con
389389

390390
```ts
391391
// ❌ Breaks reactivity — `count` is just the number 0
392-
const { count } = counterStore;
392+
const {count} = counterStore;
393393

394394
// ✅ Always access through the proxy
395395
counterStore.count;
@@ -443,7 +443,7 @@ class Counter {
443443
Calling `snapshot()` multiple times without intervening mutations returns the same frozen object — an O(1) cache hit. There's no cost to calling it liberally.
444444

445445
```ts
446-
import { snapshot } from '@codebelt/classy-store';
446+
import {snapshot} from '@codebelt/classy-store';
447447

448448
const a = snapshot(counterStore);
449449
const b = snapshot(counterStore);
@@ -460,7 +460,7 @@ a === c; // false — new snapshot after mutation
460460
`persist()` saves your store's state to `localStorage` (or any storage adapter) and restores it on page load. It's a standalone utility function — import it from `@codebelt/classy-store/utils`.
461461

462462
```ts
463-
import { persist } from '@codebelt/classy-store/utils';
463+
import {persist} from '@codebelt/classy-store/utils';
464464

465465
const handle = persist(todoStore, {
466466
name: 'todo-store',

0 commit comments

Comments
 (0)