Skip to content

Commit 5fb91d3

Browse files
authored
Opus 4.7 improvments (#25)
1 parent 2cfaf26 commit 5fb91d3

39 files changed

Lines changed: 1163 additions & 432 deletions

.changeset/spotty-impalas-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@codebelt/classy-store": patch
3+
---
4+
5+
core improvements and tests

examples/angular/src/store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const counterStore = createClassyStore(new CounterStore());
3636

3737
export const persistHandle = persist(counterStore, {
3838
name: 'classy-angular-example',
39+
storage: localStorage,
3940
properties: ['count', 'step', 'label'],
4041
});
4142

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* SSR-safe storage adapter. Uses real `localStorage` in the browser; falls
3+
* back to a per-process in-memory map on the server so module-init
4+
* `persist()` calls never throw.
5+
*/
6+
const memory = new Map<string, string>();
7+
8+
export const ssrSafeLocalStorage = {
9+
getItem(key: string): string | null {
10+
if (typeof localStorage !== 'undefined') return localStorage.getItem(key);
11+
return memory.get(key) ?? null;
12+
},
13+
setItem(key: string, value: string): void {
14+
if (typeof localStorage !== 'undefined') {
15+
localStorage.setItem(key, value);
16+
return;
17+
}
18+
memory.set(key, value);
19+
},
20+
removeItem(key: string): void {
21+
if (typeof localStorage !== 'undefined') {
22+
localStorage.removeItem(key);
23+
return;
24+
}
25+
memory.delete(key);
26+
},
27+
};

examples/nextjs/src/stores/planner-store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {createClassyStore} from '@codebelt/classy-store';
22
import {devtools, persist} from '@codebelt/classy-store/utils';
3+
import {ssrSafeLocalStorage} from './_storage';
34

45
export type MealSlot = {
56
recipeId: string | null;
@@ -82,6 +83,7 @@ export const plannerStore = createClassyStore(new PlannerStore());
8283

8384
export const plannerPersist = persist(plannerStore, {
8485
name: 'classy-meal-planner',
86+
storage: ssrSafeLocalStorage,
8587
properties: ['days'],
8688
debounce: 500,
8789
version: 2,

examples/nextjs/src/stores/settings-store.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {createClassyStore} from '@codebelt/classy-store';
22
import {devtools, persist} from '@codebelt/classy-store/utils';
3+
import {ssrSafeLocalStorage} from './_storage';
34

45
class SettingsStore {
56
theme = 'system' as 'light' | 'dark' | 'system';
@@ -14,8 +15,8 @@ export const settingsStore = createClassyStore(new SettingsStore());
1415

1516
export const settingsPersist = persist(settingsStore, {
1617
name: 'classy-kitchen-settings',
18+
storage: ssrSafeLocalStorage,
1719
debounce: 300,
18-
merge: 'replace',
1920
});
2021

2122
devtools(settingsStore, {name: 'Settings Store'});

examples/nextjs/src/stores/shopping-store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {createClassyStore} from '@codebelt/classy-store';
22
import type {PropertyTransform} from '@codebelt/classy-store/utils';
33
import {devtools, persist, subscribeKey} from '@codebelt/classy-store/utils';
4+
import {ssrSafeLocalStorage} from './_storage';
45

56
export type ShoppingItem = {
67
id: string;
@@ -84,6 +85,7 @@ const itemsTransform: PropertyTransform<ShoppingStore> = {
8485

8586
export const shoppingPersist = persist(shoppingStore, {
8687
name: 'classy-shopping-list',
88+
storage: ssrSafeLocalStorage,
8789
properties: [itemsTransform, 'lastAction'],
8890
merge: (persisted, current) => ({
8991
...current,

examples/react/src/demos/persist/KitchenSinkPersistDemo.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const STORE_CODE = `class KitchenSinkStore {
2020
2121
persist(store, {
2222
name: 'kitchen-sink',
23+
storage: localStorage,
2324
debounce: 300,
2425
version: 1,
2526
merge: 'shallow',

examples/react/src/demos/persist/SimplePersistDemo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const STORE_CODE = `class PreferencesStore {
3232
}
3333
3434
const store = createClassyStore(new PreferencesStore());
35-
persist(store, { name: 'preferences' });`;
35+
persist(store, { name: 'preferences', storage: localStorage });`;
3636

3737
function ThemeToggle() {
3838
const theme = useClassyStore(preferencesStore, (state) => state.theme);
@@ -183,7 +183,7 @@ export function SimplePersistDemo() {
183183
return (
184184
<DemoContainer
185185
title="Simple Persist"
186-
description="persist(store, { name: 'preferences' }) — that's it. All properties are saved to localStorage automatically."
186+
description="persist(store, { name: 'preferences', storage: localStorage }) — pass any storage adapter; properties are saved on every batched mutation."
187187
codeTabs={[{label: 'Store', code: STORE_CODE, language: 'typescript'}]}
188188
>
189189
<div className="flex flex-col gap-4">

examples/react/src/pages/PersistPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export function PersistPage() {
2323
<SimplePersistDemo />
2424
<KitchenSinkPersistDemo />
2525
<TipBox>
26-
The simplest setup is just{' '}
26+
The simplest setup is{' '}
2727
<code className="text-xs bg-zinc-800 px-1 rounded">
28-
persist(store, {'{'} name: 'key' {'}'})
28+
persist(store, {'{'} name: 'key', storage: localStorage {'}'})
2929
</code>
3030
. Add{' '}
3131
<code className="text-xs bg-zinc-800 px-1 rounded">properties</code>{' '}

examples/react/src/stores/persistStores.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const preferencesStore = createClassyStore(new PreferencesStore());
4848

4949
export const preferencesHandle = persist(preferencesStore, {
5050
name: 'preferences',
51+
storage: localStorage,
5152
});
5253

5354
// ── Kitchen Sink Store ──────────────────────────────────────────────────────
@@ -150,6 +151,7 @@ export const kitchenSinkStore = createClassyStore(new KitchenSinkStore());
150151

151152
export const kitchenSinkHandle = persist(kitchenSinkStore, {
152153
name: 'kitchen-sink',
154+
storage: localStorage,
153155
debounce: 300,
154156
version: 1,
155157
merge: 'shallow',

0 commit comments

Comments
 (0)