@figliolia/galena 2.3.5 → 3.0.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 +172 -476
- package/dist/Galena.cjs +102 -0
- package/dist/Galena.d.cts +80 -0
- package/dist/Galena.d.cts.map +1 -0
- package/dist/Galena.d.mts +80 -0
- package/dist/Galena.d.mts.map +1 -0
- package/dist/Galena.mjs +103 -0
- package/dist/Galena.mjs.map +1 -0
- package/dist/GalenaProvider.cjs +50 -0
- package/dist/GalenaProvider.d.cts +35 -0
- package/dist/GalenaProvider.d.cts.map +1 -0
- package/dist/GalenaProvider.d.mts +35 -0
- package/dist/GalenaProvider.d.mts.map +1 -0
- package/dist/GalenaProvider.mjs +52 -0
- package/dist/GalenaProvider.mjs.map +1 -0
- package/dist/Logger.cjs +46 -0
- package/dist/Logger.d.cts +35 -0
- package/dist/Logger.d.cts.map +1 -0
- package/dist/Logger.d.mts +35 -0
- package/dist/Logger.d.mts.map +1 -0
- package/dist/Logger.mjs +48 -0
- package/dist/Logger.mjs.map +1 -0
- package/dist/Middleware.cjs +62 -0
- package/dist/Middleware.d.cts +65 -0
- package/dist/Middleware.d.cts.map +1 -0
- package/dist/Middleware.d.mts +65 -0
- package/dist/Middleware.d.mts.map +1 -0
- package/dist/Middleware.mjs +64 -0
- package/dist/Middleware.mjs.map +1 -0
- package/dist/Profiler.cjs +41 -0
- package/dist/Profiler.d.cts +31 -0
- package/dist/Profiler.d.cts.map +1 -0
- package/dist/Profiler.d.mts +31 -0
- package/dist/Profiler.d.mts.map +1 -0
- package/dist/Profiler.mjs +43 -0
- package/dist/Profiler.mjs.map +1 -0
- package/dist/State.cjs +147 -0
- package/dist/State.d.cts +114 -0
- package/dist/State.d.cts.map +1 -0
- package/dist/State.d.mts +114 -0
- package/dist/State.d.mts.map +1 -0
- package/dist/State.mjs +148 -0
- package/dist/State.mjs.map +1 -0
- package/dist/commonHooks.cjs +17 -0
- package/dist/commonHooks.mjs +18 -0
- package/dist/commonHooks.mjs.map +1 -0
- package/dist/connect-multi.cjs +42 -0
- package/dist/connect-multi.d.cts +10 -0
- package/dist/connect-multi.d.cts.map +1 -0
- package/dist/connect-multi.d.mts +10 -0
- package/dist/connect-multi.d.mts.map +1 -0
- package/dist/connect-multi.mjs +44 -0
- package/dist/connect-multi.mjs.map +1 -0
- package/dist/connect.cjs +41 -0
- package/dist/connect.d.cts +9 -0
- package/dist/connect.d.cts.map +1 -0
- package/dist/connect.d.mts +9 -0
- package/dist/connect.d.mts.map +1 -0
- package/dist/connect.mjs +43 -0
- package/dist/connect.mjs.map +1 -0
- package/dist/createUseState.cjs +47 -0
- package/dist/createUseState.d.cts +46 -0
- package/dist/createUseState.d.cts.map +1 -0
- package/dist/createUseState.d.mts +46 -0
- package/dist/createUseState.d.mts.map +1 -0
- package/dist/createUseState.mjs +49 -0
- package/dist/createUseState.mjs.map +1 -0
- package/dist/index.cjs +23 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +11 -0
- package/dist/types.d.cts +23 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +23 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/useState.cjs +45 -0
- package/dist/useState.d.cts +43 -0
- package/dist/useState.d.cts.map +1 -0
- package/dist/useState.d.mts +43 -0
- package/dist/useState.d.mts.map +1 -0
- package/dist/useState.mjs +47 -0
- package/dist/useState.mjs.map +1 -0
- package/media/Logging.png +0 -0
- package/media/Profiling.png +0 -0
- package/package.json +38 -59
- package/src/Galena.ts +120 -0
- package/src/{Middlewares/Logger.ts → Logger.ts} +15 -14
- package/src/Middleware.ts +62 -0
- package/src/Profiler.ts +53 -0
- package/src/State.ts +167 -0
- package/src/index.ts +6 -3
- package/src/types.ts +28 -0
- package/dist/cjs/Galena/Galena.js +0 -223
- package/dist/cjs/Galena/Guards.js +0 -40
- package/dist/cjs/Galena/Scheduler.js +0 -84
- package/dist/cjs/Galena/State.js +0 -314
- package/dist/cjs/Galena/index.js +0 -22
- package/dist/cjs/Galena/types.js +0 -9
- package/dist/cjs/Middleware/Middleware.js +0 -46
- package/dist/cjs/Middleware/index.js +0 -20
- package/dist/cjs/Middleware/types.js +0 -8
- package/dist/cjs/Middlewares/Logger.js +0 -51
- package/dist/cjs/Middlewares/Profiler.js +0 -38
- package/dist/cjs/Middlewares/index.js +0 -7
- package/dist/cjs/index.js +0 -19
- package/dist/cjs/package.json +0 -3
- package/dist/mjs/Galena/Galena.js +0 -218
- package/dist/mjs/Galena/Guards.js +0 -36
- package/dist/mjs/Galena/Scheduler.js +0 -79
- package/dist/mjs/Galena/State.js +0 -313
- package/dist/mjs/Galena/index.js +0 -3
- package/dist/mjs/Galena/types.js +0 -6
- package/dist/mjs/Middleware/Middleware.js +0 -42
- package/dist/mjs/Middleware/index.js +0 -2
- package/dist/mjs/Middleware/types.js +0 -5
- package/dist/mjs/Middlewares/Logger.js +0 -44
- package/dist/mjs/Middlewares/Profiler.js +0 -35
- package/dist/mjs/Middlewares/index.js +0 -2
- package/dist/mjs/index.js +0 -3
- package/dist/mjs/package.json +0 -4
- package/dist/types/Galena/Galena.d.ts +0 -160
- package/dist/types/Galena/Guards.d.ts +0 -29
- package/dist/types/Galena/Scheduler.d.ts +0 -51
- package/dist/types/Galena/State.d.ts +0 -235
- package/dist/types/Galena/index.d.ts +0 -3
- package/dist/types/Galena/types.d.ts +0 -13
- package/dist/types/Middleware/Middleware.d.ts +0 -43
- package/dist/types/Middleware/index.d.ts +0 -2
- package/dist/types/Middleware/types.d.ts +0 -4
- package/dist/types/Middlewares/Logger.d.ts +0 -27
- package/dist/types/Middlewares/Profiler.d.ts +0 -22
- package/dist/types/Middlewares/index.d.ts +0 -2
- package/dist/types/index.d.ts +0 -3
- package/src/Galena/Galena.ts +0 -252
- package/src/Galena/Guards.ts +0 -49
- package/src/Galena/Scheduler.ts +0 -85
- package/src/Galena/State.ts +0 -344
- package/src/Galena/index.ts +0 -3
- package/src/Galena/types.ts +0 -18
- package/src/Middleware/Middleware.ts +0 -45
- package/src/Middleware/index.ts +0 -2
- package/src/Middleware/types.ts +0 -4
- package/src/Middlewares/Profiler.ts +0 -41
- package/src/Middlewares/index.ts +0 -2
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import type { Task } from "./types";
|
|
2
|
-
import { Priority } from "./types";
|
|
3
|
-
/**
|
|
4
|
-
* Scheduler
|
|
5
|
-
*
|
|
6
|
-
* Scheduling dispatched events to state consumers is how Galena
|
|
7
|
-
* out-performs just about every state management library out there.
|
|
8
|
-
* The scheduler offers the ability to dispatch state updates on 3
|
|
9
|
-
* priorities:
|
|
10
|
-
*
|
|
11
|
-
* 1. Immediate - Immediate synchronous task execution and propagation of
|
|
12
|
-
* changes to consumers
|
|
13
|
-
* 2. Microtask - Immediate task execution and scheduled propagation of
|
|
14
|
-
* changes to consumers
|
|
15
|
-
* 3. Batched - Immediate task execution and batched propagation of
|
|
16
|
-
* changes to consumers
|
|
17
|
-
*
|
|
18
|
-
* This module manages the propagation of changes to State consumers
|
|
19
|
-
* by implementing the three priorities outlined above
|
|
20
|
-
*/
|
|
21
|
-
export declare class Scheduler<T extends Task = Task> {
|
|
22
|
-
private task;
|
|
23
|
-
private schedule;
|
|
24
|
-
constructor();
|
|
25
|
-
/**
|
|
26
|
-
* Schedule Task
|
|
27
|
-
*
|
|
28
|
-
* Given a task (the emission of state changes to consumers)
|
|
29
|
-
* and a priority, this method executes the task on the priority
|
|
30
|
-
* level specified
|
|
31
|
-
*/
|
|
32
|
-
protected scheduleTask(task: T, priority: Priority): void | Promise<void>;
|
|
33
|
-
/**
|
|
34
|
-
* Create Schedule
|
|
35
|
-
*
|
|
36
|
-
* Schedules the execution of the current task after 5 milliseconds
|
|
37
|
-
*/
|
|
38
|
-
private createSchedule;
|
|
39
|
-
/**
|
|
40
|
-
* Clear Schedule
|
|
41
|
-
*
|
|
42
|
-
* Clears the schedule if it exists
|
|
43
|
-
*/
|
|
44
|
-
private clearSchedule;
|
|
45
|
-
/**
|
|
46
|
-
* Execute Tasks
|
|
47
|
-
*
|
|
48
|
-
* Clears the schedule if it exists and executes the current task
|
|
49
|
-
*/
|
|
50
|
-
private executeTasks;
|
|
51
|
-
}
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import type { Middleware } from "../Middleware/Middleware";
|
|
2
|
-
import { Scheduler } from "./Scheduler";
|
|
3
|
-
import type { Subscription } from "./types";
|
|
4
|
-
import { Priority } from "./types";
|
|
5
|
-
/**
|
|
6
|
-
* ### State
|
|
7
|
-
*
|
|
8
|
-
* The root of all reactivity in Galena. State instances can
|
|
9
|
-
* operate in isolation by calling `new State(...args)` or as
|
|
10
|
-
* part of your application's larger global state by using
|
|
11
|
-
* `new Galena().composeState()`.
|
|
12
|
-
*
|
|
13
|
-
* `State` instances operate on the premise of pub-sub and mutability.
|
|
14
|
-
* This provides significant performance improvement over more traditional
|
|
15
|
-
* state management tools because
|
|
16
|
-
*
|
|
17
|
-
* 1. Mutations can occur in O(1) space
|
|
18
|
-
* 2. Mutations can be batched when dispatching updates to subscribers
|
|
19
|
-
*
|
|
20
|
-
* When deciding how many `State` instances are required for your
|
|
21
|
-
* applications needs, we suggest creating and organizing state in
|
|
22
|
-
* accordance with your application logic. Meaning, you might have a
|
|
23
|
-
* `State` instance for navigation/routing, another `State` instance
|
|
24
|
-
* for storing user information, and so on. Performance can improve
|
|
25
|
-
* significantly when state is dispersed amongst multiple instances
|
|
26
|
-
*
|
|
27
|
-
* #### Creating State Instances
|
|
28
|
-
*
|
|
29
|
-
* ```typescript
|
|
30
|
-
* const MyState = new State("MyState", {
|
|
31
|
-
* someData: true,
|
|
32
|
-
* listItems: [1, 2, 3, 4];
|
|
33
|
-
* // ...etc
|
|
34
|
-
* });
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* #### Updating State
|
|
38
|
-
* ##### Synchronous updates
|
|
39
|
-
* ```typescript
|
|
40
|
-
* MyState.update((state) => {
|
|
41
|
-
* state.listItems.push(5);
|
|
42
|
-
* });
|
|
43
|
-
* ```
|
|
44
|
-
* ##### Asynchronous updates
|
|
45
|
-
* ```typescript
|
|
46
|
-
* MyState.update(async (state) => {
|
|
47
|
-
* const listItems = await fetch("/list-items");
|
|
48
|
-
* state.listItems = listItems;
|
|
49
|
-
* });
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* #### Subscribing to State Changes
|
|
53
|
-
* ```typescript
|
|
54
|
-
* MyState.subscribe((state) => {
|
|
55
|
-
* const { listItems } = state
|
|
56
|
-
* // Do something with your list items!
|
|
57
|
-
* });
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
export declare class State<T extends any = any> extends Scheduler {
|
|
61
|
-
state: T;
|
|
62
|
-
readonly name: string;
|
|
63
|
-
readonly initialState: T;
|
|
64
|
-
private readonly middleware;
|
|
65
|
-
private readonly emitter;
|
|
66
|
-
constructor(name: string, initialState: T);
|
|
67
|
-
/**
|
|
68
|
-
* Get State
|
|
69
|
-
*
|
|
70
|
-
* Returns a readonly snapshot of the current state
|
|
71
|
-
*/
|
|
72
|
-
getState(): Readonly<T>;
|
|
73
|
-
/**
|
|
74
|
-
* Update
|
|
75
|
-
*
|
|
76
|
-
* Mutates state and notifies any open subscriptions. This method
|
|
77
|
-
* by default uses task batching for optimized performance. In almost
|
|
78
|
-
* every use-case, this method is the correct way to mutate state. If
|
|
79
|
-
* you need to bypass batching for higher-priority state updates, you
|
|
80
|
-
* can use `State.priorityUpdate()` or `State.backgroundUpdate()`
|
|
81
|
-
*
|
|
82
|
-
* ##### Synchronous updates
|
|
83
|
-
* ```typescript
|
|
84
|
-
* MyState.update((state, initialState) => {
|
|
85
|
-
* state.listItems.push(5);
|
|
86
|
-
* });
|
|
87
|
-
* ```
|
|
88
|
-
* ##### Asynchronous updates
|
|
89
|
-
* ```typescript
|
|
90
|
-
* MyState.update(async (state, initialState) => {
|
|
91
|
-
* const listItems = await fetch("/list-items");
|
|
92
|
-
* state.listItems = listItems;
|
|
93
|
-
* });
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
update: (func: (state: T, initialState: T) => void | Promise<void>) => any;
|
|
97
|
-
/**
|
|
98
|
-
* Background Update
|
|
99
|
-
*
|
|
100
|
-
* Mutates state and notifies any open subscriptions. This method
|
|
101
|
-
* bypasses Galena's internal task batching for a more immediate
|
|
102
|
-
* state update and propagation of state to consumers. It utilizes
|
|
103
|
-
* a micro-task that allows for the current call stack to clear
|
|
104
|
-
* ahead of propagating state updates to consumers
|
|
105
|
-
*
|
|
106
|
-
* ##### Synchronous updates
|
|
107
|
-
* ```typescript
|
|
108
|
-
* MyState.backgroundUpdate((state, initialState) => {
|
|
109
|
-
* state.listItems.push(5);
|
|
110
|
-
* });
|
|
111
|
-
* ```
|
|
112
|
-
* ##### Asynchronous updates
|
|
113
|
-
* ```typescript
|
|
114
|
-
* MyState.backgroundUpdate(async (state, initialState) => {
|
|
115
|
-
* const listItems = await fetch("/list-items");
|
|
116
|
-
* state.listItems = listItems;
|
|
117
|
-
* });
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
backgroundUpdate: (func: (state: T, initialState: T) => void | Promise<void>) => any;
|
|
121
|
-
/**
|
|
122
|
-
* Priority Update
|
|
123
|
-
*
|
|
124
|
-
* Mutates state and notifies any open subscriptions. This method
|
|
125
|
-
* bypasses optimizations for task batching and scheduling. This means
|
|
126
|
-
* that state updates made with this method propagate to subscriptions
|
|
127
|
-
* as immediately as possible. Overusing this method can cause your
|
|
128
|
-
* state updates to perform slower in certain cases. The usage of this
|
|
129
|
-
* method should be conserved for state mutations that need to occur
|
|
130
|
-
* at a certain frame rate
|
|
131
|
-
*
|
|
132
|
-
* ##### Synchronous updates
|
|
133
|
-
* ```typescript
|
|
134
|
-
* MyState.priorityUpdate((state, initialState) => {
|
|
135
|
-
* state.listItems.push(5);
|
|
136
|
-
* });
|
|
137
|
-
* ```
|
|
138
|
-
* ##### Asynchronous updates
|
|
139
|
-
* ```typescript
|
|
140
|
-
* MyState.priorityUpdate(async (state, initialState) => {
|
|
141
|
-
* const listItems = await fetch("/list-items");
|
|
142
|
-
* state.listItems = listItems;
|
|
143
|
-
* });
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
priorityUpdate: (func: (state: T, initialState: T) => void | Promise<void>) => any;
|
|
147
|
-
/**
|
|
148
|
-
* Reset
|
|
149
|
-
*
|
|
150
|
-
* Resets the current state to its initial state
|
|
151
|
-
*/
|
|
152
|
-
reset: () => any;
|
|
153
|
-
/**
|
|
154
|
-
* Mutation
|
|
155
|
-
*
|
|
156
|
-
* This method can be used to wrap arbitrary functions that when invoked
|
|
157
|
-
* will:
|
|
158
|
-
* 1. Notify your subscriptions with the latest state
|
|
159
|
-
* 2. Execute any registered middleware (such as loggers or profiling tools)
|
|
160
|
-
*
|
|
161
|
-
* Using this method, developers can compose and extend `Galena`'s internal
|
|
162
|
-
* infrastructure for state mutations to create proprietary models for your
|
|
163
|
-
* state
|
|
164
|
-
*
|
|
165
|
-
* ```typescript
|
|
166
|
-
* import { State } from "@figliolia/galena";
|
|
167
|
-
*
|
|
168
|
-
* // Extend of Galena State
|
|
169
|
-
* class MyState extends State {
|
|
170
|
-
* addListItem = mutation((newListItem) => {
|
|
171
|
-
* this.state.list.push(newListItem);
|
|
172
|
-
* });
|
|
173
|
-
* }
|
|
174
|
-
*
|
|
175
|
-
* // Create an instance
|
|
176
|
-
* const myState = new MyState("myState", { list: [] });
|
|
177
|
-
*
|
|
178
|
-
* // Invoke your custom mutation method
|
|
179
|
-
* myState.addListItem("new-item");
|
|
180
|
-
* ```
|
|
181
|
-
*/
|
|
182
|
-
protected mutation<F extends (...args: any[]) => any>(func: F, priority?: Priority): (...args: Parameters<F>) => any;
|
|
183
|
-
/**
|
|
184
|
-
* Schedule Update
|
|
185
|
-
*
|
|
186
|
-
* Schedules an update to State subscribers and emits the
|
|
187
|
-
* `onUpdate` lifecycle hook
|
|
188
|
-
*/
|
|
189
|
-
private scheduleUpdate;
|
|
190
|
-
/**
|
|
191
|
-
* Register Middleware
|
|
192
|
-
*
|
|
193
|
-
* Caches a `Middleware` instance and invokes its
|
|
194
|
-
* lifecycle subscriptions on all state transitions
|
|
195
|
-
*/
|
|
196
|
-
registerMiddleware(...middleware: Middleware[]): void;
|
|
197
|
-
/**
|
|
198
|
-
* Subscribe
|
|
199
|
-
*
|
|
200
|
-
* Registers a subscription on the state instance. The
|
|
201
|
-
* callback you provide will execute each time state changes.
|
|
202
|
-
* Returns a unique identifier for your subscription
|
|
203
|
-
*/
|
|
204
|
-
subscribe(callback: Subscription<T>): string;
|
|
205
|
-
/**
|
|
206
|
-
* Unsubscribe
|
|
207
|
-
*
|
|
208
|
-
* Given a subscription ID, removes a registered subscription
|
|
209
|
-
* from the `State` instance
|
|
210
|
-
*/
|
|
211
|
-
unsubscribe(ID: string): boolean | undefined;
|
|
212
|
-
/**
|
|
213
|
-
* Clear All Subscriptions
|
|
214
|
-
*
|
|
215
|
-
* Removes all open subscriptions to the `State` instance
|
|
216
|
-
*/
|
|
217
|
-
clearAllSubscriptions(): void;
|
|
218
|
-
/**
|
|
219
|
-
* Life Cycle Event
|
|
220
|
-
*
|
|
221
|
-
* Triggers a life cycle event for each registered middleware
|
|
222
|
-
*/
|
|
223
|
-
private lifeCycleEvent;
|
|
224
|
-
/**
|
|
225
|
-
* Clone
|
|
226
|
-
*
|
|
227
|
-
* `State` instances accept any value as a form of reactive
|
|
228
|
-
* state. In order to maintain the initial state past any state
|
|
229
|
-
* transitions, this method clones the initial values provided
|
|
230
|
-
* to the `State` constructor and caches them to allow for
|
|
231
|
-
* developers to easily reset their current state back to its
|
|
232
|
-
* initial value
|
|
233
|
-
*/
|
|
234
|
-
static clone<T>(state: T): T;
|
|
235
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Callback } from "@figliolia/event-emitter";
|
|
2
|
-
import type { State } from "./State";
|
|
3
|
-
export type MutationEvent<T extends any> = {
|
|
4
|
-
[key: State<T>["name"]]: T;
|
|
5
|
-
};
|
|
6
|
-
export declare enum Priority {
|
|
7
|
-
"IMMEDIATE" = 1,
|
|
8
|
-
"MICROTASK" = 2,
|
|
9
|
-
"BATCHED" = 3
|
|
10
|
-
}
|
|
11
|
-
export type Task = () => void;
|
|
12
|
-
export type SubscriptionTuple = [state: string, ID: string];
|
|
13
|
-
export type Subscription<T> = Callback<[nextState: T]>;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { State } from "../Galena/State";
|
|
2
|
-
/**
|
|
3
|
-
* # Middleware
|
|
4
|
-
*
|
|
5
|
-
* A root interface for all `Galena` Middleware. When creating
|
|
6
|
-
* a middleware for your `Galena` state, simply extend this
|
|
7
|
-
* class any override any of its public lifecycle methods.
|
|
8
|
-
*
|
|
9
|
-
* ### Creating a Profiling Middleware
|
|
10
|
-
*
|
|
11
|
-
* ```typescript
|
|
12
|
-
* export class ProfilerMiddleware extends Middleware {
|
|
13
|
-
* updateState: number | null = null;
|
|
14
|
-
*
|
|
15
|
-
* override onBeforeUpdate(state: State) {
|
|
16
|
-
* this.updateStart = performance.now();
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* override onUpdate(state: State) {
|
|
20
|
-
* if(this.updateStart) {
|
|
21
|
-
* const timeToUpdate = performance.now() - this.updateStart;
|
|
22
|
-
* if(timeToUpdate > 16) {
|
|
23
|
-
* console.warn("A state transition took more than 16 milliseconds!", State);
|
|
24
|
-
* }
|
|
25
|
-
* }
|
|
26
|
-
* }
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export declare class Middleware<T extends any = any> {
|
|
31
|
-
/**
|
|
32
|
-
* On Before Update
|
|
33
|
-
*
|
|
34
|
-
* An event emitted each time a `State` mutation is enqueued
|
|
35
|
-
*/
|
|
36
|
-
onBeforeUpdate(_state: State<T>): void;
|
|
37
|
-
/**
|
|
38
|
-
* On Update
|
|
39
|
-
*
|
|
40
|
-
* An event emitted each time a `State` instance is mutated
|
|
41
|
-
*/
|
|
42
|
-
onUpdate(_state: State<T>): void;
|
|
43
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { State } from "../Galena/State";
|
|
2
|
-
import { Middleware } from "../Middleware/Middleware";
|
|
3
|
-
/**
|
|
4
|
-
* Logger
|
|
5
|
-
*
|
|
6
|
-
* A middleware for Redux-style logging! Each state transition
|
|
7
|
-
* will log to the console the `State` instance that changed
|
|
8
|
-
* along with a before and after snapshot of the current state:
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* const State = new Galena([new Logger()]);
|
|
12
|
-
* // if using isolated state instances:
|
|
13
|
-
* const MyState = new State(...args);
|
|
14
|
-
* MyState.registerMiddleware(new Logger())
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
export declare class Logger extends Middleware {
|
|
18
|
-
private previousState;
|
|
19
|
-
onBeforeUpdate(state: State): void;
|
|
20
|
-
onUpdate(state: State): void;
|
|
21
|
-
/**
|
|
22
|
-
* Time
|
|
23
|
-
*
|
|
24
|
-
* Returns the time in which a given state transition completed
|
|
25
|
-
*/
|
|
26
|
-
private get time();
|
|
27
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { State } from "../Galena/State";
|
|
2
|
-
import { Middleware } from "../Middleware/Middleware";
|
|
3
|
-
/**
|
|
4
|
-
* Profiler
|
|
5
|
-
*
|
|
6
|
-
* A logger for state transitions exceeding a given threshold
|
|
7
|
-
* for duration:
|
|
8
|
-
*
|
|
9
|
-
* ```typescript
|
|
10
|
-
* const State = new Galena([new Profiler()]);
|
|
11
|
-
* // if using isolated state instances:
|
|
12
|
-
* const MyState = new State(...args);
|
|
13
|
-
* MyState.registerMiddleware(new Profiler())
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export declare class Profiler extends Middleware {
|
|
17
|
-
private threshold;
|
|
18
|
-
private startTime;
|
|
19
|
-
constructor(threshold?: number);
|
|
20
|
-
onBeforeUpdate(_: State): void;
|
|
21
|
-
onUpdate(nextState: State): void;
|
|
22
|
-
}
|
package/dist/types/index.d.ts
DELETED
package/src/Galena/Galena.ts
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { AutoIncrementingID } from "@figliolia/event-emitter";
|
|
2
|
-
import { State } from "Galena/State";
|
|
3
|
-
import type { Middleware } from "Middleware/Middleware";
|
|
4
|
-
import { Guards } from "./Guards";
|
|
5
|
-
import type { Subscription, SubscriptionTuple } from "./types";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* ## Galena
|
|
9
|
-
*
|
|
10
|
-
* A performant global state solution that scales
|
|
11
|
-
*
|
|
12
|
-
* ### Creating State
|
|
13
|
-
*
|
|
14
|
-
* ```typescript
|
|
15
|
-
* // AppState.ts
|
|
16
|
-
* import { Galena } from "@figliolia/galena";
|
|
17
|
-
*
|
|
18
|
-
* const AppState = new Galena([...middleware]);
|
|
19
|
-
*
|
|
20
|
-
* const NavigationState = AppState.composeState("navigation", {
|
|
21
|
-
* currentRoute: "/",
|
|
22
|
-
* userID: "12345",
|
|
23
|
-
* permittedRoutes: ["/*"]
|
|
24
|
-
* });
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* ### Subscribing to State Changes
|
|
28
|
-
* #### Using the Galena Instance
|
|
29
|
-
* ```typescript
|
|
30
|
-
* import { AppState } from "./AppState";
|
|
31
|
-
*
|
|
32
|
-
* AppState.subscribe(appState => {
|
|
33
|
-
* const navState = appState.get("navigation");
|
|
34
|
-
* const { currentRoute } = navState.state;
|
|
35
|
-
* // do something with state changes!
|
|
36
|
-
* });
|
|
37
|
-
* ```
|
|
38
|
-
* #### Using the State Instance
|
|
39
|
-
* ```typescript
|
|
40
|
-
* NavigationState.subscribe(navigation => {
|
|
41
|
-
* const { currentRoute } = navigation
|
|
42
|
-
* // do something with state changes!
|
|
43
|
-
* });
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* #### Using Global Subscriptions
|
|
47
|
-
* ```typescript
|
|
48
|
-
* NavigationState.subscribeAll(nextState => {
|
|
49
|
-
* const { currentRoute } = nextState.navigation
|
|
50
|
-
* // do something with state changes!
|
|
51
|
-
* });
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* ### Mutating State
|
|
55
|
-
* ```typescript
|
|
56
|
-
* NavigationState.update(state => {
|
|
57
|
-
* state.currentRoute = "/profile";
|
|
58
|
-
* // You can mutate state without creating new objects!
|
|
59
|
-
* });
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
export class Galena<
|
|
63
|
-
T extends Record<string, State<any>> = Record<string, State<any>>,
|
|
64
|
-
> extends Guards {
|
|
65
|
-
public readonly state = {} as T;
|
|
66
|
-
private readonly middleware: Middleware[] = [];
|
|
67
|
-
private readonly IDs = new AutoIncrementingID();
|
|
68
|
-
private readonly subscriptions = new Map<string, SubscriptionTuple[]>();
|
|
69
|
-
constructor(middleware: Middleware[] = []) {
|
|
70
|
-
super();
|
|
71
|
-
this.middleware = middleware;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Compose State
|
|
76
|
-
*
|
|
77
|
-
* Creates a new `State` instance and returns it. Your new state
|
|
78
|
-
* becomes immediately available on your `Galena` instance and
|
|
79
|
-
* is wired into your middleware. All existing subscriptions to
|
|
80
|
-
* state will automatically receive updates when your new unit of
|
|
81
|
-
* state updates
|
|
82
|
-
*/
|
|
83
|
-
public composeState<S extends Record<string, any>, M extends typeof State<S>>(
|
|
84
|
-
name: string,
|
|
85
|
-
initialState: S,
|
|
86
|
-
Model?: M,
|
|
87
|
-
) {
|
|
88
|
-
this.guardDuplicateStates(name, this.state);
|
|
89
|
-
const StateModel = Model || State<S>;
|
|
90
|
-
const state = new StateModel(name, initialState);
|
|
91
|
-
state.registerMiddleware(...this.middleware);
|
|
92
|
-
this.mutable[name] = state;
|
|
93
|
-
this.reIndexSubscriptions(name);
|
|
94
|
-
return state as InstanceType<M>;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Get State
|
|
99
|
-
*
|
|
100
|
-
* Returns a mutable state instance
|
|
101
|
-
*/
|
|
102
|
-
public getState() {
|
|
103
|
-
return this.state as Readonly<T>;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Get
|
|
108
|
-
*
|
|
109
|
-
* Returns a unit of `State` by name
|
|
110
|
-
*/
|
|
111
|
-
public get<K extends Extract<keyof T, string>>(name: K): T[K] {
|
|
112
|
-
this.warnForUndefinedStates(name, this.state);
|
|
113
|
-
return this.state[name];
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Mutable
|
|
118
|
-
*
|
|
119
|
-
* Returns a mutable state instance
|
|
120
|
-
*/
|
|
121
|
-
private get mutable() {
|
|
122
|
-
return this.state as Record<string, State>;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Update
|
|
127
|
-
*
|
|
128
|
-
* Runs a mutation on the specified unit of state
|
|
129
|
-
*/
|
|
130
|
-
public update<K extends Extract<keyof T, string>>(
|
|
131
|
-
name: K,
|
|
132
|
-
mutation: Parameters<T[K]["update"]>["0"],
|
|
133
|
-
) {
|
|
134
|
-
return this.get(name).update(mutation);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Background Update
|
|
139
|
-
*
|
|
140
|
-
* Runs a higher priority mutation on the specified unit of
|
|
141
|
-
* state
|
|
142
|
-
*/
|
|
143
|
-
public backgroundUpdate<K extends Extract<keyof T, string>>(
|
|
144
|
-
name: K,
|
|
145
|
-
mutation: Parameters<T[K]["backgroundUpdate"]>["0"],
|
|
146
|
-
) {
|
|
147
|
-
return this.get(name).backgroundUpdate(mutation);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Priority Update
|
|
152
|
-
*
|
|
153
|
-
* Runs an immediate priority mutation on the specified unit
|
|
154
|
-
* of state
|
|
155
|
-
*/
|
|
156
|
-
public priorityUpdate<K extends Extract<keyof T, string>>(
|
|
157
|
-
name: K,
|
|
158
|
-
mutation: Parameters<T[K]["priorityUpdate"]>["0"],
|
|
159
|
-
) {
|
|
160
|
-
return this.get(name).priorityUpdate(mutation);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Subscribe
|
|
165
|
-
*
|
|
166
|
-
* Given the name of a unit of state, this method registers
|
|
167
|
-
* a subscription on the target state instance. The callback
|
|
168
|
-
* you provide will execute each time state changes. Returns
|
|
169
|
-
* a unique identifier for your subscription. To clean up your
|
|
170
|
-
* subscription, call `Galena.unsubscribe()` with the ID returned
|
|
171
|
-
* by this method
|
|
172
|
-
*/
|
|
173
|
-
public subscribe<K extends Extract<keyof T, string>>(
|
|
174
|
-
name: K,
|
|
175
|
-
callback: Parameters<T[K]["subscribe"]>["0"],
|
|
176
|
-
) {
|
|
177
|
-
return this.get(name).subscribe(callback);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Unsubscribe
|
|
182
|
-
*
|
|
183
|
-
* Given a subscription ID returned from the `subscribe` method,
|
|
184
|
-
* this method removes and cleans up the corresponding subscription
|
|
185
|
-
*/
|
|
186
|
-
public unsubscribe<K extends Extract<keyof T, string>>(name: K, ID: string) {
|
|
187
|
-
return this.get(name).unsubscribe(ID);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Subscribe All
|
|
192
|
-
*
|
|
193
|
-
* Registers a callback on each registered `State` instance and
|
|
194
|
-
* is invoked each time your state changes. Using `Galena`'s
|
|
195
|
-
* `subscribeAll` method, although performant, can be less
|
|
196
|
-
* performant than subscribing directly to a target `State`
|
|
197
|
-
* instance using `Galena.subscribe()`. To clean up your
|
|
198
|
-
* subscription, call `Galena.unsubscribeAll()` with the ID
|
|
199
|
-
* returned
|
|
200
|
-
*/
|
|
201
|
-
public subscribeAll(callback: Subscription<T>) {
|
|
202
|
-
const stateSubscriptions: [state: string, ID: string][] = [];
|
|
203
|
-
for (const key in this.state) {
|
|
204
|
-
stateSubscriptions.push([
|
|
205
|
-
key,
|
|
206
|
-
this.state[key].subscribe(() => {
|
|
207
|
-
void callback(this.state);
|
|
208
|
-
}),
|
|
209
|
-
]);
|
|
210
|
-
}
|
|
211
|
-
const subscriptionID = this.IDs.get();
|
|
212
|
-
this.subscriptions.set(subscriptionID, stateSubscriptions);
|
|
213
|
-
return subscriptionID;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Unsubscribe
|
|
218
|
-
*
|
|
219
|
-
* Given a subscription ID returned from the `subscribeAll()` method,
|
|
220
|
-
* this method removes and cleans up the corresponding subscription
|
|
221
|
-
*/
|
|
222
|
-
public unsubscribeAll(ID: string) {
|
|
223
|
-
const IDs = this.subscriptions.get(ID);
|
|
224
|
-
if (IDs) {
|
|
225
|
-
for (const [state, ID] of IDs) {
|
|
226
|
-
this.state[state].unsubscribe(ID);
|
|
227
|
-
}
|
|
228
|
-
this.subscriptions.delete(ID);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* ReIndex Subscriptions
|
|
234
|
-
*
|
|
235
|
-
* When units of state are created lazily, this method updates
|
|
236
|
-
* each existing subscription to receive mutations occurring on
|
|
237
|
-
* recently created `State` instances that post-date prior
|
|
238
|
-
* subscriptions
|
|
239
|
-
*/
|
|
240
|
-
private reIndexSubscriptions(name: string) {
|
|
241
|
-
for (const [ID, unitSubscriptions] of this.subscriptions) {
|
|
242
|
-
const [stateName, listenerID] = unitSubscriptions[0];
|
|
243
|
-
const subscriptions =
|
|
244
|
-
this.state[stateName]["emitter"].storage.get(stateName);
|
|
245
|
-
const listener = subscriptions?.storage?.get?.(listenerID);
|
|
246
|
-
if (listener) {
|
|
247
|
-
unitSubscriptions.push([name, this.state[name].subscribe(listener)]);
|
|
248
|
-
this.subscriptions.set(ID, unitSubscriptions);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|