@angular-libs/signal-storage 0.2.0-beta.2 → 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/README.md +41 -41
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,27 @@
|
|
|
1
1
|
# @angular-libs/signal-storage
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Docs @angular-libs/signal-storage](https://www.npmjs.com/package/@angular-libs/signal-storage)
|
|
4
|
+
|
|
5
|
+
[StackBlitz demo](https://stackblitz.com/edit/signal-storage?file=src%2Fmain.ts)
|
|
6
|
+
|
|
7
|
+
`npm install @angular-libs/signal-storage`
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
A lightweight, type-safe Angular library for reactive state management using Signals. It provides seamless syncing with **localStorage**, **sessionStorage**, **IndexedDB** (via Angular Resources), and **In-Memory** storage.
|
|
6
10
|
|
|
7
11
|
---
|
|
8
12
|
|
|
9
|
-
##
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### 💾 1. `SignalStorage` service
|
|
16
|
+
|
|
17
|
+
Perfect for lightweight, synchronous key-value storage.
|
|
10
18
|
|
|
11
|
-
|
|
19
|
+
Supports:
|
|
20
|
+
|
|
21
|
+
- `localStorage` (default)
|
|
22
|
+
- `sessionStorage`
|
|
23
|
+
- `MemoryStorage` (similar to NgRx SignalStore)
|
|
24
|
+
- Custom
|
|
12
25
|
|
|
13
26
|
```typescript
|
|
14
27
|
import { Component, inject } from '@angular/core';
|
|
@@ -23,7 +36,7 @@ interface AppState {
|
|
|
23
36
|
template: `<button (click)="toggle()">{{ theme() }}</button>`,
|
|
24
37
|
})
|
|
25
38
|
export class AppComponent {
|
|
26
|
-
private storage = inject(SignalStorage
|
|
39
|
+
private storage: SignalStorage<AppState> = inject(SignalStorage);
|
|
27
40
|
theme = this.storage.getSignal('theme', 'light');
|
|
28
41
|
|
|
29
42
|
toggle() {
|
|
@@ -32,26 +45,32 @@ export class AppComponent {
|
|
|
32
45
|
}
|
|
33
46
|
```
|
|
34
47
|
|
|
35
|
-
|
|
48
|
+
#### Changing the Sync Storage Provider
|
|
36
49
|
|
|
37
|
-
You can switch to `sessionStorage
|
|
50
|
+
You can switch to `sessionStorage`, `MemoryStorage` or custom via providers:
|
|
38
51
|
|
|
39
52
|
```typescript
|
|
40
|
-
import {
|
|
53
|
+
import { SIGNAL_STORAGE_CONFIG, MemoryStorage } from '@angular-libs/signal-storage';
|
|
41
54
|
|
|
42
55
|
// In app.config.ts
|
|
43
56
|
providers: [
|
|
44
|
-
// For
|
|
45
|
-
{
|
|
57
|
+
// For Pure Memory Storage:
|
|
58
|
+
{
|
|
59
|
+
provide: SIGNAL_STORAGE_CONFIG,
|
|
60
|
+
useValue: { storageFactory: () => new MemoryStorage() },
|
|
61
|
+
},
|
|
46
62
|
|
|
47
|
-
//
|
|
48
|
-
{
|
|
63
|
+
// For Session Storage:
|
|
64
|
+
{
|
|
65
|
+
provide: SIGNAL_STORAGE_CONFIG,
|
|
66
|
+
useValue: { storageFactory: () => window.sessionStorage },
|
|
67
|
+
},
|
|
49
68
|
];
|
|
50
69
|
```
|
|
51
70
|
|
|
52
71
|
---
|
|
53
72
|
|
|
54
|
-
|
|
73
|
+
### 💾 2. Async `SignalIndexedDB` service (IndexedDB + Angular Resources)
|
|
55
74
|
|
|
56
75
|
Ideal for larger amounts of data. It uses Angular's new `resource` API and supports automatic cross-tab synchronization.
|
|
57
76
|
|
|
@@ -87,7 +106,7 @@ interface AppData {
|
|
|
87
106
|
`,
|
|
88
107
|
})
|
|
89
108
|
export class ProfileComponent {
|
|
90
|
-
private db = inject(SignalIndexedDB
|
|
109
|
+
private db: SignalIndexedDB<AppData> = inject(SignalIndexedDB);
|
|
91
110
|
|
|
92
111
|
// Automatically fetches from IndexedDB as a ResourceRef
|
|
93
112
|
profile = this.db.getResource('user');
|
|
@@ -100,25 +119,23 @@ export class ProfileComponent {
|
|
|
100
119
|
|
|
101
120
|
---
|
|
102
121
|
|
|
103
|
-
|
|
122
|
+
### 🛠 3. Advanced: Multiple Storage Instances
|
|
104
123
|
|
|
105
124
|
Need `localStorage` AND `MemoryStorage` in the same app? Extend the base class:
|
|
106
125
|
|
|
107
126
|
```typescript
|
|
108
|
-
import { Injectable } from '@angular/core';
|
|
109
|
-
import { SignalStorage,
|
|
127
|
+
import { Injectable, inject, Injector } from '@angular/core';
|
|
128
|
+
import { MemoryStorage, SignalStorage, SignalIndexedDB } from '@angular-libs/signal-storage';
|
|
110
129
|
|
|
130
|
+
// Store 1
|
|
111
131
|
interface LocalState {
|
|
112
132
|
user: string;
|
|
113
133
|
}
|
|
114
134
|
|
|
115
135
|
@Injectable({ providedIn: 'root' })
|
|
116
|
-
export class LocalStore extends SignalStorage<LocalState> {
|
|
117
|
-
constructor() {
|
|
118
|
-
super(typeof window !== 'undefined' ? window.localStorage : new MemoryStorage());
|
|
119
|
-
}
|
|
120
|
-
}
|
|
136
|
+
export class LocalStore extends SignalStorage<LocalState> {}
|
|
121
137
|
|
|
138
|
+
// Store 2
|
|
122
139
|
interface TempState {
|
|
123
140
|
tempId: string;
|
|
124
141
|
}
|
|
@@ -126,17 +143,11 @@ interface TempState {
|
|
|
126
143
|
@Injectable({ providedIn: 'root' })
|
|
127
144
|
export class TempStore extends SignalStorage<TempState> {
|
|
128
145
|
constructor() {
|
|
129
|
-
super(new MemoryStorage());
|
|
146
|
+
super({ storageFactory: () => new MemoryStorage() });
|
|
130
147
|
}
|
|
131
148
|
}
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
The same applies for `SignalIndexedDB` if you need multiple different databases:
|
|
135
|
-
|
|
136
|
-
```typescript
|
|
137
|
-
import { Injectable, Injector, inject } from '@angular/core';
|
|
138
|
-
import { SignalIndexedDB } from '@angular-libs/signal-storage';
|
|
139
149
|
|
|
150
|
+
// Store 3
|
|
140
151
|
interface UserDbState {
|
|
141
152
|
user: { name: string; email: string };
|
|
142
153
|
}
|
|
@@ -147,17 +158,6 @@ export class UserDb extends SignalIndexedDB<UserDbState> {
|
|
|
147
158
|
super(inject(Injector), { dbName: 'UserDb', storeName: 'keyValue', version: 1 });
|
|
148
159
|
}
|
|
149
160
|
}
|
|
150
|
-
|
|
151
|
-
interface SettingsDbState {
|
|
152
|
-
settings: { theme: 'light' | 'dark' };
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
@Injectable({ providedIn: 'root' })
|
|
156
|
-
export class SettingsDb extends SignalIndexedDB<SettingsDbState> {
|
|
157
|
-
constructor() {
|
|
158
|
-
super(inject(Injector), { dbName: 'SettingsDb', storeName: 'keyValue', version: 1 });
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
161
|
```
|
|
162
162
|
|
|
163
163
|
---
|