@effuse/store 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,147 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { addSubscriber, addKeySubscriber } from './subscriptions.js';
3
+ import type { StoreInternals } from './types.js';
4
+ import { createCancellationScope } from '../actions/cancellation.js';
5
+
6
+ const createMockInternals = (): StoreInternals => ({
7
+ signalMap: new Map(),
8
+ initialState: {},
9
+ actions: {},
10
+ subscribers: new Set(),
11
+ keySubscribers: new Map(),
12
+ computedSelectors: new Map(),
13
+ isBatching: false,
14
+ cancellationScope: createCancellationScope(),
15
+ pendingActions: new Map(),
16
+ });
17
+
18
+ describe('subscriptions handlers', () => {
19
+ describe('addSubscriber', () => {
20
+ it('should add subscriber to the set', () => {
21
+ const internals = createMockInternals();
22
+ const callback = vi.fn();
23
+ addSubscriber(internals, { callback });
24
+ expect(internals.subscribers.has(callback)).toBe(true);
25
+ });
26
+
27
+ it('should return unsubscribe function', () => {
28
+ const internals = createMockInternals();
29
+ const callback = vi.fn();
30
+ const unsubscribe = addSubscriber(internals, { callback });
31
+ expect(typeof unsubscribe).toBe('function');
32
+ });
33
+
34
+ it('should remove subscriber when unsubscribe is called', () => {
35
+ const internals = createMockInternals();
36
+ const callback = vi.fn();
37
+ const unsubscribe = addSubscriber(internals, { callback });
38
+ unsubscribe();
39
+ expect(internals.subscribers.has(callback)).toBe(false);
40
+ });
41
+
42
+ it('should handle multiple subscribers', () => {
43
+ const internals = createMockInternals();
44
+ const cb1 = vi.fn();
45
+ const cb2 = vi.fn();
46
+ const cb3 = vi.fn();
47
+ addSubscriber(internals, { callback: cb1 });
48
+ addSubscriber(internals, { callback: cb2 });
49
+ addSubscriber(internals, { callback: cb3 });
50
+ expect(internals.subscribers.size).toBe(3);
51
+ });
52
+
53
+ it('should handle unsubscribing multiple times without error', () => {
54
+ const internals = createMockInternals();
55
+ const callback = vi.fn();
56
+ const unsubscribe = addSubscriber(internals, { callback });
57
+ unsubscribe();
58
+ unsubscribe();
59
+ unsubscribe();
60
+ expect(internals.subscribers.size).toBe(0);
61
+ });
62
+
63
+ it('should not affect other subscribers when one unsubscribes', () => {
64
+ const internals = createMockInternals();
65
+ const cb1 = vi.fn();
66
+ const cb2 = vi.fn();
67
+ const unsub1 = addSubscriber(internals, { callback: cb1 });
68
+ addSubscriber(internals, { callback: cb2 });
69
+ unsub1();
70
+ expect(internals.subscribers.has(cb1)).toBe(false);
71
+ expect(internals.subscribers.has(cb2)).toBe(true);
72
+ });
73
+ });
74
+
75
+ describe('addKeySubscriber', () => {
76
+ it('should add key subscriber for new key', () => {
77
+ const internals = createMockInternals();
78
+ const callback = vi.fn();
79
+ addKeySubscriber(internals, { key: 'count', callback });
80
+ const subs = internals.keySubscribers.get('count');
81
+ expect(subs?.has(callback)).toBe(true);
82
+ });
83
+
84
+ it('should create subscriber set for new key', () => {
85
+ const internals = createMockInternals();
86
+ const callback = vi.fn();
87
+ addKeySubscriber(internals, { key: 'newKey', callback });
88
+ expect(internals.keySubscribers.has('newKey')).toBe(true);
89
+ });
90
+
91
+ it('should add to existing subscriber set', () => {
92
+ const internals = createMockInternals();
93
+ const cb1 = vi.fn();
94
+ const cb2 = vi.fn();
95
+ addKeySubscriber(internals, { key: 'count', callback: cb1 });
96
+ addKeySubscriber(internals, { key: 'count', callback: cb2 });
97
+ const subs = internals.keySubscribers.get('count');
98
+ expect(subs?.size).toBe(2);
99
+ });
100
+
101
+ it('should return unsubscribe function', () => {
102
+ const internals = createMockInternals();
103
+ const callback = vi.fn();
104
+ const unsubscribe = addKeySubscriber(internals, {
105
+ key: 'count',
106
+ callback,
107
+ });
108
+ expect(typeof unsubscribe).toBe('function');
109
+ });
110
+
111
+ it('should remove subscriber when unsubscribe is called', () => {
112
+ const internals = createMockInternals();
113
+ const callback = vi.fn();
114
+ const unsubscribe = addKeySubscriber(internals, {
115
+ key: 'count',
116
+ callback,
117
+ });
118
+ unsubscribe();
119
+ const subs = internals.keySubscribers.get('count');
120
+ expect(subs?.has(callback)).toBe(false);
121
+ });
122
+
123
+ it('should handle different keys independently', () => {
124
+ const internals = createMockInternals();
125
+ const cb1 = vi.fn();
126
+ const cb2 = vi.fn();
127
+ addKeySubscriber(internals, { key: 'a', callback: cb1 });
128
+ addKeySubscriber(internals, { key: 'b', callback: cb2 });
129
+ expect(internals.keySubscribers.get('a')?.size).toBe(1);
130
+ expect(internals.keySubscribers.get('b')?.size).toBe(1);
131
+ });
132
+
133
+ it('should handle empty string key', () => {
134
+ const internals = createMockInternals();
135
+ const callback = vi.fn();
136
+ addKeySubscriber(internals, { key: '', callback });
137
+ expect(internals.keySubscribers.has('')).toBe(true);
138
+ });
139
+
140
+ it('should handle special characters in key', () => {
141
+ const internals = createMockInternals();
142
+ const callback = vi.fn();
143
+ addKeySubscriber(internals, { key: 'user.profile.name', callback });
144
+ expect(internals.keySubscribers.has('user.profile.name')).toBe(true);
145
+ });
146
+ });
147
+ });
@@ -0,0 +1,58 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2025 Chris M. Perez
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { Predicate } from 'effect';
26
+ import type {
27
+ StoreInternals,
28
+ SubscribeInput,
29
+ SubscribeKeyInput,
30
+ } from './types.js';
31
+
32
+ export const addSubscriber = (
33
+ internals: StoreInternals,
34
+ input: SubscribeInput
35
+ ): (() => void) => {
36
+ internals.subscribers.add(input.callback);
37
+ return () => {
38
+ internals.subscribers.delete(input.callback);
39
+ };
40
+ };
41
+
42
+ export const addKeySubscriber = (
43
+ internals: StoreInternals,
44
+ input: SubscribeKeyInput
45
+ ): (() => void) => {
46
+ let subs = internals.keySubscribers.get(input.key);
47
+ if (!subs) {
48
+ subs = new Set();
49
+ internals.keySubscribers.set(input.key, subs);
50
+ }
51
+ subs.add(input.callback);
52
+ return () => {
53
+ const subsSet = internals.keySubscribers.get(input.key);
54
+ if (Predicate.isNotNullable(subsSet)) {
55
+ subsSet.delete(input.callback);
56
+ }
57
+ };
58
+ };
@@ -0,0 +1,80 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2025 Chris M. Perez
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { Signal } from '@effuse/core';
26
+ import type { AtomicState } from '../core/state.js';
27
+ import type { MiddlewareManager } from '../middleware/index.js';
28
+ import type { StorageAdapter } from '../persistence/index.js';
29
+ import type {
30
+ CancellationScope,
31
+ CancellationToken,
32
+ } from '../actions/cancellation.js';
33
+
34
+ export interface StoreInternals {
35
+ signalMap: Map<string, Signal<unknown>>;
36
+ initialState: Record<string, unknown>;
37
+ actions: Record<string, (...args: unknown[]) => unknown>;
38
+ subscribers: Set<() => void>;
39
+ keySubscribers: Map<string, Set<(value: unknown) => void>>;
40
+ computedSelectors: Map<
41
+ (s: Record<string, unknown>) => unknown,
42
+ Signal<unknown>
43
+ >;
44
+ isBatching: boolean;
45
+ cancellationScope: CancellationScope;
46
+ pendingActions: Map<string, CancellationToken>;
47
+ }
48
+
49
+ export interface StoreConfig {
50
+ name: string;
51
+ shouldPersist: boolean;
52
+ storageKey: string;
53
+ enableDevtools: boolean;
54
+ adapter: StorageAdapter;
55
+ }
56
+
57
+ export interface StoreHandlerDeps {
58
+ internals: StoreInternals;
59
+ atomicState: AtomicState<Record<string, unknown>>;
60
+ middlewareManager: MiddlewareManager<Record<string, unknown>>;
61
+ config: StoreConfig;
62
+ }
63
+
64
+ export interface SetValueInput {
65
+ prop: string;
66
+ value: unknown;
67
+ }
68
+
69
+ export interface UpdateStateInput {
70
+ updater: (draft: Record<string, unknown>) => void;
71
+ }
72
+
73
+ export interface SubscribeInput {
74
+ callback: () => void;
75
+ }
76
+
77
+ export interface SubscribeKeyInput {
78
+ key: string;
79
+ callback: (value: unknown) => void;
80
+ }
@@ -23,65 +23,95 @@
23
23
  */
24
24
 
25
25
  import { Effect, Option } from 'effect';
26
+ import {
27
+ getItem,
28
+ setItem,
29
+ removeItem,
30
+ hasItem,
31
+ clearStorage,
32
+ getStorageKeys,
33
+ getStorageSize,
34
+ type StorageHandlerDeps,
35
+ } from '../handlers/index.js';
26
36
 
27
- // Storage engine interface
28
37
  export interface StorageAdapter {
29
38
  getItem: (key: string) => Effect.Effect<Option.Option<string>>;
30
39
  setItem: (key: string, value: string) => Effect.Effect<void>;
31
40
  removeItem: (key: string) => Effect.Effect<void>;
41
+ has: (key: string) => Effect.Effect<boolean>;
42
+ clear: () => Effect.Effect<void>;
43
+ keys: () => Effect.Effect<string[]>;
44
+ size: () => Effect.Effect<number>;
32
45
  }
33
46
 
34
- // Browser local storage adapter
35
- export const localStorageAdapter: StorageAdapter = {
47
+ const createBrowserStorageAdapter = (storage: Storage): StorageAdapter => ({
36
48
  getItem: (key) =>
37
49
  Effect.try({
38
- try: () => Option.fromNullable(localStorage.getItem(key)),
50
+ try: () => Option.fromNullable(storage.getItem(key)),
39
51
  catch: () => Option.none<string>(),
40
52
  }).pipe(Effect.catchAll(() => Effect.succeed(Option.none<string>()))),
41
53
  setItem: (key, value) =>
42
54
  Effect.try(() => {
43
- localStorage.setItem(key, value);
55
+ storage.setItem(key, value);
44
56
  }).pipe(Effect.catchAll(() => Effect.void)),
45
57
  removeItem: (key) =>
46
58
  Effect.try(() => {
47
- localStorage.removeItem(key);
59
+ storage.removeItem(key);
48
60
  }).pipe(Effect.catchAll(() => Effect.void)),
49
- };
50
-
51
- // Browser session storage adapter
52
- export const sessionStorageAdapter: StorageAdapter = {
53
- getItem: (key) =>
61
+ has: (key) =>
54
62
  Effect.try({
55
- try: () => Option.fromNullable(sessionStorage.getItem(key)),
56
- catch: () => Option.none<string>(),
57
- }).pipe(Effect.catchAll(() => Effect.succeed(Option.none<string>()))),
58
- setItem: (key, value) =>
59
- Effect.try(() => {
60
- sessionStorage.setItem(key, value);
61
- }).pipe(Effect.catchAll(() => Effect.void)),
62
- removeItem: (key) =>
63
+ try: () => storage.getItem(key) !== null,
64
+ catch: () => false,
65
+ }).pipe(Effect.catchAll(() => Effect.succeed(false))),
66
+ clear: () =>
63
67
  Effect.try(() => {
64
- sessionStorage.removeItem(key);
68
+ storage.clear();
65
69
  }).pipe(Effect.catchAll(() => Effect.void)),
66
- };
70
+ keys: () =>
71
+ Effect.try({
72
+ try: () => Object.keys(storage),
73
+ catch: () => [] as string[],
74
+ }).pipe(Effect.catchAll(() => Effect.succeed([] as string[]))),
75
+ size: () =>
76
+ Effect.try({
77
+ try: () => storage.length,
78
+ catch: () => 0,
79
+ }).pipe(Effect.catchAll(() => Effect.succeed(0))),
80
+ });
81
+
82
+ export const localStorageAdapter: StorageAdapter = createBrowserStorageAdapter(
83
+ typeof localStorage !== 'undefined' ? localStorage : ({} as Storage)
84
+ );
85
+
86
+ export const sessionStorageAdapter: StorageAdapter =
87
+ createBrowserStorageAdapter(
88
+ typeof sessionStorage !== 'undefined' ? sessionStorage : ({} as Storage)
89
+ );
67
90
 
68
- // Build in memory storage adapter
69
91
  export const createMemoryAdapter = (): StorageAdapter => {
70
92
  const storage = new Map<string, string>();
93
+ const deps: StorageHandlerDeps = { storage };
94
+
71
95
  return {
72
- getItem: (key) => Effect.succeed(Option.fromNullable(storage.get(key))),
96
+ getItem: (key) => Effect.succeed(getItem(deps, { key })),
73
97
  setItem: (key, value) =>
74
98
  Effect.sync(() => {
75
- storage.set(key, value);
99
+ setItem(deps, { key, value });
76
100
  }),
77
101
  removeItem: (key) =>
78
102
  Effect.sync(() => {
79
- storage.delete(key);
103
+ removeItem(deps, { key });
104
+ }),
105
+ has: (key) => Effect.succeed(hasItem(deps, { key })),
106
+ clear: () =>
107
+ Effect.sync(() => {
108
+ clearStorage(deps);
80
109
  }),
110
+ keys: () => Effect.succeed(getStorageKeys(deps)),
111
+ size: () => Effect.succeed(getStorageSize(deps)),
81
112
  };
82
113
  };
83
114
 
84
- // Synchronous storage adapter bridge
85
115
  export const runAdapter = {
86
116
  getItem: (adapter: StorageAdapter, key: string): string | null =>
87
117
  Effect.runSync(
@@ -93,4 +123,11 @@ export const runAdapter = {
93
123
  removeItem: (adapter: StorageAdapter, key: string): void => {
94
124
  Effect.runSync(adapter.removeItem(key));
95
125
  },
126
+ has: (adapter: StorageAdapter, key: string): boolean =>
127
+ Effect.runSync(adapter.has(key)),
128
+ clear: (adapter: StorageAdapter): void => {
129
+ Effect.runSync(adapter.clear());
130
+ },
131
+ keys: (adapter: StorageAdapter): string[] => Effect.runSync(adapter.keys()),
132
+ size: (adapter: StorageAdapter): number => Effect.runSync(adapter.size()),
96
133
  };
@@ -28,7 +28,6 @@ import {
28
28
  type CancellationToken,
29
29
  } from '../actions/cancellation.js';
30
30
 
31
- // Store state change stream
32
31
  export interface StoreStream<T> {
33
32
  subscribe: (handler: (value: T) => void) => () => void;
34
33
  map: <R>(fn: (value: T) => R) => StoreStream<R>;
@@ -145,7 +144,6 @@ const createBaseStream = <T>(
145
144
  };
146
145
  };
147
146
 
148
- // Build store property stream
149
147
  export const createStoreStream = <T, K extends keyof T>(
150
148
  store: Store<T>,
151
149
  key: K
@@ -172,7 +170,6 @@ export const createStoreStream = <T, K extends keyof T>(
172
170
  });
173
171
  };
174
172
 
175
- // Observe entire store stream
176
173
  export const streamAll = <T>(
177
174
  store: Store<T>
178
175
  ): StoreStream<ReturnType<Store<T>['getSnapshot']>> => {
@@ -21,22 +21,20 @@
21
21
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
  * SOFTWARE.
23
23
  */
24
+
24
25
  import type { Store } from '../core/types.js';
25
26
  import { getStoreConfig } from '../config/index.js';
26
27
  import { StoreNotFoundError } from '../errors.js';
27
28
 
28
29
  const stores = new Map<string, Store<unknown>>();
29
30
 
30
- // Register store instance
31
31
  export const registerStore = <T>(name: string, store: Store<T>): void => {
32
32
  if (stores.has(name) && getStoreConfig().debug) {
33
- // eslint-disable-next-line no-console
34
33
  console.warn(`[store] Overwriting existing store: ${name}`);
35
34
  }
36
35
  stores.set(name, store as Store<unknown>);
37
36
  };
38
37
 
39
- // Access registered store
40
38
  export const getStore = <T>(name: string): Store<T> => {
41
39
  const store = stores.get(name);
42
40
  if (!store) {
@@ -45,16 +43,12 @@ export const getStore = <T>(name: string): Store<T> => {
45
43
  return store as Store<T>;
46
44
  };
47
45
 
48
- // Detect registered store
49
46
  export const hasStore = (name: string): boolean => stores.has(name);
50
47
 
51
- // Remove registered store
52
48
  export const removeStore = (name: string): boolean => stores.delete(name);
53
49
 
54
- // Reset store registry
55
50
  export const clearStores = (): void => {
56
51
  stores.clear();
57
52
  };
58
53
 
59
- // Access store names
60
54
  export const getStoreNames = (): string[] => Array.from(stores.keys());
@@ -26,17 +26,14 @@ import { Effect, Schema, Duration, Predicate } from 'effect';
26
26
  import { TimeoutError } from '../errors.js';
27
27
  import { DEFAULT_TIMEOUT_MS } from '../config/constants.js';
28
28
 
29
- // State validation schema type
30
29
  export type StateSchema<T> = Schema.Schema<T, T>;
31
30
 
32
- // Validation check result
33
31
  export interface ValidationResult<T> {
34
32
  success: boolean;
35
33
  data: T | null;
36
34
  errors: string[];
37
35
  }
38
36
 
39
- // Validate state synchronously
40
37
  export const validateState = <T>(
41
38
  schema: StateSchema<T>,
42
39
  state: unknown
@@ -60,7 +57,6 @@ export const validateState = <T>(
60
57
  return result;
61
58
  };
62
59
 
63
- // Validate state asynchronously
64
60
  export const validateStateAsync = <T>(
65
61
  schema: StateSchema<T>,
66
62
  state: unknown,
@@ -92,7 +88,6 @@ export const validateStateAsync = <T>(
92
88
  );
93
89
  };
94
90
 
95
- // Build validated state setter
96
91
  export const createValidatedSetter = <T extends Record<string, unknown>>(
97
92
  schema: StateSchema<T>,
98
93
  onValid: (state: T) => void,
@@ -111,7 +106,6 @@ export const createValidatedSetter = <T extends Record<string, unknown>>(
111
106
  };
112
107
  };
113
108
 
114
- // Build field validator
115
109
  export const createFieldValidator = <T>(
116
110
  schema: Schema.Schema<T, T>
117
111
  ): ((value: unknown) => T) => {
@@ -120,7 +114,6 @@ export const createFieldValidator = <T>(
120
114
  };
121
115
  };
122
116
 
123
- // Build safe field setter
124
117
  export const createSafeFieldSetter = <T>(
125
118
  schema: Schema.Schema<T, T>,
126
119
  setter: (value: T) => void