@figliolia/galena 3.0.1 → 3.0.3
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 +43 -43
- package/dist/Galena.cjs +49 -18
- package/dist/Galena.d.cts +46 -19
- package/dist/Galena.d.cts.map +1 -1
- package/dist/Galena.d.mts +46 -19
- package/dist/Galena.d.mts.map +1 -1
- package/dist/Galena.mjs +49 -18
- package/dist/Galena.mjs.map +1 -1
- package/dist/State.cjs +13 -18
- package/dist/State.d.cts +13 -18
- package/dist/State.d.cts.map +1 -1
- package/dist/State.d.mts +13 -18
- package/dist/State.d.mts.map +1 -1
- package/dist/State.mjs +13 -18
- package/dist/State.mjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/types.d.cts +3 -2
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +3 -2
- package/dist/types.d.mts.map +1 -1
- package/package.json +9 -3
- package/src/Galena.ts +64 -19
- package/src/State.ts +13 -18
- package/src/types.ts +3 -1
package/README.md
CHANGED
|
@@ -2,9 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
Lightning fast, framework agnostic state, that doesn't glue your state operations to your UI components!
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Basic Usage
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { State, createState } from "@figliolia/galena";
|
|
9
|
+
|
|
10
|
+
const myState = new State(/* any value */);
|
|
11
|
+
// or
|
|
12
|
+
const myState = createState(/* any value */);
|
|
13
|
+
|
|
14
|
+
const currentValue = myState.getSnapshot();
|
|
15
|
+
const subscriber = myState.subscribe(nextValue => {});
|
|
16
|
+
myState.set(/* new value */);
|
|
17
|
+
myState.update(previousValue => /* new value */);
|
|
6
18
|
|
|
7
|
-
|
|
19
|
+
// to reset state back to its original value
|
|
20
|
+
myState.reset();
|
|
21
|
+
|
|
22
|
+
// to unregister the subscription
|
|
23
|
+
subscriber();
|
|
24
|
+
```
|
|
8
25
|
|
|
9
26
|
## Installation
|
|
10
27
|
|
|
@@ -18,19 +35,19 @@ npm i @figliolia/react-galena
|
|
|
18
35
|
|
|
19
36
|
### The State Model
|
|
20
37
|
|
|
21
|
-
The instancable `State` object in Galena is
|
|
38
|
+
The instancable `State` object in Galena is a reactive wrapper around any value. You can use it to apply reactivity to large objects or simple values
|
|
22
39
|
|
|
23
40
|
```typescript
|
|
24
41
|
import { State } from "@figliolia/galena";
|
|
25
42
|
|
|
26
43
|
const MyState = new State(/* any value */, /* middleware */);
|
|
27
44
|
|
|
28
|
-
const
|
|
45
|
+
const subscriber = MyState.subscribe(value => {
|
|
29
46
|
// do something with changed values
|
|
30
47
|
});
|
|
31
48
|
|
|
32
49
|
// to unsubscribe
|
|
33
|
-
|
|
50
|
+
subscriber();
|
|
34
51
|
|
|
35
52
|
MyState.set(/* new value */);
|
|
36
53
|
MyState.update(previousValue => /* new value */);
|
|
@@ -41,7 +58,9 @@ Instances of `State` are ultimately what compose all reactivity in Galena. They
|
|
|
41
58
|
|
|
42
59
|
### The Galena Model
|
|
43
60
|
|
|
44
|
-
|
|
61
|
+
`Galena` objects are designed to "link" multiple instances of `State` together to create a "global" application state.
|
|
62
|
+
|
|
63
|
+
To use it simply define your `State`'s and pass them to a `Galena` instance
|
|
45
64
|
|
|
46
65
|
```typescript
|
|
47
66
|
import { Galena, State } from "@figliolia/galena";
|
|
@@ -55,16 +74,14 @@ const AppState = new Galena(
|
|
|
55
74
|
user: new State({
|
|
56
75
|
userID: "<id>",
|
|
57
76
|
membershipTier: "free",
|
|
58
|
-
friends: ["<
|
|
77
|
+
friends: ["<id-1>", "<id-2>"],
|
|
59
78
|
}),
|
|
60
79
|
// ...and so on
|
|
61
80
|
} /* middleware */,
|
|
62
81
|
);
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
From here, operations on any slice of state are type-aware and operable via a single construct:
|
|
66
82
|
|
|
67
|
-
|
|
83
|
+
// From here, operations on any slice of state are type-aware
|
|
84
|
+
// and operable via a single construct:
|
|
68
85
|
const subscriber = AppState.subscribe(
|
|
69
86
|
({
|
|
70
87
|
state, // The entire state object at the time of change
|
|
@@ -75,23 +92,19 @@ const subscriber = AppState.subscribe(
|
|
|
75
92
|
);
|
|
76
93
|
|
|
77
94
|
// to unsubscribe
|
|
78
|
-
|
|
95
|
+
subscriber();
|
|
79
96
|
|
|
97
|
+
// to access an instance of state
|
|
98
|
+
const UserState = AppState.get("user");
|
|
80
99
|
// to operate
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}));
|
|
85
|
-
|
|
86
|
-
AppState.get("navigation").update(state => ({
|
|
87
|
-
...state,
|
|
88
|
-
navigationMenuOpen: true,
|
|
89
|
-
}));
|
|
100
|
+
UserState.update(state => /* next state */);
|
|
101
|
+
// or
|
|
102
|
+
AppState.update("user", state => /* next state */)
|
|
90
103
|
```
|
|
91
104
|
|
|
92
105
|
### Beyond the Basics
|
|
93
106
|
|
|
94
|
-
####
|
|
107
|
+
#### Modeling Data with Mutations
|
|
95
108
|
|
|
96
109
|
`State` in Galena is designed for extension and instancing - a need that ultimately motivated the library's development.
|
|
97
110
|
|
|
@@ -136,42 +149,29 @@ export class MyGameState extends State<IMyGameState> {
|
|
|
136
149
|
}
|
|
137
150
|
```
|
|
138
151
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
```typescript
|
|
142
|
-
import { MyGameState } from "./MyGameState";
|
|
143
|
-
|
|
144
|
-
const player1 = new MyGameState("<playerID>");
|
|
145
|
-
const player2 = new MyGameState("<playerID>");
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
Each instance has a shared API and defined set of state operations that make for predictable operability
|
|
149
|
-
|
|
150
|
-
This instances or robust models can also be used on your `Galena` instances
|
|
152
|
+
These more "robust" state models assist in standardizing a developer API along with your data models. The models you create are also compatible with your your `Galena` instances:
|
|
151
153
|
|
|
152
154
|
```typescript
|
|
153
|
-
import { Galena
|
|
155
|
+
import { Galena } from "@figliolia/galena";
|
|
154
156
|
import { MyGameState } from "./MyGameState";
|
|
155
157
|
|
|
156
158
|
const MyAppState = new Galena({
|
|
157
|
-
navigation: new State({
|
|
158
|
-
currentScreen: "/",
|
|
159
|
-
navigationMenuOpen: false,
|
|
160
|
-
}),
|
|
161
159
|
player1: new MyGameState(),
|
|
162
160
|
player2: new MyGameState(),
|
|
163
161
|
});
|
|
164
162
|
|
|
165
163
|
// Operate
|
|
166
164
|
MyAppState.get("player1").incrementScore(100);
|
|
167
|
-
MyAppState.get("
|
|
165
|
+
MyAppState.get("player2").raiseLevel();
|
|
168
166
|
```
|
|
169
167
|
|
|
170
168
|
### Middleware
|
|
171
169
|
|
|
172
|
-
Middleware provides a developer API for building out
|
|
170
|
+
Middleware provides a developer API for building out custom tooling for your state.
|
|
171
|
+
|
|
172
|
+
Building middleware is as simple as extending `Galena`'s `Middleware` class and registering on your state.
|
|
173
173
|
|
|
174
|
-
|
|
174
|
+
Here's a quick example using the redux-like logger provided by this package:
|
|
175
175
|
|
|
176
176
|
```typescript
|
|
177
177
|
import { Middleware, type State } from "@figliolia/galena";
|
|
@@ -232,7 +232,7 @@ const MyAppState = new Galena({
|
|
|
232
232
|
// state
|
|
233
233
|
}, new Logger(), new Profiler());
|
|
234
234
|
|
|
235
|
-
// To apply middleware to a single of `State`
|
|
235
|
+
// To apply middleware to a single piece of `State`
|
|
236
236
|
const MyState = new State(
|
|
237
237
|
/* reactive value */,
|
|
238
238
|
new Logger(),
|
package/dist/Galena.cjs
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
1
|
let _figliolia_event_emitter = require("@figliolia/event-emitter");
|
|
2
2
|
//#region src/Galena.ts
|
|
3
3
|
/**
|
|
4
|
-
* Galena
|
|
4
|
+
* ### Galena
|
|
5
5
|
*
|
|
6
|
-
* Galena
|
|
6
|
+
* Galena instances are designed to house one or more units of `State`
|
|
7
7
|
* and exist as a pseudo global application state.
|
|
8
8
|
*
|
|
9
9
|
* By design, each of its `State` units have isolated reactivity
|
|
10
10
|
* that prevents entire state trees from updating when a single
|
|
11
11
|
* unit changes.
|
|
12
12
|
*
|
|
13
|
-
* This is dissimilar to redux-like models where downstream reconciliations
|
|
14
|
-
* will propagate everwhere a given store is read from. In galena, downstream
|
|
15
|
-
* reconciliations occur only for consumers of the slice of state that
|
|
16
|
-
* changed - making it safer to use with more frequent state changes.
|
|
17
|
-
*
|
|
18
13
|
* ```typescript
|
|
19
14
|
* import { Galena } from "@figliolia/galena";
|
|
20
15
|
*
|
|
21
16
|
* const AppState = new Galena({
|
|
17
|
+
* user: new State("<user-stuff>"),
|
|
18
|
+
* business: new State("<business-logic-stuff>")
|
|
22
19
|
* // your reactive instances
|
|
23
20
|
* }, ...middleware);
|
|
24
21
|
*
|
|
@@ -26,8 +23,9 @@ let _figliolia_event_emitter = require("@figliolia/event-emitter");
|
|
|
26
23
|
* const myUnit = AppState.get("<key>"); // Returns State<T>
|
|
27
24
|
*
|
|
28
25
|
* // to run a callback anytime a unit of state changes
|
|
29
|
-
* const
|
|
26
|
+
* const listener = AppState.subscribe(({ state, updated }) => {
|
|
30
27
|
* // do something with the `State` instance that updated
|
|
28
|
+
* // the entirety of your state
|
|
31
29
|
* });
|
|
32
30
|
* ```
|
|
33
31
|
*/
|
|
@@ -37,9 +35,38 @@ var Galena = class {
|
|
|
37
35
|
this.state = state;
|
|
38
36
|
this.registerMiddleware(...middleware);
|
|
39
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Get
|
|
40
|
+
*
|
|
41
|
+
* Returns a connected State instance by key
|
|
42
|
+
*/
|
|
40
43
|
get(key) {
|
|
41
44
|
return this.state[key];
|
|
42
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Set
|
|
48
|
+
*
|
|
49
|
+
* Sets a connected State instance's state by key
|
|
50
|
+
*/
|
|
51
|
+
set(key, value) {
|
|
52
|
+
return this.get(key).set(value);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Update
|
|
56
|
+
*
|
|
57
|
+
* Invokes a connected State instance's update method key
|
|
58
|
+
*/
|
|
59
|
+
update(key, updater) {
|
|
60
|
+
return this.get(key).update(updater);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Subscribe
|
|
64
|
+
*
|
|
65
|
+
* Listen for changes on your Galena instnace. Your provided
|
|
66
|
+
* callback will be invoked each time an attached state instance
|
|
67
|
+
* changes. To your callback will be provided the `updated` state
|
|
68
|
+
* instance, along with the entire `state` tree
|
|
69
|
+
*/
|
|
43
70
|
subscribe = (subscriber) => {
|
|
44
71
|
const ID = this.Emitter.on("change", subscriber);
|
|
45
72
|
const unsubscribers = [];
|
|
@@ -56,6 +83,12 @@ var Galena = class {
|
|
|
56
83
|
while (unsubscribers.length) unsubscribers.pop?.()?.();
|
|
57
84
|
};
|
|
58
85
|
};
|
|
86
|
+
/**
|
|
87
|
+
* Register Middleware
|
|
88
|
+
*
|
|
89
|
+
* Adds middleware instances to each of the connected
|
|
90
|
+
* `State` instances
|
|
91
|
+
*/
|
|
59
92
|
registerMiddleware(...middlewares) {
|
|
60
93
|
for (const key in this.state) this.state[key]?.registerMiddleware?.(...middlewares);
|
|
61
94
|
}
|
|
@@ -64,24 +97,21 @@ var Galena = class {
|
|
|
64
97
|
}
|
|
65
98
|
};
|
|
66
99
|
/**
|
|
67
|
-
*
|
|
100
|
+
* ### createGalena
|
|
68
101
|
*
|
|
69
|
-
* Galena
|
|
102
|
+
* Galena instances are designed to house one or more units of `State`
|
|
70
103
|
* and exist as a pseudo global application state.
|
|
71
104
|
*
|
|
72
105
|
* By design, each of its `State` units have isolated reactivity
|
|
73
106
|
* that prevents entire state trees from updating when a single
|
|
74
107
|
* unit changes.
|
|
75
108
|
*
|
|
76
|
-
* This is dissimilar to redux-like models where downstream reconciliations
|
|
77
|
-
* will propagate everwhere a given store is read from. In galena, downstream
|
|
78
|
-
* reconciliations occur only for consumers of the slice of state that
|
|
79
|
-
* changed - making it safer to use with more frequent state changes.
|
|
80
|
-
*
|
|
81
109
|
* ```typescript
|
|
82
|
-
* import {
|
|
110
|
+
* import { Galena } from "@figliolia/galena";
|
|
83
111
|
*
|
|
84
|
-
* const AppState =
|
|
112
|
+
* const AppState = new Galena({
|
|
113
|
+
* user: new State("<user-stuff>"),
|
|
114
|
+
* business: new State("<business-logic-stuff>")
|
|
85
115
|
* // your reactive instances
|
|
86
116
|
* }, ...middleware);
|
|
87
117
|
*
|
|
@@ -89,8 +119,9 @@ var Galena = class {
|
|
|
89
119
|
* const myUnit = AppState.get("<key>"); // Returns State<T>
|
|
90
120
|
*
|
|
91
121
|
* // to run a callback anytime a unit of state changes
|
|
92
|
-
* const
|
|
122
|
+
* const listener = AppState.subscribe(({ state, updated }) => {
|
|
93
123
|
* // do something with the `State` instance that updated
|
|
124
|
+
* // the entirety of your state
|
|
94
125
|
* });
|
|
95
126
|
* ```
|
|
96
127
|
*/
|
package/dist/Galena.d.cts
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
import { AppSubscriber, StateTypes } from "./types.cjs";
|
|
1
|
+
import { AppSubscriber, Setter, StateType, StateTypes } from "./types.cjs";
|
|
2
2
|
import { State } from "./State.cjs";
|
|
3
3
|
import { Middleware } from "./Middleware.cjs";
|
|
4
4
|
|
|
5
5
|
//#region src/Galena.d.ts
|
|
6
6
|
/**
|
|
7
|
-
* Galena
|
|
7
|
+
* ### Galena
|
|
8
8
|
*
|
|
9
|
-
* Galena
|
|
9
|
+
* Galena instances are designed to house one or more units of `State`
|
|
10
10
|
* and exist as a pseudo global application state.
|
|
11
11
|
*
|
|
12
12
|
* By design, each of its `State` units have isolated reactivity
|
|
13
13
|
* that prevents entire state trees from updating when a single
|
|
14
14
|
* unit changes.
|
|
15
15
|
*
|
|
16
|
-
* This is dissimilar to redux-like models where downstream reconciliations
|
|
17
|
-
* will propagate everwhere a given store is read from. In galena, downstream
|
|
18
|
-
* reconciliations occur only for consumers of the slice of state that
|
|
19
|
-
* changed - making it safer to use with more frequent state changes.
|
|
20
|
-
*
|
|
21
16
|
* ```typescript
|
|
22
17
|
* import { Galena } from "@figliolia/galena";
|
|
23
18
|
*
|
|
24
19
|
* const AppState = new Galena({
|
|
20
|
+
* user: new State("<user-stuff>"),
|
|
21
|
+
* business: new State("<business-logic-stuff>")
|
|
25
22
|
* // your reactive instances
|
|
26
23
|
* }, ...middleware);
|
|
27
24
|
*
|
|
@@ -29,8 +26,9 @@ import { Middleware } from "./Middleware.cjs";
|
|
|
29
26
|
* const myUnit = AppState.get("<key>"); // Returns State<T>
|
|
30
27
|
*
|
|
31
28
|
* // to run a callback anytime a unit of state changes
|
|
32
|
-
* const
|
|
29
|
+
* const listener = AppState.subscribe(({ state, updated }) => {
|
|
33
30
|
* // do something with the `State` instance that updated
|
|
31
|
+
* // the entirety of your state
|
|
34
32
|
* });
|
|
35
33
|
* ```
|
|
36
34
|
*/
|
|
@@ -38,30 +36,58 @@ declare class Galena<T extends Record<string, State<any>>> {
|
|
|
38
36
|
readonly state: T;
|
|
39
37
|
private Emitter;
|
|
40
38
|
constructor(state: T, ...middleware: Middleware<StateTypes<T>>[]);
|
|
39
|
+
/**
|
|
40
|
+
* Get
|
|
41
|
+
*
|
|
42
|
+
* Returns a connected State instance by key
|
|
43
|
+
*/
|
|
41
44
|
get<K extends Extract<keyof T, string>>(key: K): T[K];
|
|
45
|
+
/**
|
|
46
|
+
* Set
|
|
47
|
+
*
|
|
48
|
+
* Sets a connected State instance's state by key
|
|
49
|
+
*/
|
|
50
|
+
set<K extends Extract<keyof T, string>>(key: K, value: StateType<T[K]>): void;
|
|
51
|
+
/**
|
|
52
|
+
* Update
|
|
53
|
+
*
|
|
54
|
+
* Invokes a connected State instance's update method key
|
|
55
|
+
*/
|
|
56
|
+
update<K extends Extract<keyof T, string>>(key: K, updater: Setter<StateType<T[K]>>): void;
|
|
57
|
+
/**
|
|
58
|
+
* Subscribe
|
|
59
|
+
*
|
|
60
|
+
* Listen for changes on your Galena instnace. Your provided
|
|
61
|
+
* callback will be invoked each time an attached state instance
|
|
62
|
+
* changes. To your callback will be provided the `updated` state
|
|
63
|
+
* instance, along with the entire `state` tree
|
|
64
|
+
*/
|
|
42
65
|
subscribe: (subscriber: AppSubscriber<T>) => () => void;
|
|
66
|
+
/**
|
|
67
|
+
* Register Middleware
|
|
68
|
+
*
|
|
69
|
+
* Adds middleware instances to each of the connected
|
|
70
|
+
* `State` instances
|
|
71
|
+
*/
|
|
43
72
|
registerMiddleware(...middlewares: Middleware<StateTypes<T>>[]): void;
|
|
44
73
|
private emit;
|
|
45
74
|
}
|
|
46
75
|
/**
|
|
47
|
-
*
|
|
76
|
+
* ### createGalena
|
|
48
77
|
*
|
|
49
|
-
* Galena
|
|
78
|
+
* Galena instances are designed to house one or more units of `State`
|
|
50
79
|
* and exist as a pseudo global application state.
|
|
51
80
|
*
|
|
52
81
|
* By design, each of its `State` units have isolated reactivity
|
|
53
82
|
* that prevents entire state trees from updating when a single
|
|
54
83
|
* unit changes.
|
|
55
84
|
*
|
|
56
|
-
* This is dissimilar to redux-like models where downstream reconciliations
|
|
57
|
-
* will propagate everwhere a given store is read from. In galena, downstream
|
|
58
|
-
* reconciliations occur only for consumers of the slice of state that
|
|
59
|
-
* changed - making it safer to use with more frequent state changes.
|
|
60
|
-
*
|
|
61
85
|
* ```typescript
|
|
62
|
-
* import {
|
|
86
|
+
* import { Galena } from "@figliolia/galena";
|
|
63
87
|
*
|
|
64
|
-
* const AppState =
|
|
88
|
+
* const AppState = new Galena({
|
|
89
|
+
* user: new State("<user-stuff>"),
|
|
90
|
+
* business: new State("<business-logic-stuff>")
|
|
65
91
|
* // your reactive instances
|
|
66
92
|
* }, ...middleware);
|
|
67
93
|
*
|
|
@@ -69,8 +95,9 @@ declare class Galena<T extends Record<string, State<any>>> {
|
|
|
69
95
|
* const myUnit = AppState.get("<key>"); // Returns State<T>
|
|
70
96
|
*
|
|
71
97
|
* // to run a callback anytime a unit of state changes
|
|
72
|
-
* const
|
|
98
|
+
* const listener = AppState.subscribe(({ state, updated }) => {
|
|
73
99
|
* // do something with the `State` instance that updated
|
|
100
|
+
* // the entirety of your state
|
|
74
101
|
* });
|
|
75
102
|
* ```
|
|
76
103
|
*/
|
package/dist/Galena.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Galena.d.cts","names":[],"sources":["../src/Galena.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"Galena.d.cts","names":[],"sources":["../src/Galena.ts"],"mappings":";;;;;;;AAwCA;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,MAAA,WAAiB,MAAA,SAAe,KAAA;EAAA,SAGzB,KAAA,EAAO,CAAA;EAAA,QAFjB,OAAA;cAEU,KAAA,EAAO,CAAA,KACpB,UAAA,EAAY,UAAA,CAAW,UAAA,CAAW,CAAA;EA0Ec;;;;;EAhE9C,GAAA,WAAc,OAAA,OAAc,CAAA,UAAA,CAAY,GAAA,EAAK,CAAA,GAAC,CAAA,CAAA,CAAA;EAdV;;;;;EAuBpC,GAAA,WAAc,OAAA,OAAc,CAAA,UAAA,CACjC,GAAA,EAAK,CAAA,EACL,KAAA,EAAO,SAAA,CAAU,CAAA,CAAE,CAAA;EAtBH;;;;;EAgCX,MAAA,WAAiB,OAAA,OAAc,CAAA,UAAA,CACpC,GAAA,EAAK,CAAA,EACL,OAAA,EAAS,MAAA,CAAO,SAAA,CAAU,CAAA,CAAE,CAAA;EAvBnB;;;;;;;;EAoCJ,SAAA,GAAa,UAAA,EAAY,aAAA,CAAc,CAAA;EA3BzB;;;;;;EAuDd,kBAAA,CAAA,GAAsB,WAAA,EAAa,UAAA,CAAW,UAAA,CAAW,CAAA;EAAA,QAMxD,IAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;AAoCV;;;;;;;cAAa,YAAA,aAA0B,MAAA,SAAe,KAAA,WACjD,IAAA,EAAM,qBAAA,QAA6B,MAAA,CAAO,CAAA,OAAG,MAAA,CAAA,CAAA"}
|
package/dist/Galena.d.mts
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
import { AppSubscriber, StateTypes } from "./types.mjs";
|
|
1
|
+
import { AppSubscriber, Setter, StateType, StateTypes } from "./types.mjs";
|
|
2
2
|
import { State } from "./State.mjs";
|
|
3
3
|
import { Middleware } from "./Middleware.mjs";
|
|
4
4
|
|
|
5
5
|
//#region src/Galena.d.ts
|
|
6
6
|
/**
|
|
7
|
-
* Galena
|
|
7
|
+
* ### Galena
|
|
8
8
|
*
|
|
9
|
-
* Galena
|
|
9
|
+
* Galena instances are designed to house one or more units of `State`
|
|
10
10
|
* and exist as a pseudo global application state.
|
|
11
11
|
*
|
|
12
12
|
* By design, each of its `State` units have isolated reactivity
|
|
13
13
|
* that prevents entire state trees from updating when a single
|
|
14
14
|
* unit changes.
|
|
15
15
|
*
|
|
16
|
-
* This is dissimilar to redux-like models where downstream reconciliations
|
|
17
|
-
* will propagate everwhere a given store is read from. In galena, downstream
|
|
18
|
-
* reconciliations occur only for consumers of the slice of state that
|
|
19
|
-
* changed - making it safer to use with more frequent state changes.
|
|
20
|
-
*
|
|
21
16
|
* ```typescript
|
|
22
17
|
* import { Galena } from "@figliolia/galena";
|
|
23
18
|
*
|
|
24
19
|
* const AppState = new Galena({
|
|
20
|
+
* user: new State("<user-stuff>"),
|
|
21
|
+
* business: new State("<business-logic-stuff>")
|
|
25
22
|
* // your reactive instances
|
|
26
23
|
* }, ...middleware);
|
|
27
24
|
*
|
|
@@ -29,8 +26,9 @@ import { Middleware } from "./Middleware.mjs";
|
|
|
29
26
|
* const myUnit = AppState.get("<key>"); // Returns State<T>
|
|
30
27
|
*
|
|
31
28
|
* // to run a callback anytime a unit of state changes
|
|
32
|
-
* const
|
|
29
|
+
* const listener = AppState.subscribe(({ state, updated }) => {
|
|
33
30
|
* // do something with the `State` instance that updated
|
|
31
|
+
* // the entirety of your state
|
|
34
32
|
* });
|
|
35
33
|
* ```
|
|
36
34
|
*/
|
|
@@ -38,30 +36,58 @@ declare class Galena<T extends Record<string, State<any>>> {
|
|
|
38
36
|
readonly state: T;
|
|
39
37
|
private Emitter;
|
|
40
38
|
constructor(state: T, ...middleware: Middleware<StateTypes<T>>[]);
|
|
39
|
+
/**
|
|
40
|
+
* Get
|
|
41
|
+
*
|
|
42
|
+
* Returns a connected State instance by key
|
|
43
|
+
*/
|
|
41
44
|
get<K extends Extract<keyof T, string>>(key: K): T[K];
|
|
45
|
+
/**
|
|
46
|
+
* Set
|
|
47
|
+
*
|
|
48
|
+
* Sets a connected State instance's state by key
|
|
49
|
+
*/
|
|
50
|
+
set<K extends Extract<keyof T, string>>(key: K, value: StateType<T[K]>): void;
|
|
51
|
+
/**
|
|
52
|
+
* Update
|
|
53
|
+
*
|
|
54
|
+
* Invokes a connected State instance's update method key
|
|
55
|
+
*/
|
|
56
|
+
update<K extends Extract<keyof T, string>>(key: K, updater: Setter<StateType<T[K]>>): void;
|
|
57
|
+
/**
|
|
58
|
+
* Subscribe
|
|
59
|
+
*
|
|
60
|
+
* Listen for changes on your Galena instnace. Your provided
|
|
61
|
+
* callback will be invoked each time an attached state instance
|
|
62
|
+
* changes. To your callback will be provided the `updated` state
|
|
63
|
+
* instance, along with the entire `state` tree
|
|
64
|
+
*/
|
|
42
65
|
subscribe: (subscriber: AppSubscriber<T>) => () => void;
|
|
66
|
+
/**
|
|
67
|
+
* Register Middleware
|
|
68
|
+
*
|
|
69
|
+
* Adds middleware instances to each of the connected
|
|
70
|
+
* `State` instances
|
|
71
|
+
*/
|
|
43
72
|
registerMiddleware(...middlewares: Middleware<StateTypes<T>>[]): void;
|
|
44
73
|
private emit;
|
|
45
74
|
}
|
|
46
75
|
/**
|
|
47
|
-
*
|
|
76
|
+
* ### createGalena
|
|
48
77
|
*
|
|
49
|
-
* Galena
|
|
78
|
+
* Galena instances are designed to house one or more units of `State`
|
|
50
79
|
* and exist as a pseudo global application state.
|
|
51
80
|
*
|
|
52
81
|
* By design, each of its `State` units have isolated reactivity
|
|
53
82
|
* that prevents entire state trees from updating when a single
|
|
54
83
|
* unit changes.
|
|
55
84
|
*
|
|
56
|
-
* This is dissimilar to redux-like models where downstream reconciliations
|
|
57
|
-
* will propagate everwhere a given store is read from. In galena, downstream
|
|
58
|
-
* reconciliations occur only for consumers of the slice of state that
|
|
59
|
-
* changed - making it safer to use with more frequent state changes.
|
|
60
|
-
*
|
|
61
85
|
* ```typescript
|
|
62
|
-
* import {
|
|
86
|
+
* import { Galena } from "@figliolia/galena";
|
|
63
87
|
*
|
|
64
|
-
* const AppState =
|
|
88
|
+
* const AppState = new Galena({
|
|
89
|
+
* user: new State("<user-stuff>"),
|
|
90
|
+
* business: new State("<business-logic-stuff>")
|
|
65
91
|
* // your reactive instances
|
|
66
92
|
* }, ...middleware);
|
|
67
93
|
*
|
|
@@ -69,8 +95,9 @@ declare class Galena<T extends Record<string, State<any>>> {
|
|
|
69
95
|
* const myUnit = AppState.get("<key>"); // Returns State<T>
|
|
70
96
|
*
|
|
71
97
|
* // to run a callback anytime a unit of state changes
|
|
72
|
-
* const
|
|
98
|
+
* const listener = AppState.subscribe(({ state, updated }) => {
|
|
73
99
|
* // do something with the `State` instance that updated
|
|
100
|
+
* // the entirety of your state
|
|
74
101
|
* });
|
|
75
102
|
* ```
|
|
76
103
|
*/
|
package/dist/Galena.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Galena.d.mts","names":[],"sources":["../src/Galena.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"Galena.d.mts","names":[],"sources":["../src/Galena.ts"],"mappings":";;;;;;;AAwCA;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,MAAA,WAAiB,MAAA,SAAe,KAAA;EAAA,SAGzB,KAAA,EAAO,CAAA;EAAA,QAFjB,OAAA;cAEU,KAAA,EAAO,CAAA,KACpB,UAAA,EAAY,UAAA,CAAW,UAAA,CAAW,CAAA;EA0Ec;;;;;EAhE9C,GAAA,WAAc,OAAA,OAAc,CAAA,UAAA,CAAY,GAAA,EAAK,CAAA,GAAC,CAAA,CAAA,CAAA;EAdV;;;;;EAuBpC,GAAA,WAAc,OAAA,OAAc,CAAA,UAAA,CACjC,GAAA,EAAK,CAAA,EACL,KAAA,EAAO,SAAA,CAAU,CAAA,CAAE,CAAA;EAtBH;;;;;EAgCX,MAAA,WAAiB,OAAA,OAAc,CAAA,UAAA,CACpC,GAAA,EAAK,CAAA,EACL,OAAA,EAAS,MAAA,CAAO,SAAA,CAAU,CAAA,CAAE,CAAA;EAvBnB;;;;;;;;EAoCJ,SAAA,GAAa,UAAA,EAAY,aAAA,CAAc,CAAA;EA3BzB;;;;;;EAuDd,kBAAA,CAAA,GAAsB,WAAA,EAAa,UAAA,CAAW,UAAA,CAAW,CAAA;EAAA,QAMxD,IAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;AAoCV;;;;;;;cAAa,YAAA,aAA0B,MAAA,SAAe,KAAA,WACjD,IAAA,EAAM,qBAAA,QAA6B,MAAA,CAAO,CAAA,OAAG,MAAA,CAAA,CAAA"}
|