@figliolia/galena 2.2.5 → 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
  /**
@@ -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
- for (const [state, subscriptionID] of unitSubscriptions) {
214
- const callback = (_a = this.state[state]["emitter"]
215
- .get(state)) === null || _a === void 0 ? void 0 : _a.get(subscriptionID);
216
- if (callback) {
217
- unitSubscriptions.push([
218
- name,
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
  }
@@ -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
- for (const [state, subscriptionID] of unitSubscriptions) {
210
- const callback = this.state[state]["emitter"]
211
- .get(state)
212
- ?.get(subscriptionID);
213
- if (callback) {
214
- unitSubscriptions.push([
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
  }
@@ -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: (nextState: T) => void): string;
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: (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.5",
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",
@@ -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: (nextState: T) => void) {
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
- for (const [state, subscriptionID] of unitSubscriptions) {
245
- const callback = this.state[state]["emitter"]
246
- .get(state)
247
- ?.get(subscriptionID);
248
- if (callback) {
249
- unitSubscriptions.push([
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
  }
@@ -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: (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>;