@figliolia/galena 2.2.5 → 2.2.7
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/dist/cjs/Galena/Galena.js +9 -17
- package/dist/cjs/Middlewares/Profiler.js +1 -1
- package/dist/mjs/Galena/Galena.js +9 -17
- package/dist/mjs/Middlewares/Profiler.js +1 -1
- package/dist/types/Galena/Galena.d.ts +3 -2
- package/dist/types/Galena/State.d.ts +2 -1
- package/dist/types/Galena/types.d.ts +2 -0
- package/package.json +1 -1
- package/src/Galena/Galena.ts +11 -21
- package/src/Galena/State.ts +2 -1
- package/src/Galena/types.ts +4 -0
- package/src/Middlewares/Profiler.ts +4 -1
|
@@ -63,9 +63,9 @@ class Galena extends Guards_1.Guards {
|
|
|
63
63
|
constructor(middleware = []) {
|
|
64
64
|
super();
|
|
65
65
|
this.state = {};
|
|
66
|
-
this.subscriptions = new Map();
|
|
67
66
|
this.middleware = [];
|
|
68
67
|
this.IDs = new event_emitter_1.AutoIncrementingID();
|
|
68
|
+
this.subscriptions = new Map();
|
|
69
69
|
this.middleware = middleware;
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
@@ -176,7 +176,7 @@ class Galena extends Guards_1.Guards {
|
|
|
176
176
|
stateSubscriptions.push([
|
|
177
177
|
key,
|
|
178
178
|
this.state[key].subscribe(() => {
|
|
179
|
-
callback(this.state);
|
|
179
|
+
void callback(this.state);
|
|
180
180
|
}),
|
|
181
181
|
]);
|
|
182
182
|
}
|
|
@@ -195,8 +195,8 @@ class Galena extends Guards_1.Guards {
|
|
|
195
195
|
if (IDs) {
|
|
196
196
|
for (const [state, ID] of IDs) {
|
|
197
197
|
this.state[state].unsubscribe(ID);
|
|
198
|
-
this.subscriptions.delete(ID);
|
|
199
198
|
}
|
|
199
|
+
this.subscriptions.delete(ID);
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
/**
|
|
@@ -208,21 +208,13 @@ class Galena extends Guards_1.Guards {
|
|
|
208
208
|
* subscriptions
|
|
209
209
|
*/
|
|
210
210
|
reIndexSubscriptions(name) {
|
|
211
|
-
var _a;
|
|
212
211
|
for (const [ID, unitSubscriptions] of this.subscriptions) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
this.state[name].subscribe(() => {
|
|
220
|
-
void callback(this.state);
|
|
221
|
-
}),
|
|
222
|
-
]);
|
|
223
|
-
this.subscriptions.set(ID, unitSubscriptions);
|
|
224
|
-
break;
|
|
225
|
-
}
|
|
212
|
+
const [state, listenerID] = unitSubscriptions[0];
|
|
213
|
+
const subscriptions = this.state[state]["emitter"].get(state);
|
|
214
|
+
const listener = subscriptions === null || subscriptions === void 0 ? void 0 : subscriptions.get(listenerID);
|
|
215
|
+
if (listener) {
|
|
216
|
+
unitSubscriptions.push([name, this.state[name].subscribe(listener)]);
|
|
217
|
+
this.subscriptions.set(ID, unitSubscriptions);
|
|
226
218
|
}
|
|
227
219
|
}
|
|
228
220
|
}
|
|
@@ -29,7 +29,7 @@ class Profiler extends Middleware_1.Middleware {
|
|
|
29
29
|
const endTime = performance.now();
|
|
30
30
|
const diff = endTime - this.startTime;
|
|
31
31
|
if (diff > this.threshold) {
|
|
32
|
-
console.warn(
|
|
32
|
+
console.warn(`A slow state transition was detected on ${nextState.name}`, nextState);
|
|
33
33
|
console.warn(`The last transition took ${diff}ms`);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -58,9 +58,9 @@ import { Guards } from "./Guards.js";
|
|
|
58
58
|
*/
|
|
59
59
|
export class Galena extends Guards {
|
|
60
60
|
state = {};
|
|
61
|
-
subscriptions = new Map();
|
|
62
61
|
middleware = [];
|
|
63
62
|
IDs = new AutoIncrementingID();
|
|
63
|
+
subscriptions = new Map();
|
|
64
64
|
constructor(middleware = []) {
|
|
65
65
|
super();
|
|
66
66
|
this.middleware = middleware;
|
|
@@ -173,7 +173,7 @@ export class Galena extends Guards {
|
|
|
173
173
|
stateSubscriptions.push([
|
|
174
174
|
key,
|
|
175
175
|
this.state[key].subscribe(() => {
|
|
176
|
-
callback(this.state);
|
|
176
|
+
void callback(this.state);
|
|
177
177
|
}),
|
|
178
178
|
]);
|
|
179
179
|
}
|
|
@@ -192,8 +192,8 @@ export class Galena extends Guards {
|
|
|
192
192
|
if (IDs) {
|
|
193
193
|
for (const [state, ID] of IDs) {
|
|
194
194
|
this.state[state].unsubscribe(ID);
|
|
195
|
-
this.subscriptions.delete(ID);
|
|
196
195
|
}
|
|
196
|
+
this.subscriptions.delete(ID);
|
|
197
197
|
}
|
|
198
198
|
}
|
|
199
199
|
/**
|
|
@@ -206,20 +206,12 @@ export class Galena extends Guards {
|
|
|
206
206
|
*/
|
|
207
207
|
reIndexSubscriptions(name) {
|
|
208
208
|
for (const [ID, unitSubscriptions] of this.subscriptions) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
name,
|
|
216
|
-
this.state[name].subscribe(() => {
|
|
217
|
-
void callback(this.state);
|
|
218
|
-
}),
|
|
219
|
-
]);
|
|
220
|
-
this.subscriptions.set(ID, unitSubscriptions);
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
209
|
+
const [state, listenerID] = unitSubscriptions[0];
|
|
210
|
+
const subscriptions = this.state[state]["emitter"].get(state);
|
|
211
|
+
const listener = subscriptions?.get(listenerID);
|
|
212
|
+
if (listener) {
|
|
213
|
+
unitSubscriptions.push([name, this.state[name].subscribe(listener)]);
|
|
214
|
+
this.subscriptions.set(ID, unitSubscriptions);
|
|
223
215
|
}
|
|
224
216
|
}
|
|
225
217
|
}
|
|
@@ -27,7 +27,7 @@ export class Profiler extends Middleware {
|
|
|
27
27
|
const endTime = performance.now();
|
|
28
28
|
const diff = endTime - this.startTime;
|
|
29
29
|
if (diff > this.threshold) {
|
|
30
|
-
console.warn(
|
|
30
|
+
console.warn(`A slow state transition was detected on ${nextState.name}`, nextState);
|
|
31
31
|
console.warn(`The last transition took ${diff}ms`);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Middleware } from "../Middleware/Middleware";
|
|
2
2
|
import { State } from "./State";
|
|
3
3
|
import { Guards } from "./Guards";
|
|
4
|
+
import type { Subscription } from "./types";
|
|
4
5
|
/**
|
|
5
6
|
* ## Galena
|
|
6
7
|
*
|
|
@@ -58,9 +59,9 @@ import { Guards } from "./Guards";
|
|
|
58
59
|
*/
|
|
59
60
|
export declare class Galena<T extends Record<string, State<any>> = Record<string, State<any>>> extends Guards {
|
|
60
61
|
readonly state: T;
|
|
61
|
-
private readonly subscriptions;
|
|
62
62
|
private readonly middleware;
|
|
63
63
|
private readonly IDs;
|
|
64
|
+
private readonly subscriptions;
|
|
64
65
|
constructor(middleware?: Middleware[]);
|
|
65
66
|
/**
|
|
66
67
|
* Compose State
|
|
@@ -139,7 +140,7 @@ export declare class Galena<T extends Record<string, State<any>> = Record<string
|
|
|
139
140
|
* subscription, call `Galena.unsubscribeAll()` with the ID
|
|
140
141
|
* returned
|
|
141
142
|
*/
|
|
142
|
-
subscribeAll(callback:
|
|
143
|
+
subscribeAll(callback: Subscription<T>): string;
|
|
143
144
|
/**
|
|
144
145
|
* Unsubscribe
|
|
145
146
|
*
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Middleware } from "../Middleware/Middleware";
|
|
2
2
|
import { Scheduler } from "./Scheduler";
|
|
3
|
+
import type { Subscription } from "./types";
|
|
3
4
|
import { Priority } from "./types";
|
|
4
5
|
/**
|
|
5
6
|
* ### State
|
|
@@ -200,7 +201,7 @@ export declare class State<T extends any = any> extends Scheduler {
|
|
|
200
201
|
* callback you provide will execute each time state changes.
|
|
201
202
|
* Returns a unique identifier for your subscription
|
|
202
203
|
*/
|
|
203
|
-
subscribe(callback:
|
|
204
|
+
subscribe(callback: Subscription<T>): string;
|
|
204
205
|
/**
|
|
205
206
|
* Unsubscribe
|
|
206
207
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@figliolia/galena",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.7",
|
|
4
4
|
"description": "A performant state management library supporting mutable state, batched updates, middleware and a rich development API",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
package/src/Galena/Galena.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { AutoIncrementingID } from "@figliolia/event-emitter";
|
|
|
2
2
|
import type { Middleware } from "Middleware/Middleware";
|
|
3
3
|
import { State } from "Galena/State";
|
|
4
4
|
import { Guards } from "./Guards";
|
|
5
|
+
import type { Subscription, SubscriptionTuple } from "./types";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* ## Galena
|
|
@@ -62,12 +63,9 @@ export class Galena<
|
|
|
62
63
|
T extends Record<string, State<any>> = Record<string, State<any>>
|
|
63
64
|
> extends Guards {
|
|
64
65
|
public readonly state = {} as T;
|
|
65
|
-
private readonly subscriptions = new Map<
|
|
66
|
-
string,
|
|
67
|
-
[state: string, ID: string][]
|
|
68
|
-
>();
|
|
69
66
|
private readonly middleware: Middleware[] = [];
|
|
70
67
|
private readonly IDs = new AutoIncrementingID();
|
|
68
|
+
private readonly subscriptions = new Map<string, SubscriptionTuple[]>();
|
|
71
69
|
constructor(middleware: Middleware[] = []) {
|
|
72
70
|
super();
|
|
73
71
|
this.middleware = middleware;
|
|
@@ -200,13 +198,13 @@ export class Galena<
|
|
|
200
198
|
* subscription, call `Galena.unsubscribeAll()` with the ID
|
|
201
199
|
* returned
|
|
202
200
|
*/
|
|
203
|
-
public subscribeAll(callback:
|
|
201
|
+
public subscribeAll(callback: Subscription<T>) {
|
|
204
202
|
const stateSubscriptions: [state: string, ID: string][] = [];
|
|
205
203
|
for (const key in this.state) {
|
|
206
204
|
stateSubscriptions.push([
|
|
207
205
|
key,
|
|
208
206
|
this.state[key].subscribe(() => {
|
|
209
|
-
callback(this.state);
|
|
207
|
+
void callback(this.state);
|
|
210
208
|
}),
|
|
211
209
|
]);
|
|
212
210
|
}
|
|
@@ -226,8 +224,8 @@ export class Galena<
|
|
|
226
224
|
if (IDs) {
|
|
227
225
|
for (const [state, ID] of IDs) {
|
|
228
226
|
this.state[state].unsubscribe(ID);
|
|
229
|
-
this.subscriptions.delete(ID);
|
|
230
227
|
}
|
|
228
|
+
this.subscriptions.delete(ID);
|
|
231
229
|
}
|
|
232
230
|
}
|
|
233
231
|
|
|
@@ -241,20 +239,12 @@ export class Galena<
|
|
|
241
239
|
*/
|
|
242
240
|
private reIndexSubscriptions(name: string) {
|
|
243
241
|
for (const [ID, unitSubscriptions] of this.subscriptions) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
name,
|
|
251
|
-
this.state[name].subscribe(() => {
|
|
252
|
-
void callback(this.state);
|
|
253
|
-
}),
|
|
254
|
-
]);
|
|
255
|
-
this.subscriptions.set(ID, unitSubscriptions);
|
|
256
|
-
break;
|
|
257
|
-
}
|
|
242
|
+
const [state, listenerID] = unitSubscriptions[0];
|
|
243
|
+
const subscriptions = this.state[state]["emitter"].get(state);
|
|
244
|
+
const listener = subscriptions?.get(listenerID);
|
|
245
|
+
if (listener) {
|
|
246
|
+
unitSubscriptions.push([name, this.state[name].subscribe(listener)]);
|
|
247
|
+
this.subscriptions.set(ID, unitSubscriptions);
|
|
258
248
|
}
|
|
259
249
|
}
|
|
260
250
|
}
|
package/src/Galena/State.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { MiddlewareEvents } from "Middleware/types";
|
|
|
2
2
|
import type { Middleware } from "Middleware/Middleware";
|
|
3
3
|
import { EventEmitter } from "@figliolia/event-emitter";
|
|
4
4
|
import { Scheduler } from "./Scheduler";
|
|
5
|
+
import type { Subscription } from "./types";
|
|
5
6
|
import { Priority, type MutationEvent } from "./types";
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -260,7 +261,7 @@ export class State<T extends any = any> extends Scheduler {
|
|
|
260
261
|
* callback you provide will execute each time state changes.
|
|
261
262
|
* Returns a unique identifier for your subscription
|
|
262
263
|
*/
|
|
263
|
-
public subscribe(callback:
|
|
264
|
+
public subscribe(callback: Subscription<T>) {
|
|
264
265
|
return this.emitter.on(this.name, callback);
|
|
265
266
|
}
|
|
266
267
|
|
package/src/Galena/types.ts
CHANGED
|
@@ -30,7 +30,10 @@ export class Profiler extends Middleware {
|
|
|
30
30
|
const endTime = performance.now();
|
|
31
31
|
const diff = endTime - this.startTime;
|
|
32
32
|
if (diff > this.threshold) {
|
|
33
|
-
console.warn(
|
|
33
|
+
console.warn(
|
|
34
|
+
`A slow state transition was detected on ${nextState.name}`,
|
|
35
|
+
nextState
|
|
36
|
+
);
|
|
34
37
|
console.warn(`The last transition took ${diff}ms`);
|
|
35
38
|
}
|
|
36
39
|
}
|