fluxie 1.0.0-lts
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/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/README.md +185 -0
- package/dist/devtools.d.ts +5 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/main.js +1 -0
- package/dist/store.d.ts +16 -0
- package/package.json +35 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [1.2.1](https://github.com/LesFabricants/Fluxie/compare/v1.2.0...v1.2.1) (2024-06-04)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **store:** do not initialize in the upgrade event ([8a9b42d](https://github.com/LesFabricants/Fluxie/commit/8a9b42dddbee9382bdb10e9601fce9b1e8bd5b42))
|
|
9
|
+
|
|
10
|
+
## [1.2.0](https://github.com/LesFabricants/Fluxie/compare/v1.1.0...v1.2.0) (2024-06-04)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* **pkg:** add package.json data ([23b1b79](https://github.com/LesFabricants/Fluxie/commit/23b1b79e6a2f5d448cb0e7a6a263b7259a5809c2))
|
|
16
|
+
|
|
17
|
+
## [1.1.0](https://github.com/LesFabricants/Fluxie/compare/v1.0.0...v1.1.0) (2024-06-04)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* **store:** update store name to prevent conflicts ([5f1662c](https://github.com/LesFabricants/Fluxie/commit/5f1662cae25ba8c70cc3113bd28014c16d8229d9))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Les Fabricants
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# Fluxie
|
|
2
|
+
|
|
3
|
+
Super small helper class meant to greatly simplify the creation of a flux architecture using angular service, complete with redux devtool and caching support
|
|
4
|
+
|
|
5
|
+
**Warning**
|
|
6
|
+
**Requires 2Kb of available space, make sure you have the space before installing !**
|
|
7
|
+
|
|
8
|
+
## Syntax
|
|
9
|
+
|
|
10
|
+
> The `store.ts` files is a base class that can be extended by services to add the necessary methods and properties required to handle a centralized state
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
interface UsersState {
|
|
14
|
+
users: IUser[];
|
|
15
|
+
selection: IUser[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Injectable({ providedIn: "root" })
|
|
19
|
+
export class UsersService extends Store<UsersState> {
|
|
20
|
+
constructor() {
|
|
21
|
+
super({ users: [], selection: [] }, { cache: true, storeName: "Users" });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Parameters
|
|
27
|
+
|
|
28
|
+
- `initialState`
|
|
29
|
+
the initial state of the store
|
|
30
|
+
|
|
31
|
+
- `options` (optional)
|
|
32
|
+
- `storeName`
|
|
33
|
+
name to use for the store
|
|
34
|
+
- `cache`
|
|
35
|
+
a boolean value, if true, enables the caching of the state via IndexedDB
|
|
36
|
+
|
|
37
|
+
## Examples
|
|
38
|
+
|
|
39
|
+
`users.service.ts`
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
interface UsersState {
|
|
43
|
+
users: IUser[];
|
|
44
|
+
selection: IUser[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Injectable({ providedIn: "root" })
|
|
48
|
+
export class UsersService extends Store<UsersState> {
|
|
49
|
+
constructor() {
|
|
50
|
+
super({ users: [], selection: [] });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
users = computed(() => {
|
|
54
|
+
return this.store.select().users.map((user) => new User(user));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
users$: Observable<User[]> = this.store.select$.pipe(
|
|
58
|
+
map((state) => state.users),
|
|
59
|
+
map((users) => users.map((user) => new User(user)))
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
get() {
|
|
63
|
+
this.http.get<IUser[]>(`${URL}/users`).subscribe((users) => {
|
|
64
|
+
this.setUsers(users);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
updateRole(id: string, role: RoleEnum) {
|
|
69
|
+
this.http.patch<IUser>(`${URL}/users/${id}`, { role }).subscribe((user) => {
|
|
70
|
+
this.updateUser(id, user);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
setUsers(users: IUser[]) {
|
|
75
|
+
this.setState("set users", (state) => ({
|
|
76
|
+
...state,
|
|
77
|
+
users,
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
updateUser(id: string, user: IUser) {
|
|
82
|
+
this.setState("update user", (state) => {
|
|
83
|
+
return {
|
|
84
|
+
...state,
|
|
85
|
+
users: state.users.map((currentUser) =>
|
|
86
|
+
currentUser.id === id ? user : currentUser
|
|
87
|
+
),
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
toggleUserSelection(user: IUser) {
|
|
93
|
+
this.setState("toggle user selection", (state) => {
|
|
94
|
+
const newSelection = state.selection;
|
|
95
|
+
const index = newSelection.findIndex(({ id }) => id === user.id);
|
|
96
|
+
|
|
97
|
+
if (index > -1) {
|
|
98
|
+
newSelection.splice(index, 1);
|
|
99
|
+
} else {
|
|
100
|
+
newSelection.push(user);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
...state,
|
|
105
|
+
selection: newSelection,
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
emptySelection() {
|
|
111
|
+
this.setState("empty selection", (state) => ({
|
|
112
|
+
...state,
|
|
113
|
+
selection: [],
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
`users.component.ts`
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
@Component({
|
|
123
|
+
selector: "oney-users",
|
|
124
|
+
templateUrl: "./users.component.html",
|
|
125
|
+
styleUrls: ["./users.component.scss"],
|
|
126
|
+
})
|
|
127
|
+
export class UsersComponent implements OnInit {
|
|
128
|
+
protected usersService = inject(UsersService);
|
|
129
|
+
|
|
130
|
+
constructor() {}
|
|
131
|
+
|
|
132
|
+
ngOnInit(): void {
|
|
133
|
+
this.usersService.get();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Via Observables:
|
|
139
|
+
|
|
140
|
+
```html
|
|
141
|
+
<article>
|
|
142
|
+
@for (user of usersService.users$ | async) {
|
|
143
|
+
<app-user [user]="user"></app-user>
|
|
144
|
+
}
|
|
145
|
+
</article>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Via Signals:
|
|
149
|
+
|
|
150
|
+
```html
|
|
151
|
+
<article>
|
|
152
|
+
@for (user of usersService.users()) {
|
|
153
|
+
<app-user [user]="user"></app-user>
|
|
154
|
+
}
|
|
155
|
+
</article>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Note: You are free to organize this however you want, although the recommended organization would be to split your service file into 3 files:
|
|
159
|
+
|
|
160
|
+
- `users.service.ts`
|
|
161
|
+
makes http calls, and is overall the file your application calls to request state changes
|
|
162
|
+
|
|
163
|
+
- in the example above, this would contain get and updateRole
|
|
164
|
+
|
|
165
|
+
- `users.query.ts`
|
|
166
|
+
contains all of the variations of the filtered state used throughout the application
|
|
167
|
+
|
|
168
|
+
- in the example above, this would contain users and users$
|
|
169
|
+
|
|
170
|
+
- `users.store.ts`
|
|
171
|
+
the file that is only called by `users.service.ts` and `users.query.ts`, this is the file that contains the store initialization code, the state typing, and every method that directly alters the state
|
|
172
|
+
|
|
173
|
+
- in the example above, this would contain setUsers, updateUser, toggleUserSelection and emptySelection
|
|
174
|
+
|
|
175
|
+
## Devtool support
|
|
176
|
+
|
|
177
|
+
In order to add devtool support add this snippet to your `app.module.ts`, right after your imports:
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
if (!environment.production) {
|
|
181
|
+
enableReduxDevtools();
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Your redux devtool menu will now show all your store actions, the option `storeName` will be the store instance name there (defaults to the class name if not provided), and every service extending the `Store` class will be its own instance in the dropdown at the top.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BehaviorSubject } from "rxjs";
|
|
2
|
+
export declare let isDevtoolEnabled: boolean;
|
|
3
|
+
export declare function enableReduxDevtools(): void;
|
|
4
|
+
export declare function initializeReduxDevtools(storeName: string, state: BehaviorSubject<any>, initialState: any): false | undefined;
|
|
5
|
+
export declare function sendActionToDevtools(storeName: string, actionName: string, payload: any): false | undefined;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{BehaviorSubject as u}from"rxjs";var r={},o=!1;function h(){o=!0}function c(a,t,s){if(!d())return!1;let e=window?.__REDUX_DEVTOOLS_EXTENSION__.connect({name:a});e.init(s),e.subscribe(n=>{n.type==="DISPATCH"&&n.payload.type==="JUMP_TO_ACTION"&&t.next(JSON.parse(n.state))}),r[a]=e}function l(a,t,s){if(!d())return!1;if(!o)throw new Error("Devtools not enabled, please call enableReduxDevtools() in app.module.ts");r[a].send(t,s)}function d(){return window?.__REDUX_DEVTOOLS_EXTENSION__!==void 0}var i=class{options;state;select$;db;constructor(t,s){if(this.options={...s,storeName:s?.storeName??this.constructor.name},this.state=new u(t),this.select$=this.state.asObservable(),s?.cache){let e=globalThis.indexedDB.open("__FLUXIE_STORE",1);e.onerror=()=>{console.error(`IndexedDB error: ${e.error}`)},e.onsuccess=()=>{this.db=e.result,this.initializeFromIndexedDB()},e.onupgradeneeded=n=>{this.db=e.result,n.newVersion===1&&this.db.createObjectStore("cachedData")}}o&&c(this.options.storeName,this.state,this.state.getValue())}setState(t,s){let e=s(this.state.getValue());this.state.next(e),this.options?.cache&&this.sendToIndexedDB(this.state.getValue()),o&&l(this.options.storeName,t,e)}async initializeFromIndexedDB(){let t=this.db.transaction("cachedData").objectStore("cachedData").get(this.options.storeName);t.onsuccess=()=>{let s=t.result;s?this.state.next(s):this.sendToIndexedDB(this.state.getValue())}}async sendToIndexedDB(t){this.db.transaction("cachedData","readwrite").objectStore("cachedData").put(t,this.options.storeName)}};export{i as Store,h as enableReduxDevtools,o as isDevtoolEnabled};
|
package/dist/main.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{BehaviorSubject as p}from"rxjs";var r={},o=!1;function h(){o=!0}function l(i,t,s){if(!d())return!1;let e=window?.__REDUX_DEVTOOLS_EXTENSION__.connect({name:i});e.init(s),e.subscribe(n=>{n.type==="DISPATCH"&&n.payload.type==="JUMP_TO_ACTION"&&t.next(JSON.parse(n.state))}),r[i]=e}function c(i,t,s){if(!d())return!1;if(!o)throw new Error("Devtools not enabled, please call enableReduxDevtools() in app.module.ts");r[i].send(t,s)}function d(){return window?.__REDUX_DEVTOOLS_EXTENSION__!==void 0}import{toSignal as u}from"@angular/core/rxjs-interop";var a=class{options;state;select$;select;db;constructor(t,s){if(this.options={...s,storeName:s?.storeName??this.constructor.name},this.state=new p(t),this.select$=this.state.asObservable(),this.select=u(this.state),s?.cache){let e=globalThis.indexedDB.open("store",1);e.onerror=()=>{console.error(`IndexedDB error: ${e.error}`)},e.onsuccess=()=>{this.db=e.result,this.initializeFromIndexedDB()},e.onupgradeneeded=n=>{this.db=e.result,n.newVersion===1&&this.db.createObjectStore("cachedData"),this.initializeFromIndexedDB()}}o&&l(this.options.storeName,this.state,this.state.getValue())}setState(t,s){let e=s(this.state.getValue());this.state.next(e),this.options?.cache&&this.sendToIndexedDB(this.state.getValue()),o&&c(this.options.storeName,t,e)}async initializeFromIndexedDB(){let t=this.db.transaction("cachedData").objectStore("cachedData").get(this.options.storeName);t.onsuccess=()=>{let s=t.result;s?this.state.next(s):this.sendToIndexedDB(this.state.getValue())}}async sendToIndexedDB(t){this.db.transaction("cachedData","readwrite").objectStore("cachedData").put(t,this.options.storeName)}};export{a as Store,h as enableReduxDevtools,o as isDevtoolEnabled};
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
interface Options {
|
|
3
|
+
storeName: string;
|
|
4
|
+
cache?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare class Store<T = any> {
|
|
7
|
+
private options;
|
|
8
|
+
private state;
|
|
9
|
+
readonly select$: Observable<T>;
|
|
10
|
+
private db?;
|
|
11
|
+
constructor(initialState: T, options?: Partial<Options>);
|
|
12
|
+
setState(actionName: string, mutationFn: (state: T) => T): void;
|
|
13
|
+
private initializeFromIndexedDB;
|
|
14
|
+
private sendToIndexedDB;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "fluxie",
|
|
3
|
+
"description": "Small helper class to easily implement Flux architecture using angular service, complete with redux devtool and caching support",
|
|
4
|
+
"authors": "Anthony Lenglet",
|
|
5
|
+
"version": "1.0.0-lts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Anthony Lenglet",
|
|
10
|
+
"url": "https://github.com/anthonylenglet"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"homepage": "https://github.com/LesFabricants/Fluxie",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/LesFabricants/Fluxie.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"angular",
|
|
20
|
+
"flux",
|
|
21
|
+
"redux",
|
|
22
|
+
"devtool",
|
|
23
|
+
"caching"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "node esbuild.config.js && ./node_modules/.bin/tsc -p tsconfig.json"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"rxjs": ">=5.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"esbuild": "^0.21.4",
|
|
33
|
+
"typescript": "^5.4.5"
|
|
34
|
+
}
|
|
35
|
+
}
|