@figliolia/galena 2.0.9 → 2.2.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 CHANGED
@@ -132,6 +132,14 @@ import type { State } from "@figliolia/galena";
132
132
 
133
133
  const AppState = new Galena(/* middleware */ [new Logger(), new Profiler()]);
134
134
 
135
+ /**
136
+ * Get State
137
+ *
138
+ * Returns the current state tree with each attached
139
+ * unit. The object returned is readonly
140
+ */
141
+ AppState.getState();
142
+
135
143
  /**
136
144
  * Compose State
137
145
  *
@@ -71,7 +71,16 @@ export declare class Galena<T extends Record<string, State<any>> = Record<string
71
71
  * state will automatically receive updates when your new unit of
72
72
  * state updates
73
73
  */
74
- composeState<S extends any, M extends typeof State<S> = typeof State<S>>(name: string, initialState: S, Model?: M): InstanceType<M>;
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>;
78
+ /**
79
+ * Get State
80
+ *
81
+ * Returns a mutable state instance
82
+ */
83
+ getState(): Readonly<T>;
75
84
  /**
76
85
  * Get
77
86
  *
@@ -77,9 +77,7 @@ 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,
81
- // @ts-ignore
82
- Model = (State_1.State)) {
80
+ composeState(name, initialState, Model = (State_1.State)) {
83
81
  this.guardDuplicateStates(name, this.state);
84
82
  const state = new Model(name, initialState);
85
83
  state.registerMiddleware(...this.middleware);
@@ -87,6 +85,14 @@ class Galena extends Guards_1.Guards {
87
85
  this.reIndexSubscriptions(name);
88
86
  return state;
89
87
  }
88
+ /**
89
+ * Get State
90
+ *
91
+ * Returns a mutable state instance
92
+ */
93
+ getState() {
94
+ return this.state;
95
+ }
90
96
  /**
91
97
  * Get
92
98
  *
@@ -57,7 +57,7 @@ import { Scheduler } from "./Scheduler";
57
57
  * ```
58
58
  */
59
59
  export declare class State<T extends any = any> extends Scheduler {
60
- state: T;
60
+ private state;
61
61
  readonly name: string;
62
62
  readonly initialState: T;
63
63
  private readonly middleware;
@@ -23,7 +23,7 @@ class Logger extends Middleware_1.Middleware {
23
23
  this.previousState = null;
24
24
  }
25
25
  onBeforeUpdate(state) {
26
- this.previousState = State_1.State.clone(state.state);
26
+ this.previousState = State_1.State.clone(state.getState());
27
27
  }
28
28
  onUpdate(state) {
29
29
  console.log("%cMutation:", "color: rgb(187, 186, 186); font-weight: bold", state.name, "@", this.time);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@figliolia/galena",
3
- "version": "2.0.9",
3
+ "version": "2.2.0",
4
4
  "description": "A performant state management library supporting mutable state, batched updates, middleware and a rich development API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -56,4 +56,4 @@
56
56
  "publishConfig": {
57
57
  "access": "public"
58
58
  }
59
- }
59
+ }
@@ -84,14 +84,10 @@ export class Galena<
84
84
  * state will automatically receive updates when your new unit of
85
85
  * state updates
86
86
  */
87
- public composeState<
88
- S extends any,
89
- M extends typeof State<S> = typeof State<S>
90
- >(
87
+ public composeState<S extends any, M extends typeof State<S>>(
91
88
  name: string,
92
89
  initialState: S,
93
- // @ts-ignore
94
- Model: M = State<S>
90
+ Model = State<S>
95
91
  ) {
96
92
  this.guardDuplicateStates(name, this.state);
97
93
  const state = new Model(name, initialState);
@@ -101,6 +97,15 @@ export class Galena<
101
97
  return state as InstanceType<M>;
102
98
  }
103
99
 
100
+ /**
101
+ * Get State
102
+ *
103
+ * Returns a mutable state instance
104
+ */
105
+ public getState() {
106
+ return this.state as Readonly<T>;
107
+ }
108
+
104
109
  /**
105
110
  * Get
106
111
  *
@@ -60,7 +60,7 @@ import { Scheduler } from "./Scheduler";
60
60
  * ```
61
61
  */
62
62
  export class State<T extends any = any> extends Scheduler {
63
- public state: T;
63
+ private state: T;
64
64
  public readonly name: string;
65
65
  public readonly initialState: T;
66
66
  private readonly middleware: Middleware[] = [];
@@ -19,7 +19,7 @@ export class Logger extends Middleware {
19
19
  private previousState: Record<string, any> | null = null;
20
20
 
21
21
  override onBeforeUpdate(state: State) {
22
- this.previousState = State.clone(state.state);
22
+ this.previousState = State.clone(state.getState());
23
23
  }
24
24
 
25
25
  override onUpdate(state: State) {