@montra-interactive/deepstate 0.1.1 → 0.2.0
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.
- package/dist/deepstate.d.ts +39 -0
- package/dist/deepstate.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -3
- package/package.json +1 -1
- package/src/deepstate.ts +99 -3
- package/src/index.ts +2 -1
package/dist/deepstate.d.ts
CHANGED
|
@@ -191,5 +191,44 @@ export declare function state<T extends object>(initialState: T, options?: State
|
|
|
191
191
|
* store.user?.name.set("Charlie"); // After ?. on user, children are directly accessible
|
|
192
192
|
*/
|
|
193
193
|
export declare function nullable<T extends object>(value: T | null): T | null;
|
|
194
|
+
/** Comparison mode for array distinct checking */
|
|
195
|
+
export type ArrayDistinct<T> = false | 'shallow' | 'deep' | ((a: T[], b: T[]) => boolean);
|
|
196
|
+
/** Options for the array() helper */
|
|
197
|
+
export interface ArrayOptions<T> {
|
|
198
|
+
/**
|
|
199
|
+
* How to compare arrays to prevent duplicate emissions.
|
|
200
|
+
* - false: No deduplication (default, always emits on set)
|
|
201
|
+
* - 'shallow': Reference equality per element (a[i] === b[i])
|
|
202
|
+
* - 'deep': JSON.stringify comparison (expensive for large arrays)
|
|
203
|
+
* - function: Custom comparator (a, b) => boolean
|
|
204
|
+
*/
|
|
205
|
+
distinct?: ArrayDistinct<T>;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Marks an array with options for how it should behave in state.
|
|
209
|
+
* Use this to enable deduplication (prevent emissions when setting same values).
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* const store = state({
|
|
214
|
+
* // Default behavior - emits on every set
|
|
215
|
+
* items: [1, 2, 3],
|
|
216
|
+
*
|
|
217
|
+
* // Shallow comparison - only emits if elements differ by reference
|
|
218
|
+
* tags: array(['a', 'b'], { distinct: 'shallow' }),
|
|
219
|
+
*
|
|
220
|
+
* // Deep comparison - only emits if JSON representation differs
|
|
221
|
+
* settings: array([{ theme: 'dark' }], { distinct: 'deep' }),
|
|
222
|
+
*
|
|
223
|
+
* // Custom comparator - you define equality
|
|
224
|
+
* users: array([{ id: 1, name: 'Alice' }], {
|
|
225
|
+
* distinct: (a, b) =>
|
|
226
|
+
* a.length === b.length &&
|
|
227
|
+
* a.every((user, i) => user.id === b[i].id)
|
|
228
|
+
* }),
|
|
229
|
+
* });
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export declare function array<T>(value: T[], options?: ArrayOptions<T>): T[];
|
|
194
233
|
export {};
|
|
195
234
|
//# sourceMappingURL=deepstate.d.ts.map
|
package/dist/deepstate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deepstate.d.ts","sourceRoot":"","sources":["../src/deepstate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAmB,UAAU,EAAqB,YAAY,EAAE,MAAM,MAAM,CAAC;AAapF,eAAO,IAAI,iBAAiB,QAAI,CAAC;AACjC,wBAAgB,sBAAsB,SAErC;AAyED,KAAK,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;AAGhF,KAAK,eAAe,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,GAAG,SAAS,GAAG,KAAK,GAAG,CAAC,CAAC;AAGjE,KAAK,OAAO,CAAC,CAAC,IAAI,IAAI,SAAS,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAChD,KAAK,YAAY,CAAC,CAAC,IAAI,SAAS,SAAS,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAC1D,KAAK,SAAS,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;AAGrE,KAAK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F;AAMD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,IAAI,CAMpE;AAUD,kDAAkD;AAClD,MAAM,MAAM,aAAa,CAAC,CAAC,IACvB,KAAK,GACL,SAAS,GACT,MAAM,GACN,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,OAAO,CAAC,CAAC;AAElC,qCAAqC;AACrC,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC;CAC7B;AAMD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,OAAO,GAAE,YAAY,CAAC,CAAC,CAAM,GAAG,CAAC,EAAE,CAIvE"}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
* Core exports:
|
|
5
5
|
* - state() - Create reactive state from plain objects
|
|
6
6
|
* - nullable() - Mark a property as nullable (can transition between null and object)
|
|
7
|
+
* - array() - Mark an array with deduplication options
|
|
7
8
|
* - RxState, Draft - Type exports
|
|
8
9
|
*
|
|
9
10
|
* Helper exports:
|
|
10
11
|
* - select() - Combine multiple observables
|
|
11
12
|
* - selectFromEach() - Select from each array item with precise change detection
|
|
12
13
|
*/
|
|
13
|
-
export { state, nullable, type RxState, type Draft, type StateOptions } from "./deepstate";
|
|
14
|
+
export { state, nullable, array, type RxState, type Draft, type StateOptions, type ArrayOptions, type ArrayDistinct } from "./deepstate";
|
|
14
15
|
export { select, selectFromEach } from "./helpers";
|
|
15
16
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,OAAO,EAAE,KAAK,KAAK,EAAE,KAAK,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AACzI,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -164,7 +164,7 @@ function createObjectNode(value) {
|
|
|
164
164
|
}
|
|
165
165
|
};
|
|
166
166
|
}
|
|
167
|
-
function createArrayNode(value) {
|
|
167
|
+
function createArrayNode(value, comparator) {
|
|
168
168
|
const subject$ = new BehaviorSubject([...value]);
|
|
169
169
|
const childCache = new Map;
|
|
170
170
|
const createChildProjection = (index) => {
|
|
@@ -188,7 +188,8 @@ function createArrayNode(value) {
|
|
|
188
188
|
};
|
|
189
189
|
};
|
|
190
190
|
const lock$ = new BehaviorSubject(true);
|
|
191
|
-
const
|
|
191
|
+
const baseLocked$ = combineLatest2([subject$, lock$]).pipe(filter(([_, unlocked]) => unlocked), map2(([arr, _]) => arr));
|
|
192
|
+
const locked$ = (comparator ? baseLocked$.pipe(distinctUntilChanged2(comparator)) : baseLocked$).pipe(map2(deepFreeze), shareReplay(1));
|
|
192
193
|
locked$.subscribe();
|
|
193
194
|
const length$ = locked$.pipe(map2((arr) => arr.length), distinctUntilChanged2(), shareReplay(1));
|
|
194
195
|
length$.subscribe();
|
|
@@ -562,6 +563,12 @@ function createNodeForValue(value, maybeNullable = false) {
|
|
|
562
563
|
return createLeafNode(value);
|
|
563
564
|
}
|
|
564
565
|
if (Array.isArray(value)) {
|
|
566
|
+
if (isArrayMarked(value)) {
|
|
567
|
+
const options = value[ARRAY_MARKER];
|
|
568
|
+
const comparator = getArrayComparator(options);
|
|
569
|
+
delete value[ARRAY_MARKER];
|
|
570
|
+
return createArrayNode(value, comparator);
|
|
571
|
+
}
|
|
565
572
|
return createArrayNode(value);
|
|
566
573
|
}
|
|
567
574
|
return createObjectNode(value);
|
|
@@ -782,9 +789,30 @@ function nullable(value) {
|
|
|
782
789
|
function isNullableMarked(value) {
|
|
783
790
|
return value !== null && typeof value === "object" && NULLABLE_MARKER in value;
|
|
784
791
|
}
|
|
792
|
+
var ARRAY_MARKER = Symbol("array");
|
|
793
|
+
function array(value, options = {}) {
|
|
794
|
+
const marked = [...value];
|
|
795
|
+
marked[ARRAY_MARKER] = options;
|
|
796
|
+
return marked;
|
|
797
|
+
}
|
|
798
|
+
function isArrayMarked(value) {
|
|
799
|
+
return Array.isArray(value) && ARRAY_MARKER in value;
|
|
800
|
+
}
|
|
801
|
+
function getArrayComparator(options) {
|
|
802
|
+
if (!options.distinct)
|
|
803
|
+
return;
|
|
804
|
+
if (options.distinct === "shallow") {
|
|
805
|
+
return (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
|
806
|
+
}
|
|
807
|
+
if (options.distinct === "deep") {
|
|
808
|
+
return (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
809
|
+
}
|
|
810
|
+
return options.distinct;
|
|
811
|
+
}
|
|
785
812
|
export {
|
|
786
813
|
state,
|
|
787
814
|
selectFromEach,
|
|
788
815
|
select,
|
|
789
|
-
nullable
|
|
816
|
+
nullable,
|
|
817
|
+
array
|
|
790
818
|
};
|
package/package.json
CHANGED
package/src/deepstate.ts
CHANGED
|
@@ -424,7 +424,10 @@ function createObjectNode<T extends object>(value: T): NodeCore<T> & {
|
|
|
424
424
|
};
|
|
425
425
|
}
|
|
426
426
|
|
|
427
|
-
function createArrayNode<T>(
|
|
427
|
+
function createArrayNode<T>(
|
|
428
|
+
value: T[],
|
|
429
|
+
comparator?: (a: T[], b: T[]) => boolean
|
|
430
|
+
): NodeCore<T[]> & {
|
|
428
431
|
at(index: number): NodeCore<T> | undefined;
|
|
429
432
|
childCache: Map<number, NodeCore<T>>;
|
|
430
433
|
length$: Observable<number> & { get(): number };
|
|
@@ -478,10 +481,17 @@ function createArrayNode<T>(value: T[]): NodeCore<T[]> & {
|
|
|
478
481
|
// Lock for batching updates - when false, emissions are filtered out
|
|
479
482
|
const lock$ = new BehaviorSubject<boolean>(true);
|
|
480
483
|
|
|
481
|
-
// Create observable that respects lock
|
|
482
|
-
const
|
|
484
|
+
// Create observable that respects lock, with optional distinct comparison
|
|
485
|
+
const baseLocked$ = combineLatest([subject$, lock$]).pipe(
|
|
483
486
|
filter(([_, unlocked]) => unlocked),
|
|
484
487
|
map(([arr, _]) => arr),
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
// Apply distinct comparison if provided
|
|
491
|
+
const locked$ = (comparator
|
|
492
|
+
? baseLocked$.pipe(distinctUntilChanged(comparator))
|
|
493
|
+
: baseLocked$
|
|
494
|
+
).pipe(
|
|
485
495
|
map(deepFreeze),
|
|
486
496
|
shareReplay(1)
|
|
487
497
|
);
|
|
@@ -1079,6 +1089,14 @@ function createNodeForValue<T>(value: T, maybeNullable: boolean = false): NodeCo
|
|
|
1079
1089
|
return createLeafNode(value as Primitive) as NodeCore<T>;
|
|
1080
1090
|
}
|
|
1081
1091
|
if (Array.isArray(value)) {
|
|
1092
|
+
// Check if array was marked with options via array() helper
|
|
1093
|
+
if (isArrayMarked(value)) {
|
|
1094
|
+
const options = value[ARRAY_MARKER];
|
|
1095
|
+
const comparator = getArrayComparator(options);
|
|
1096
|
+
// Remove the marker before creating the node
|
|
1097
|
+
delete (value as Record<symbol, unknown>)[ARRAY_MARKER];
|
|
1098
|
+
return createArrayNode(value, comparator) as unknown as NodeCore<T>;
|
|
1099
|
+
}
|
|
1082
1100
|
return createArrayNode(value) as unknown as NodeCore<T>;
|
|
1083
1101
|
}
|
|
1084
1102
|
return createObjectNode(value as object) as unknown as NodeCore<T>;
|
|
@@ -1401,3 +1419,81 @@ export function nullable<T extends object>(value: T | null): T | null {
|
|
|
1401
1419
|
function isNullableMarked<T>(value: T): boolean {
|
|
1402
1420
|
return value !== null && typeof value === "object" && NULLABLE_MARKER in value;
|
|
1403
1421
|
}
|
|
1422
|
+
|
|
1423
|
+
// Symbol to mark an array with distinct options
|
|
1424
|
+
const ARRAY_MARKER = Symbol("array");
|
|
1425
|
+
|
|
1426
|
+
/** Comparison mode for array distinct checking */
|
|
1427
|
+
export type ArrayDistinct<T> =
|
|
1428
|
+
| false // No deduplication (default)
|
|
1429
|
+
| 'shallow' // Reference equality per element
|
|
1430
|
+
| 'deep' // JSON.stringify comparison
|
|
1431
|
+
| ((a: T[], b: T[]) => boolean); // Custom comparator
|
|
1432
|
+
|
|
1433
|
+
/** Options for the array() helper */
|
|
1434
|
+
export interface ArrayOptions<T> {
|
|
1435
|
+
/**
|
|
1436
|
+
* How to compare arrays to prevent duplicate emissions.
|
|
1437
|
+
* - false: No deduplication (default, always emits on set)
|
|
1438
|
+
* - 'shallow': Reference equality per element (a[i] === b[i])
|
|
1439
|
+
* - 'deep': JSON.stringify comparison (expensive for large arrays)
|
|
1440
|
+
* - function: Custom comparator (a, b) => boolean
|
|
1441
|
+
*/
|
|
1442
|
+
distinct?: ArrayDistinct<T>;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
interface MarkedArray<T> extends Array<T> {
|
|
1446
|
+
[ARRAY_MARKER]: ArrayOptions<T>;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
/**
|
|
1450
|
+
* Marks an array with options for how it should behave in state.
|
|
1451
|
+
* Use this to enable deduplication (prevent emissions when setting same values).
|
|
1452
|
+
*
|
|
1453
|
+
* @example
|
|
1454
|
+
* ```ts
|
|
1455
|
+
* const store = state({
|
|
1456
|
+
* // Default behavior - emits on every set
|
|
1457
|
+
* items: [1, 2, 3],
|
|
1458
|
+
*
|
|
1459
|
+
* // Shallow comparison - only emits if elements differ by reference
|
|
1460
|
+
* tags: array(['a', 'b'], { distinct: 'shallow' }),
|
|
1461
|
+
*
|
|
1462
|
+
* // Deep comparison - only emits if JSON representation differs
|
|
1463
|
+
* settings: array([{ theme: 'dark' }], { distinct: 'deep' }),
|
|
1464
|
+
*
|
|
1465
|
+
* // Custom comparator - you define equality
|
|
1466
|
+
* users: array([{ id: 1, name: 'Alice' }], {
|
|
1467
|
+
* distinct: (a, b) =>
|
|
1468
|
+
* a.length === b.length &&
|
|
1469
|
+
* a.every((user, i) => user.id === b[i].id)
|
|
1470
|
+
* }),
|
|
1471
|
+
* });
|
|
1472
|
+
* ```
|
|
1473
|
+
*/
|
|
1474
|
+
export function array<T>(value: T[], options: ArrayOptions<T> = {}): T[] {
|
|
1475
|
+
const marked = [...value] as MarkedArray<T>;
|
|
1476
|
+
marked[ARRAY_MARKER] = options;
|
|
1477
|
+
return marked;
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
// Check if an array was marked with options
|
|
1481
|
+
function isArrayMarked<T>(value: unknown): value is MarkedArray<T> {
|
|
1482
|
+
return Array.isArray(value) && ARRAY_MARKER in value;
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
// Get the distinct comparator function from array options
|
|
1486
|
+
function getArrayComparator<T>(options: ArrayOptions<T>): ((a: T[], b: T[]) => boolean) | undefined {
|
|
1487
|
+
if (!options.distinct) return undefined;
|
|
1488
|
+
|
|
1489
|
+
if (options.distinct === 'shallow') {
|
|
1490
|
+
return (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
if (options.distinct === 'deep') {
|
|
1494
|
+
return (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
// Custom function
|
|
1498
|
+
return options.distinct;
|
|
1499
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Core exports:
|
|
5
5
|
* - state() - Create reactive state from plain objects
|
|
6
6
|
* - nullable() - Mark a property as nullable (can transition between null and object)
|
|
7
|
+
* - array() - Mark an array with deduplication options
|
|
7
8
|
* - RxState, Draft - Type exports
|
|
8
9
|
*
|
|
9
10
|
* Helper exports:
|
|
@@ -11,5 +12,5 @@
|
|
|
11
12
|
* - selectFromEach() - Select from each array item with precise change detection
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
|
-
export { state, nullable, type RxState, type Draft, type StateOptions } from "./deepstate";
|
|
15
|
+
export { state, nullable, array, type RxState, type Draft, type StateOptions, type ArrayOptions, type ArrayDistinct } from "./deepstate";
|
|
15
16
|
export { select, selectFromEach } from "./helpers";
|