ngx-signal-plus 2.0.2 → 2.1.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/README.md +618 -597
- package/fesm2022/ngx-signal-plus.mjs +174 -1
- package/fesm2022/ngx-signal-plus.mjs.map +1 -1
- package/index.d.ts +39 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,597 +1,618 @@
|
|
|
1
|
-
# ngx-signal-plus
|
|
2
|
-
|
|
3
|
-
A powerful utility library that enhances Angular Signals with additional features for robust state management.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- Enhanced signal operations with built-in state tracking
|
|
8
|
-
- Type-safe validations and transformations
|
|
9
|
-
- Persistent storage with automatic serialization
|
|
10
|
-
- Time-based operations (debounce, throttle, delay)
|
|
11
|
-
- Signal operators for transformation and combination
|
|
12
|
-
- Built-in undo/redo functionality
|
|
13
|
-
- Form handling with validation
|
|
14
|
-
- Form groups with aggregated state and validation
|
|
15
|
-
- Async state management with loading, error, and retry logic
|
|
16
|
-
- Reactive Queries for server state (TanStack Query style)
|
|
17
|
-
- Collection management with ID-based CRUD operations
|
|
18
|
-
- Automatic cleanup and memory management
|
|
19
|
-
- Performance optimizations
|
|
20
|
-
- Transactions and batching for atomic operations
|
|
21
|
-
|
|
22
|
-
## Installation
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
npm install ngx-signal-plus
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
## Requirements
|
|
29
|
-
|
|
30
|
-
- Angular >= 16.0.0 (fully compatible with Angular 16-20)
|
|
31
|
-
- TypeScript >= 5.0.0
|
|
32
|
-
|
|
33
|
-
## Basic Usage
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
import { Component } from "@angular/core";
|
|
37
|
-
import { sp, enhance, spMap, spFilter } from "ngx-signal-plus";
|
|
38
|
-
import { signal, computed } from "@angular/core";
|
|
39
|
-
|
|
40
|
-
@Component({
|
|
41
|
-
standalone: true,
|
|
42
|
-
selector: "app-counter",
|
|
43
|
-
template: `
|
|
44
|
-
<div>Count: {{ counter.value() }}</div>
|
|
45
|
-
<div>Doubled: {{ doubled() }}</div>
|
|
46
|
-
<button (click)="increment()">Increment</button>
|
|
47
|
-
<button (click)="decrement()">Decrement</button>
|
|
48
|
-
|
|
49
|
-
@if (counter.history().length > 0) {
|
|
50
|
-
<button (click)="counter.undo()">Undo</button>
|
|
51
|
-
}
|
|
52
|
-
`,
|
|
53
|
-
})
|
|
54
|
-
export class CounterComponent {
|
|
55
|
-
// Create an enhanced signal with persistence and history
|
|
56
|
-
counter = sp(0)
|
|
57
|
-
.persist("counter")
|
|
58
|
-
.withHistory(10)
|
|
59
|
-
.validate((value) => value >= 0)
|
|
60
|
-
.build();
|
|
61
|
-
|
|
62
|
-
// Use signal operators
|
|
63
|
-
doubled = computed(() => this.counter.value() * 2);
|
|
64
|
-
|
|
65
|
-
increment() {
|
|
66
|
-
this.counter.setValue(this.counter.value() + 1);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
decrement() {
|
|
70
|
-
if (this.counter.value() > 0) {
|
|
71
|
-
this.counter.setValue(this.counter.value() - 1);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## Core Features
|
|
78
|
-
|
|
79
|
-
### Signal Creation
|
|
80
|
-
|
|
81
|
-
```typescript
|
|
82
|
-
import { sp, spCounter, spToggle, spForm } from "ngx-signal-plus";
|
|
83
|
-
|
|
84
|
-
// Simple enhanced signal
|
|
85
|
-
const name = sp("John").build();
|
|
86
|
-
|
|
87
|
-
// Counter with min/max validation
|
|
88
|
-
const counter = spCounter(0, { min: 0, max: 100 });
|
|
89
|
-
|
|
90
|
-
// Toggle (boolean) with persistence
|
|
91
|
-
const darkMode = spToggle(false, "theme-mode");
|
|
92
|
-
|
|
93
|
-
// Form input with validation
|
|
94
|
-
const username = spForm.text("", {
|
|
95
|
-
minLength: 3,
|
|
96
|
-
maxLength: 20,
|
|
97
|
-
debounce: 300,
|
|
98
|
-
});
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### Signal Enhancement
|
|
102
|
-
|
|
103
|
-
Enhance existing signals with additional features:
|
|
104
|
-
|
|
105
|
-
```typescript
|
|
106
|
-
import { enhance } from "ngx-signal-plus";
|
|
107
|
-
import { signal } from "@angular/core";
|
|
108
|
-
|
|
109
|
-
const enhanced = enhance(signal(0))
|
|
110
|
-
.persist("counter")
|
|
111
|
-
.validate((n) => n >= 0)
|
|
112
|
-
.transform(Math.round)
|
|
113
|
-
.withHistory(5)
|
|
114
|
-
.debounce(300)
|
|
115
|
-
.distinctUntilChanged()
|
|
116
|
-
.build();
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### Signal
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
//
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
},
|
|
207
|
-
);
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
{
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
userData
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
todos.
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
todos.
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
//
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
//
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
//
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
//
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
//
|
|
470
|
-
//
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
**
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
1
|
+
# ngx-signal-plus
|
|
2
|
+
|
|
3
|
+
A powerful utility library that enhances Angular Signals with additional features for robust state management.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Enhanced signal operations with built-in state tracking
|
|
8
|
+
- Type-safe validations and transformations
|
|
9
|
+
- Persistent storage with automatic serialization
|
|
10
|
+
- Time-based operations (debounce, throttle, delay)
|
|
11
|
+
- Signal operators for transformation and combination
|
|
12
|
+
- Built-in undo/redo functionality
|
|
13
|
+
- Form handling with validation
|
|
14
|
+
- Form groups with aggregated state and validation
|
|
15
|
+
- Async state management with loading, error, and retry logic
|
|
16
|
+
- Reactive Queries for server state (TanStack Query style)
|
|
17
|
+
- Collection management with ID-based CRUD operations
|
|
18
|
+
- Automatic cleanup and memory management
|
|
19
|
+
- Performance optimizations
|
|
20
|
+
- Transactions and batching for atomic operations
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install ngx-signal-plus
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Requirements
|
|
29
|
+
|
|
30
|
+
- Angular >= 16.0.0 (fully compatible with Angular 16-20)
|
|
31
|
+
- TypeScript >= 5.0.0
|
|
32
|
+
|
|
33
|
+
## Basic Usage
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { Component } from "@angular/core";
|
|
37
|
+
import { sp, enhance, spMap, spFilter } from "ngx-signal-plus";
|
|
38
|
+
import { signal, computed } from "@angular/core";
|
|
39
|
+
|
|
40
|
+
@Component({
|
|
41
|
+
standalone: true,
|
|
42
|
+
selector: "app-counter",
|
|
43
|
+
template: `
|
|
44
|
+
<div>Count: {{ counter.value() }}</div>
|
|
45
|
+
<div>Doubled: {{ doubled() }}</div>
|
|
46
|
+
<button (click)="increment()">Increment</button>
|
|
47
|
+
<button (click)="decrement()">Decrement</button>
|
|
48
|
+
|
|
49
|
+
@if (counter.history().length > 0) {
|
|
50
|
+
<button (click)="counter.undo()">Undo</button>
|
|
51
|
+
}
|
|
52
|
+
`,
|
|
53
|
+
})
|
|
54
|
+
export class CounterComponent {
|
|
55
|
+
// Create an enhanced signal with persistence and history
|
|
56
|
+
counter = sp(0)
|
|
57
|
+
.persist("counter")
|
|
58
|
+
.withHistory(10)
|
|
59
|
+
.validate((value) => value >= 0)
|
|
60
|
+
.build();
|
|
61
|
+
|
|
62
|
+
// Use signal operators
|
|
63
|
+
doubled = computed(() => this.counter.value() * 2);
|
|
64
|
+
|
|
65
|
+
increment() {
|
|
66
|
+
this.counter.setValue(this.counter.value() + 1);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
decrement() {
|
|
70
|
+
if (this.counter.value() > 0) {
|
|
71
|
+
this.counter.setValue(this.counter.value() - 1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Core Features
|
|
78
|
+
|
|
79
|
+
### Signal Creation
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { sp, spCounter, spToggle, spForm } from "ngx-signal-plus";
|
|
83
|
+
|
|
84
|
+
// Simple enhanced signal
|
|
85
|
+
const name = sp("John").build();
|
|
86
|
+
|
|
87
|
+
// Counter with min/max validation
|
|
88
|
+
const counter = spCounter(0, { min: 0, max: 100 });
|
|
89
|
+
|
|
90
|
+
// Toggle (boolean) with persistence
|
|
91
|
+
const darkMode = spToggle(false, "theme-mode");
|
|
92
|
+
|
|
93
|
+
// Form input with validation
|
|
94
|
+
const username = spForm.text("", {
|
|
95
|
+
minLength: 3,
|
|
96
|
+
maxLength: 20,
|
|
97
|
+
debounce: 300,
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Signal Enhancement
|
|
102
|
+
|
|
103
|
+
Enhance existing signals with additional features:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { enhance } from "ngx-signal-plus";
|
|
107
|
+
import { signal } from "@angular/core";
|
|
108
|
+
|
|
109
|
+
const enhanced = enhance(signal(0))
|
|
110
|
+
.persist("counter")
|
|
111
|
+
.validate((n) => n >= 0)
|
|
112
|
+
.transform(Math.round)
|
|
113
|
+
.withHistory(5)
|
|
114
|
+
.debounce(300)
|
|
115
|
+
.distinctUntilChanged()
|
|
116
|
+
.build();
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Computed Signal Enhancement
|
|
120
|
+
|
|
121
|
+
Create computed signals with persistence, history, and validation:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
import { spComputed } from "ngx-signal-plus";
|
|
125
|
+
import { signal } from "@angular/core";
|
|
126
|
+
|
|
127
|
+
const firstName = signal("John");
|
|
128
|
+
const lastName = signal("Doe");
|
|
129
|
+
|
|
130
|
+
// Computed signal with history and persistence
|
|
131
|
+
const fullName = spComputed(() => `${firstName()} ${lastName()}`, { persist: "user-fullname", historySize: 5 });
|
|
132
|
+
|
|
133
|
+
fullName.value; // 'John Doe'
|
|
134
|
+
firstName.set("Jane");
|
|
135
|
+
fullName.value; // 'Jane Doe' (auto-updates)
|
|
136
|
+
fullName.undo(); // 'John Doe'
|
|
137
|
+
fullName.isValid(); // true
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Signal Operators
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { spMap, spFilter, spDebounceTime, spCombineLatest } from "ngx-signal-plus";
|
|
144
|
+
import { signal } from "@angular/core";
|
|
145
|
+
|
|
146
|
+
// Transform values
|
|
147
|
+
const price = signal(100);
|
|
148
|
+
const withTax = price.pipe(
|
|
149
|
+
spMap((n) => n * 1.2),
|
|
150
|
+
spMap((n) => Math.round(n * 100) / 100),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// Combine signals
|
|
154
|
+
const firstName = signal("John");
|
|
155
|
+
const lastName = signal("Doe");
|
|
156
|
+
const fullName = spCombineLatest([firstName, lastName]).pipe(spMap(([first, last]) => `${first} ${last}`));
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Form Handling
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { spForm } from "ngx-signal-plus";
|
|
163
|
+
import { computed } from "@angular/core";
|
|
164
|
+
|
|
165
|
+
// Form inputs with validation
|
|
166
|
+
const username = spForm.text("", { minLength: 3, maxLength: 20 });
|
|
167
|
+
const email = spForm.email("");
|
|
168
|
+
const age = spForm.number({ min: 18, max: 99, initial: 30 });
|
|
169
|
+
|
|
170
|
+
// Form validation
|
|
171
|
+
const isFormValid = computed(() => username.isValid() && email.isValid() && age.isValid());
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Form Groups
|
|
175
|
+
|
|
176
|
+
Group multiple form controls together with aggregated state, validation, and persistence:
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { spFormGroup, spForm } from "ngx-signal-plus";
|
|
180
|
+
|
|
181
|
+
// Basic form group
|
|
182
|
+
const loginForm = spFormGroup({
|
|
183
|
+
email: spForm.email(""),
|
|
184
|
+
password: spForm.text("", { minLength: 8 }),
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// Access aggregated state
|
|
188
|
+
loginForm.isValid(); // false if password < 8 chars
|
|
189
|
+
loginForm.isDirty(); // true if any field changed
|
|
190
|
+
loginForm.isTouched(); // true if any field touched
|
|
191
|
+
loginForm.value(); // { email: '', password: '' }
|
|
192
|
+
loginForm.errors(); // { email: [...], password: [...] }
|
|
193
|
+
|
|
194
|
+
// Update values
|
|
195
|
+
loginForm.setValue({ email: "user@example.com", password: "secret123" });
|
|
196
|
+
loginForm.patchValue({ email: "new@example.com" }); // Partial update
|
|
197
|
+
|
|
198
|
+
// Form actions
|
|
199
|
+
loginForm.reset(); // Reset all fields to initial values
|
|
200
|
+
loginForm.markAsTouched(); // Mark all fields as touched
|
|
201
|
+
loginForm.submit(); // Returns values if valid, null otherwise
|
|
202
|
+
|
|
203
|
+
// Nested form groups
|
|
204
|
+
const credentials = spFormGroup({
|
|
205
|
+
email: spForm.email(""),
|
|
206
|
+
password: spForm.text("", { minLength: 8 }),
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const profile = spFormGroup({
|
|
210
|
+
name: spForm.text(""),
|
|
211
|
+
age: spForm.number({ min: 18 }),
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const registrationForm = spFormGroup({
|
|
215
|
+
credentials,
|
|
216
|
+
profile,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// Group-level validation
|
|
220
|
+
const passwordForm = spFormGroup(
|
|
221
|
+
{
|
|
222
|
+
password: spForm.text("password123"),
|
|
223
|
+
confirmPassword: spForm.text("password123"),
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
validators: [(values) => values.password === values.confirmPassword || "Passwords must match"],
|
|
227
|
+
},
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
// Persistence
|
|
231
|
+
const persistedForm = spFormGroup(
|
|
232
|
+
{
|
|
233
|
+
email: spForm.email(""),
|
|
234
|
+
preferences: spForm.text(""),
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
persistKey: "user-form", // Automatically saves/restores from localStorage
|
|
238
|
+
},
|
|
239
|
+
);
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Async State Management
|
|
243
|
+
|
|
244
|
+
Manage asynchronous operations with built-in loading, error, and data states:
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
import { spAsync } from "ngx-signal-plus";
|
|
248
|
+
|
|
249
|
+
const userData = spAsync<User>({
|
|
250
|
+
fetcher: () => fetch("/api/user").then((r) => r.json()),
|
|
251
|
+
initialValue: null,
|
|
252
|
+
retryCount: 3,
|
|
253
|
+
retryDelay: 1000,
|
|
254
|
+
cacheTime: 5000,
|
|
255
|
+
autoFetch: true,
|
|
256
|
+
onSuccess: (data) => console.log("Loaded:", data),
|
|
257
|
+
onError: (error) => console.error("Failed:", error),
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// Reactive state signals
|
|
261
|
+
userData.data(); // Signal<User | null>
|
|
262
|
+
userData.loading(); // Signal<boolean>
|
|
263
|
+
userData.error(); // Signal<Error | null>
|
|
264
|
+
userData.isSuccess(); // Signal<boolean>
|
|
265
|
+
userData.isError(); // Signal<boolean>
|
|
266
|
+
|
|
267
|
+
// Methods
|
|
268
|
+
await userData.refetch(); // Manually refetch data
|
|
269
|
+
userData.invalidate(); // Mark cache as stale
|
|
270
|
+
userData.reset(); // Reset to initial state
|
|
271
|
+
userData.mutate(newData); // Optimistic update
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### Reactive Queries
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
import { QueryClient, setGlobalQueryClient } from "ngx-signal-plus";
|
|
278
|
+
import { spQuery, spMutation } from "ngx-signal-plus";
|
|
279
|
+
|
|
280
|
+
const qc = new QueryClient();
|
|
281
|
+
setGlobalQueryClient(qc);
|
|
282
|
+
|
|
283
|
+
const todosQuery = spQuery({
|
|
284
|
+
queryKey: ["todos"],
|
|
285
|
+
queryFn: async () => fetch("/api/todos").then((r) => r.json()),
|
|
286
|
+
staleTime: 5000,
|
|
287
|
+
refetchOnWindowFocus: true,
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
const addTodo = spMutation({
|
|
291
|
+
mutationFn: async (title: string) => postTodo(title),
|
|
292
|
+
onMutate: (title) => {
|
|
293
|
+
qc.setQueryData(["todos"], (prev) => [...((prev as { title: string }[] | undefined) ?? []), { title }], true);
|
|
294
|
+
},
|
|
295
|
+
onSuccess: () => qc.refetchQueries(["todos"]),
|
|
296
|
+
});
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Highlights:
|
|
300
|
+
|
|
301
|
+
- Cache-aware queries with invalidation and refetch
|
|
302
|
+
- Mutations with optimistic updates
|
|
303
|
+
- Interval/focus/reconnect refetch strategies
|
|
304
|
+
|
|
305
|
+
### Collection Management
|
|
306
|
+
|
|
307
|
+
Manage arrays of entities with ID-based operations, optimized updates, and history support:
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
import { spCollection } from "ngx-signal-plus";
|
|
311
|
+
|
|
312
|
+
interface Todo {
|
|
313
|
+
id: string;
|
|
314
|
+
title: string;
|
|
315
|
+
completed: boolean;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const todos = spCollection<Todo>({
|
|
319
|
+
idField: "id",
|
|
320
|
+
initialValue: [],
|
|
321
|
+
persist: "todos-key",
|
|
322
|
+
withHistory: true,
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// CRUD operations
|
|
326
|
+
todos.add({ id: "1", title: "Learn Angular", completed: false });
|
|
327
|
+
todos.addMany([todo1, todo2, todo3]);
|
|
328
|
+
todos.update("1", { completed: true });
|
|
329
|
+
todos.updateMany([
|
|
330
|
+
{ id: "1", changes: { completed: true } },
|
|
331
|
+
{ id: "2", changes: { title: "Updated" } },
|
|
332
|
+
]);
|
|
333
|
+
todos.remove("1");
|
|
334
|
+
todos.removeMany(["1", "2"]);
|
|
335
|
+
todos.clear();
|
|
336
|
+
|
|
337
|
+
// Query operations
|
|
338
|
+
const todo = todos.findById("1");
|
|
339
|
+
const completed = todos.filter((t) => t.completed);
|
|
340
|
+
const firstCompleted = todos.find((t) => t.completed);
|
|
341
|
+
const hasCompleted = todos.some((t) => t.completed);
|
|
342
|
+
const allCompleted = todos.every((t) => t.completed);
|
|
343
|
+
|
|
344
|
+
// Transform operations
|
|
345
|
+
const sorted = todos.sort((a, b) => a.title.localeCompare(b.title));
|
|
346
|
+
const titles = todos.map((t) => t.title);
|
|
347
|
+
const totalCompleted = todos.reduce((acc, t) => acc + (t.completed ? 1 : 0), 0);
|
|
348
|
+
|
|
349
|
+
// History operations
|
|
350
|
+
todos.undo(); // Undo last operation
|
|
351
|
+
todos.redo(); // Redo last undone operation
|
|
352
|
+
todos.canUndo(); // Check if undo is available
|
|
353
|
+
todos.canRedo(); // Check if redo is available
|
|
354
|
+
|
|
355
|
+
// Reactive signals
|
|
356
|
+
todos.value(); // Signal<Todo[]>
|
|
357
|
+
todos.count(); // Signal<number>
|
|
358
|
+
todos.isEmpty(); // Signal<boolean>
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### Validation and Presets
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
import { spValidators, spPresets } from "ngx-signal-plus";
|
|
365
|
+
|
|
366
|
+
// Use built-in validators
|
|
367
|
+
const email = sp("").validate(spValidators.string.required).validate(spValidators.string.email).build();
|
|
368
|
+
|
|
369
|
+
// Use presets for common patterns
|
|
370
|
+
const counter = spPresets.counter({
|
|
371
|
+
initial: 0,
|
|
372
|
+
min: 0,
|
|
373
|
+
max: 100,
|
|
374
|
+
step: 1,
|
|
375
|
+
withHistory: true,
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
const darkMode = spPresets.toggle({
|
|
379
|
+
initial: false,
|
|
380
|
+
persistent: true,
|
|
381
|
+
storageKey: "theme-mode",
|
|
382
|
+
});
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### State Management
|
|
386
|
+
|
|
387
|
+
```typescript
|
|
388
|
+
import { spStorageManager, sp } from "ngx-signal-plus";
|
|
389
|
+
|
|
390
|
+
// Storage management (saves to localStorage with namespace prefix)
|
|
391
|
+
spStorageManager.save("app-settings", { theme: "dark", language: "en" });
|
|
392
|
+
const settings = spStorageManager.load<{ theme: string; language: string }>("app-settings");
|
|
393
|
+
|
|
394
|
+
// Remove when no longer needed
|
|
395
|
+
spStorageManager.remove("app-settings");
|
|
396
|
+
|
|
397
|
+
// History management through signals
|
|
398
|
+
const counter = sp(0)
|
|
399
|
+
.withHistory(10) // Keep last 10 values
|
|
400
|
+
.build();
|
|
401
|
+
|
|
402
|
+
counter.setValue(1);
|
|
403
|
+
counter.setValue(2);
|
|
404
|
+
counter.setValue(3);
|
|
405
|
+
|
|
406
|
+
// Navigate history
|
|
407
|
+
counter.undo(); // Back to 2
|
|
408
|
+
counter.undo(); // Back to 1
|
|
409
|
+
counter.redo(); // Forward to 2
|
|
410
|
+
|
|
411
|
+
// Check history
|
|
412
|
+
console.log(counter.history()); // Array of past values
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
### Cleanup and Memory Management
|
|
416
|
+
|
|
417
|
+
**ngx-signal-plus** provides automatic and manual cleanup to prevent memory leaks:
|
|
418
|
+
|
|
419
|
+
```typescript
|
|
420
|
+
import { sp } from "ngx-signal-plus";
|
|
421
|
+
|
|
422
|
+
// Automatic cleanup when all subscribers unsubscribe
|
|
423
|
+
const signal = sp(0).persist("counter").debounce(300).build();
|
|
424
|
+
const unsubscribe = signal.subscribe((value) => console.log(value));
|
|
425
|
+
|
|
426
|
+
// When you're done with the signal
|
|
427
|
+
unsubscribe(); // Automatically cleans up when last subscriber unsubscribes
|
|
428
|
+
|
|
429
|
+
// Manual cleanup with destroy()
|
|
430
|
+
const signal2 = sp(0).persist("data").withHistory(10).build();
|
|
431
|
+
signal2.setValue(42);
|
|
432
|
+
|
|
433
|
+
// Explicitly destroy and clean up all resources
|
|
434
|
+
signal2.destroy(); // Removes event listeners, clears timers, frees memory
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
**What gets cleaned up:**
|
|
438
|
+
|
|
439
|
+
- ✅ Storage event listeners (for `localStorage` synchronization)
|
|
440
|
+
- ✅ Debounce/throttle timers
|
|
441
|
+
- ✅ All subscribers
|
|
442
|
+
- ✅ Pending operations
|
|
443
|
+
|
|
444
|
+
**SSR-Safe:** All cleanup operations work safely in server-side rendering environments.
|
|
445
|
+
|
|
446
|
+
### Transactions and Batching
|
|
447
|
+
|
|
448
|
+
Group multiple updates together with automatic rollback on errors:
|
|
449
|
+
|
|
450
|
+
```typescript
|
|
451
|
+
import { spTransaction, spBatch } from "ngx-signal-plus";
|
|
452
|
+
|
|
453
|
+
const balance = sp(100).build();
|
|
454
|
+
const cart = sp<string[]>([]).build();
|
|
455
|
+
|
|
456
|
+
// Transaction with automatic rollback
|
|
457
|
+
try {
|
|
458
|
+
spTransaction(() => {
|
|
459
|
+
balance.setValue(balance.value() - 50);
|
|
460
|
+
cart.update((items) => [...items, "premium-item"]);
|
|
461
|
+
|
|
462
|
+
if (balance.value() < 0) {
|
|
463
|
+
throw new Error("Insufficient funds");
|
|
464
|
+
}
|
|
465
|
+
// Success - changes are committed
|
|
466
|
+
});
|
|
467
|
+
} catch (error) {
|
|
468
|
+
// Error - all changes automatically rolled back
|
|
469
|
+
console.log(balance.value()); // 100 (original value)
|
|
470
|
+
console.log(cart.value()); // [] (original value)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Batch updates for performance (no rollback)
|
|
474
|
+
spBatch(() => {
|
|
475
|
+
signal1.setValue(1);
|
|
476
|
+
signal2.setValue(2);
|
|
477
|
+
signal3.setValue(3);
|
|
478
|
+
// All changes applied together efficiently
|
|
479
|
+
});
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### Server-Side Rendering
|
|
483
|
+
|
|
484
|
+
The library works seamlessly with Angular Universal:
|
|
485
|
+
|
|
486
|
+
```typescript
|
|
487
|
+
// This code works in both SSR and browser
|
|
488
|
+
const userPrefs = sp({ theme: "dark" }).persist("user-preferences").build();
|
|
489
|
+
|
|
490
|
+
// In SSR: works in-memory, localStorage calls are safely skipped
|
|
491
|
+
// In browser: full persistence with localStorage
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
What happens during SSR:
|
|
495
|
+
|
|
496
|
+
- Signals work normally with in-memory state
|
|
497
|
+
- localStorage operations are safely skipped (no errors)
|
|
498
|
+
- State automatically persists once the app runs in the browser
|
|
499
|
+
|
|
500
|
+
## Available Features
|
|
501
|
+
|
|
502
|
+
| Category | Features |
|
|
503
|
+
| --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
504
|
+
| **Signal Creation** | `sp`, `spCounter`, `spToggle`, `spForm`, `spComputed` |
|
|
505
|
+
| **Signal Enhancement** | `enhance`, validation, transformation, persistence, history |
|
|
506
|
+
| **Signal Operators** | `spMap`, `spFilter`, `spDebounceTime`, `spThrottleTime`, `spDelay`, `spDistinctUntilChanged`, `spSkip`, `spTake`, `spMerge`, `spCombineLatest` |
|
|
507
|
+
| **Form Groups** | `spFormGroup` - Group multiple controls with aggregated state, validation, and persistence |
|
|
508
|
+
| **Async State Management** | `spAsync` - Manage asynchronous operations with loading, error, retry, and caching |
|
|
509
|
+
| **Collection Management** | `spCollection` - Manage arrays of entities with ID-based CRUD, queries, transforms, and history |
|
|
510
|
+
| **Transactions & Batching** | `spTransaction`, `spBatch`, `spIsTransactionActive`, `spIsInTransaction`, `spIsInBatch`, `spGetModifiedSignals` |
|
|
511
|
+
| **Utilities** | `spValidators`, `spPresets` |
|
|
512
|
+
| **State Management** | `spHistoryManager`, `spStorageManager` |
|
|
513
|
+
| **Components** | `spSignalPlusComponent`, `spSignalPlusService`, `spSignalBuilder` |
|
|
514
|
+
|
|
515
|
+
## Bundle Size Optimization
|
|
516
|
+
|
|
517
|
+
The library is built with tree-shaking and optimization in mind. You only pay for what you use.
|
|
518
|
+
|
|
519
|
+
### Modern Package Exports
|
|
520
|
+
|
|
521
|
+
The package provides **modular exports** for selective importing:
|
|
522
|
+
|
|
523
|
+
```typescript
|
|
524
|
+
// Import only what you need - tree-shaking removes unused code
|
|
525
|
+
|
|
526
|
+
// Core signals only (~3KB gzipped)
|
|
527
|
+
import { sp, spCounter, spToggle } from "ngx-signal-plus/core";
|
|
528
|
+
|
|
529
|
+
// Operators only (~2KB gzipped)
|
|
530
|
+
import { spMap, spFilter, spDebounceTime } from "ngx-signal-plus/operators";
|
|
531
|
+
|
|
532
|
+
// Utilities only (~2KB gzipped)
|
|
533
|
+
import { enhance, spValidators, spPresets } from "ngx-signal-plus/utils";
|
|
534
|
+
|
|
535
|
+
// State managers (~1KB gzipped)
|
|
536
|
+
import { spHistoryManager, spStorageManager } from "ngx-signal-plus";
|
|
537
|
+
|
|
538
|
+
// Everything (~8KB gzipped)
|
|
539
|
+
import { sp, spMap, spFilter, enhance, spValidators } from "ngx-signal-plus";
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
### Tree-Shaking Configuration
|
|
543
|
+
|
|
544
|
+
The package is optimized for tree-shaking:
|
|
545
|
+
|
|
546
|
+
- ✅ **`sideEffects: false`** in package.json - marks the library as side-effect free
|
|
547
|
+
- ✅ **Modular exports** - separate entry points for each feature category
|
|
548
|
+
- ✅ **ES2022 modules** - modern JavaScript with full tree-shaking support
|
|
549
|
+
- ✅ **FESM bundles** - Flat ESM bundles for better optimization
|
|
550
|
+
- ✅ **Individual entry points** for granular control:
|
|
551
|
+
- `ngx-signal-plus/core` - Core signal creation
|
|
552
|
+
- `ngx-signal-plus/operators` - Signal operators
|
|
553
|
+
- `ngx-signal-plus/utils` - Utilities and validators
|
|
554
|
+
- `ngx-signal-plus/models` - TypeScript types
|
|
555
|
+
|
|
556
|
+
### Best Practices for Minimal Bundle
|
|
557
|
+
|
|
558
|
+
**1. Import only what you need:**
|
|
559
|
+
|
|
560
|
+
```typescript
|
|
561
|
+
// ✅ Good - imports only used features
|
|
562
|
+
import { sp, spCounter } from "ngx-signal-plus";
|
|
563
|
+
|
|
564
|
+
// ❌ Avoid - imports everything even if unused
|
|
565
|
+
import * as SignalPlus from "ngx-signal-plus";
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
**2. Use named imports:**
|
|
569
|
+
|
|
570
|
+
```typescript
|
|
571
|
+
// ✅ Good - tree-shaking can remove unused exports
|
|
572
|
+
import { sp, spMap } from "ngx-signal-plus";
|
|
573
|
+
|
|
574
|
+
// ❌ Less optimal - may import more than needed
|
|
575
|
+
import SignalPlus from "ngx-signal-plus";
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
**3. Import from specific entry points:**
|
|
579
|
+
|
|
580
|
+
```typescript
|
|
581
|
+
// ✅ Good - direct import from feature module
|
|
582
|
+
import { spMap, spFilter } from "ngx-signal-plus/operators";
|
|
583
|
+
|
|
584
|
+
// ✅ Also good - barrel export handles tree-shaking
|
|
585
|
+
import { spMap, spFilter } from "ngx-signal-plus";
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
### Typical Bundle Sizes
|
|
589
|
+
|
|
590
|
+
| Feature Set | Size (gzipped) | Savings vs Full |
|
|
591
|
+
| --------------- | -------------- | --------------- |
|
|
592
|
+
| Just `sp()` | ~1.5 KB | -87% |
|
|
593
|
+
| Core signals | ~3 KB | -62% |
|
|
594
|
+
| + Operators | ~5 KB | -38% |
|
|
595
|
+
| + All utilities | ~8 KB | 0% |
|
|
596
|
+
|
|
597
|
+
### Performance Impact
|
|
598
|
+
|
|
599
|
+
- **Tree-shaking enabled**: Webpack, Vite, Rollup automatically remove unused code
|
|
600
|
+
- **No performance penalty**: Modern bundlers handle optimization automatically
|
|
601
|
+
- **Zero runtime overhead**: Only loaded features are included
|
|
602
|
+
|
|
603
|
+
## Documentation
|
|
604
|
+
|
|
605
|
+
For detailed documentation including all features, API reference, and examples, see our [API Documentation](https://github.com/milad-hub/ngx-signal-plus/blob/main/projects/signal-plus/docs/API.md).
|
|
606
|
+
|
|
607
|
+
## Contributing
|
|
608
|
+
|
|
609
|
+
Please read our [Contributing Guide](https://github.com/milad-hub/ngx-signal-plus/blob/main/projects/signal-plus/CONTRIBUTING.md).
|
|
610
|
+
|
|
611
|
+
## Support
|
|
612
|
+
|
|
613
|
+
- [Documentation](https://github.com/milad-hub/ngx-signal-plus/blob/main/projects/signal-plus/docs/API.md)
|
|
614
|
+
- [Issue Tracker](https://github.com/milad-hub/ngx-signal-plus/issues)
|
|
615
|
+
|
|
616
|
+
## License
|
|
617
|
+
|
|
618
|
+
MIT
|