@figliolia/galena 3.0.3 → 4.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.
Files changed (47) hide show
  1. package/README.md +21 -33
  2. package/dist/API.cjs +9 -0
  3. package/dist/API.d.cts +13 -0
  4. package/dist/API.d.cts.map +1 -0
  5. package/dist/API.d.mts +13 -0
  6. package/dist/API.d.mts.map +1 -0
  7. package/dist/API.mjs +11 -0
  8. package/dist/API.mjs.map +1 -0
  9. package/dist/Galena.cjs +31 -4
  10. package/dist/Galena.d.cts +24 -4
  11. package/dist/Galena.d.cts.map +1 -1
  12. package/dist/Galena.d.mts +24 -4
  13. package/dist/Galena.d.mts.map +1 -1
  14. package/dist/Galena.mjs +31 -4
  15. package/dist/Galena.mjs.map +1 -1
  16. package/dist/Logger.cjs +2 -2
  17. package/dist/Logger.mjs +2 -2
  18. package/dist/Logger.mjs.map +1 -1
  19. package/dist/Middleware.cjs +2 -2
  20. package/dist/Middleware.d.cts +2 -2
  21. package/dist/Middleware.d.mts +2 -2
  22. package/dist/Middleware.mjs +2 -2
  23. package/dist/Middleware.mjs.map +1 -1
  24. package/dist/Profiler.cjs +2 -2
  25. package/dist/Profiler.mjs +2 -2
  26. package/dist/Profiler.mjs.map +1 -1
  27. package/dist/State.cjs +12 -8
  28. package/dist/State.d.cts +11 -7
  29. package/dist/State.d.cts.map +1 -1
  30. package/dist/State.d.mts +11 -7
  31. package/dist/State.d.mts.map +1 -1
  32. package/dist/State.mjs +12 -8
  33. package/dist/State.mjs.map +1 -1
  34. package/dist/index.d.cts +2 -2
  35. package/dist/index.d.mts +2 -2
  36. package/dist/types.d.cts +3 -2
  37. package/dist/types.d.cts.map +1 -1
  38. package/dist/types.d.mts +3 -2
  39. package/dist/types.d.mts.map +1 -1
  40. package/package.json +8 -8
  41. package/src/API.ts +12 -0
  42. package/src/Galena.ts +37 -4
  43. package/src/Logger.ts +2 -2
  44. package/src/Middleware.ts +2 -2
  45. package/src/Profiler.ts +2 -2
  46. package/src/State.ts +12 -8
  47. package/src/types.ts +5 -1
package/src/State.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from "@figliolia/event-emitter";
2
+ import { API } from "./API";
2
3
  import type { Middleware } from "./Middleware";
3
4
  import type { NonFunction, Setter, Subscriber } from "./types";
4
5
 
@@ -9,11 +10,11 @@ import type { NonFunction, Setter, Subscriber } from "./types";
9
10
  * as isolated instances or be part of your global app
10
11
  * state (via `Galena` instances).
11
12
  *
12
- * There are three ways to create state instances
13
+ * There are two ways to create state instances
13
14
  *
14
15
  * ```typescript
15
16
  * import { State, createState, Profiler } from "@figliolia/galena";
16
- * // for island states that can be shared between react components
17
+
17
18
  * const myState = new State("<any value>", ...middleware);
18
19
  * // or
19
20
  * const myState = createState("<any value>", ...middleware);
@@ -23,18 +24,19 @@ import type { NonFunction, Setter, Subscriber } from "./types";
23
24
  * myState.subscribe(nextValue => {});
24
25
  * myState.registerMiddleware(new Profiler());
25
26
  * myState.reset(); // reset back to it's original value
27
+ * // to get the current value at any point in time
28
+ * const currentValue = myState.getState();
26
29
  * ```
27
30
  */
28
- export class State<T> {
31
+ export class State<T> extends API<T, NonFunction<T>> {
29
32
  private state: NonFunction<T>;
30
- public readonly middleware: Middleware<T>[] = [];
31
33
  private readonly Emitter = new EventEmitter<{ change: NonFunction<T> }>();
32
34
  constructor(
33
35
  public readonly initialState: NonFunction<T>,
34
36
  ...middleware: Middleware<T>[]
35
37
  ) {
38
+ super(...middleware);
36
39
  this.state = initialState;
37
- this.registerMiddleware(...middleware);
38
40
  }
39
41
 
40
42
  /**
@@ -73,7 +75,7 @@ export class State<T> {
73
75
  * Returns the current state. Designed for compatibility with
74
76
  * `useSyncExternalStore`
75
77
  */
76
- public readonly getSnapshot = () => {
78
+ public readonly getState = () => {
77
79
  return this.state;
78
80
  };
79
81
 
@@ -139,11 +141,11 @@ export class State<T> {
139
141
  * as isolated instances or be part of your global app
140
142
  * state (via `Galena` instances).
141
143
  *
142
- * There are three ways to create state instances
144
+ * There are two ways to create state instances
143
145
  *
144
146
  * ```typescript
145
147
  * import { State, createState, Profiler } from "@figliolia/galena";
146
- * // for island states that can be shared between react components
148
+
147
149
  * const myState = new State("<any value>", ...middleware);
148
150
  * // or
149
151
  * const myState = createState("<any value>", ...middleware);
@@ -153,6 +155,8 @@ export class State<T> {
153
155
  * myState.subscribe(nextValue => {});
154
156
  * myState.registerMiddleware(new Profiler());
155
157
  * myState.reset(); // reset back to it's original value
158
+ * // to get the current value at any point in time
159
+ * const currentValue = myState.getState();
156
160
  * ```
157
161
  */
158
162
  export function createState<T>(
package/src/types.ts CHANGED
@@ -27,4 +27,8 @@ export type StateTypes<T extends Record<string, State<any>>> = ReturnType<
27
27
  StateType<T[keyof T]>
28
28
  >;
29
29
 
30
- export type StateType<T extends State<any>> = ReturnType<T["getSnapshot"]>;
30
+ export type StateType<T extends State<any>> = ReturnType<T["getState"]>;
31
+
32
+ export type GalenaState<T extends Record<string, State<any>>> = {
33
+ [K in keyof T]: StateType<T[K]>;
34
+ };