@figliolia/galena 2.2.4 → 2.2.6

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.
@@ -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
  /**
@@ -77,9 +77,10 @@ class Galena extends Guards_1.Guards {
77
77
  * state will automatically receive updates when your new unit of
78
78
  * state updates
79
79
  */
80
- composeState(name, initialState, Model = (State_1.State)) {
80
+ composeState(name, initialState, Model) {
81
81
  this.guardDuplicateStates(name, this.state);
82
- const state = new Model(name, initialState);
82
+ const StateModel = Model || (State_1.State);
83
+ const state = new StateModel(name, initialState);
83
84
  state.registerMiddleware(...this.middleware);
84
85
  this.mutable[name] = state;
85
86
  this.reIndexSubscriptions(name);
@@ -175,7 +176,7 @@ class Galena extends Guards_1.Guards {
175
176
  stateSubscriptions.push([
176
177
  key,
177
178
  this.state[key].subscribe(() => {
178
- callback(this.state);
179
+ void callback(this.state);
179
180
  }),
180
181
  ]);
181
182
  }
@@ -194,8 +195,8 @@ class Galena extends Guards_1.Guards {
194
195
  if (IDs) {
195
196
  for (const [state, ID] of IDs) {
196
197
  this.state[state].unsubscribe(ID);
197
- this.subscriptions.delete(ID);
198
198
  }
199
+ this.subscriptions.delete(ID);
199
200
  }
200
201
  }
201
202
  /**
@@ -207,21 +208,13 @@ class Galena extends Guards_1.Guards {
207
208
  * subscriptions
208
209
  */
209
210
  reIndexSubscriptions(name) {
210
- var _a;
211
211
  for (const [ID, unitSubscriptions] of this.subscriptions) {
212
- for (const [state, subscriptionID] of unitSubscriptions) {
213
- const callback = (_a = this.state[state]["emitter"]
214
- .get(state)) === null || _a === void 0 ? void 0 : _a.get(subscriptionID);
215
- if (callback) {
216
- unitSubscriptions.push([
217
- name,
218
- this.state[name].subscribe(() => {
219
- void callback(this.state);
220
- }),
221
- ]);
222
- this.subscriptions.set(ID, unitSubscriptions);
223
- break;
224
- }
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);
225
218
  }
226
219
  }
227
220
  }
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.State = void 0;
4
4
  const types_1 = require("../Middleware/types");
5
5
  const event_emitter_1 = require("@figliolia/event-emitter");
6
- const types_2 = require("./types");
7
6
  const Scheduler_1 = require("./Scheduler");
7
+ const types_2 = require("./types");
8
8
  /**
9
9
  * ### State
10
10
  *
@@ -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;
@@ -74,9 +74,10 @@ export class Galena extends Guards {
74
74
  * state will automatically receive updates when your new unit of
75
75
  * state updates
76
76
  */
77
- composeState(name, initialState, Model = (State)) {
77
+ composeState(name, initialState, Model) {
78
78
  this.guardDuplicateStates(name, this.state);
79
- const state = new Model(name, initialState);
79
+ const StateModel = Model || (State);
80
+ const state = new StateModel(name, initialState);
80
81
  state.registerMiddleware(...this.middleware);
81
82
  this.mutable[name] = state;
82
83
  this.reIndexSubscriptions(name);
@@ -172,7 +173,7 @@ export class Galena extends Guards {
172
173
  stateSubscriptions.push([
173
174
  key,
174
175
  this.state[key].subscribe(() => {
175
- callback(this.state);
176
+ void callback(this.state);
176
177
  }),
177
178
  ]);
178
179
  }
@@ -191,8 +192,8 @@ export class Galena extends Guards {
191
192
  if (IDs) {
192
193
  for (const [state, ID] of IDs) {
193
194
  this.state[state].unsubscribe(ID);
194
- this.subscriptions.delete(ID);
195
195
  }
196
+ this.subscriptions.delete(ID);
196
197
  }
197
198
  }
198
199
  /**
@@ -205,20 +206,12 @@ export class Galena extends Guards {
205
206
  */
206
207
  reIndexSubscriptions(name) {
207
208
  for (const [ID, unitSubscriptions] of this.subscriptions) {
208
- for (const [state, subscriptionID] of unitSubscriptions) {
209
- const callback = this.state[state]["emitter"]
210
- .get(state)
211
- ?.get(subscriptionID);
212
- if (callback) {
213
- unitSubscriptions.push([
214
- name,
215
- this.state[name].subscribe(() => {
216
- void callback(this.state);
217
- }),
218
- ]);
219
- this.subscriptions.set(ID, unitSubscriptions);
220
- break;
221
- }
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);
222
215
  }
223
216
  }
224
217
  }
@@ -1,7 +1,7 @@
1
1
  import { MiddlewareEvents } from "../Middleware/types.js";
2
2
  import { EventEmitter } from "@figliolia/event-emitter";
3
- import { Priority } from "./types.js";
4
3
  import { Scheduler } from "./Scheduler.js";
4
+ import { Priority } from "./types.js";
5
5
  /**
6
6
  * ### State
7
7
  *
@@ -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
@@ -71,10 +72,7 @@ export declare class Galena<T extends Record<string, State<any>> = Record<string
71
72
  * state will automatically receive updates when your new unit of
72
73
  * state updates
73
74
  */
74
- composeState<S extends any, M extends typeof State<S>>(name: string, initialState: S, Model?: {
75
- new (name: string, initialState: S): State<S>;
76
- clone<T_1>(state: T_1): T_1;
77
- }): InstanceType<M>;
75
+ composeState<S extends Record<string, any>, M extends typeof State<S>>(name: string, initialState: S, Model?: M): InstanceType<M>;
78
76
  /**
79
77
  * Get State
80
78
  *
@@ -142,7 +140,7 @@ export declare class Galena<T extends Record<string, State<any>> = Record<string
142
140
  * subscription, call `Galena.unsubscribeAll()` with the ID
143
141
  * returned
144
142
  */
145
- subscribeAll(callback: (nextState: T) => void): string;
143
+ subscribeAll(callback: Subscription<T>): string;
146
144
  /**
147
145
  * Unsubscribe
148
146
  *
@@ -1,6 +1,7 @@
1
1
  import type { Middleware } from "../Middleware/Middleware";
2
- import { Priority } from "./types";
3
2
  import { Scheduler } from "./Scheduler";
3
+ import type { Subscription } from "./types";
4
+ import { Priority } from "./types";
4
5
  /**
5
6
  * ### State
6
7
  *
@@ -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: (nextState: T) => void): string;
204
+ subscribe(callback: Subscription<T>): string;
204
205
  /**
205
206
  * Unsubscribe
206
207
  *
@@ -8,3 +8,5 @@ export declare enum Priority {
8
8
  "BATCHED" = 3
9
9
  }
10
10
  export type Task = () => void;
11
+ export type SubscriptionTuple = [state: string, ID: string];
12
+ export type Subscription<T> = (nextState: T) => void | Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@figliolia/galena",
3
- "version": "2.2.4",
3
+ "version": "2.2.6",
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",
@@ -1,9 +1,8 @@
1
1
  import { AutoIncrementingID } from "@figliolia/event-emitter";
2
-
3
2
  import type { Middleware } from "Middleware/Middleware";
4
-
5
3
  import { State } from "Galena/State";
6
4
  import { Guards } from "./Guards";
5
+ import type { Subscription, SubscriptionTuple } from "./types";
7
6
 
8
7
  /**
9
8
  * ## Galena
@@ -64,12 +63,9 @@ export class Galena<
64
63
  T extends Record<string, State<any>> = Record<string, State<any>>
65
64
  > extends Guards {
66
65
  public readonly state = {} as T;
67
- private readonly subscriptions = new Map<
68
- string,
69
- [state: string, ID: string][]
70
- >();
71
66
  private readonly middleware: Middleware[] = [];
72
67
  private readonly IDs = new AutoIncrementingID();
68
+ private readonly subscriptions = new Map<string, SubscriptionTuple[]>();
73
69
  constructor(middleware: Middleware[] = []) {
74
70
  super();
75
71
  this.middleware = middleware;
@@ -84,13 +80,14 @@ export class Galena<
84
80
  * state will automatically receive updates when your new unit of
85
81
  * state updates
86
82
  */
87
- public composeState<S extends any, M extends typeof State<S>>(
83
+ public composeState<S extends Record<string, any>, M extends typeof State<S>>(
88
84
  name: string,
89
85
  initialState: S,
90
- Model = State<S>
86
+ Model?: M
91
87
  ) {
92
88
  this.guardDuplicateStates(name, this.state);
93
- const state = new Model(name, initialState);
89
+ const StateModel = Model || State<S>;
90
+ const state = new StateModel(name, initialState);
94
91
  state.registerMiddleware(...this.middleware);
95
92
  this.mutable[name] = state;
96
93
  this.reIndexSubscriptions(name);
@@ -201,13 +198,13 @@ export class Galena<
201
198
  * subscription, call `Galena.unsubscribeAll()` with the ID
202
199
  * returned
203
200
  */
204
- public subscribeAll(callback: (nextState: T) => void) {
201
+ public subscribeAll(callback: Subscription<T>) {
205
202
  const stateSubscriptions: [state: string, ID: string][] = [];
206
203
  for (const key in this.state) {
207
204
  stateSubscriptions.push([
208
205
  key,
209
206
  this.state[key].subscribe(() => {
210
- callback(this.state);
207
+ void callback(this.state);
211
208
  }),
212
209
  ]);
213
210
  }
@@ -227,8 +224,8 @@ export class Galena<
227
224
  if (IDs) {
228
225
  for (const [state, ID] of IDs) {
229
226
  this.state[state].unsubscribe(ID);
230
- this.subscriptions.delete(ID);
231
227
  }
228
+ this.subscriptions.delete(ID);
232
229
  }
233
230
  }
234
231
 
@@ -242,20 +239,12 @@ export class Galena<
242
239
  */
243
240
  private reIndexSubscriptions(name: string) {
244
241
  for (const [ID, unitSubscriptions] of this.subscriptions) {
245
- for (const [state, subscriptionID] of unitSubscriptions) {
246
- const callback = this.state[state]["emitter"]
247
- .get(state)
248
- ?.get(subscriptionID);
249
- if (callback) {
250
- unitSubscriptions.push([
251
- name,
252
- this.state[name].subscribe(() => {
253
- void callback(this.state);
254
- }),
255
- ]);
256
- this.subscriptions.set(ID, unitSubscriptions);
257
- break;
258
- }
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);
259
248
  }
260
249
  }
261
250
  }
@@ -1,8 +1,9 @@
1
1
  import { MiddlewareEvents } from "Middleware/types";
2
2
  import type { Middleware } from "Middleware/Middleware";
3
3
  import { EventEmitter } from "@figliolia/event-emitter";
4
- import { Priority, type MutationEvent } from "./types";
5
4
  import { Scheduler } from "./Scheduler";
5
+ import type { Subscription } from "./types";
6
+ import { Priority, type MutationEvent } from "./types";
6
7
 
7
8
  /**
8
9
  * ### State
@@ -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: (nextState: T) => void) {
264
+ public subscribe(callback: Subscription<T>) {
264
265
  return this.emitter.on(this.name, callback);
265
266
  }
266
267
 
@@ -11,3 +11,7 @@ export enum Priority {
11
11
  }
12
12
 
13
13
  export type Task = () => void;
14
+
15
+ export type SubscriptionTuple = [state: string, ID: string];
16
+
17
+ export type Subscription<T> = (nextState: T) => void | Promise<void>;