@figliolia/galena 3.0.3 → 4.0.1
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 +21 -33
- package/dist/API.cjs +9 -0
- package/dist/API.d.cts +13 -0
- package/dist/API.d.cts.map +1 -0
- package/dist/API.d.mts +13 -0
- package/dist/API.d.mts.map +1 -0
- package/dist/API.mjs +11 -0
- package/dist/API.mjs.map +1 -0
- package/dist/Galena.cjs +31 -4
- package/dist/Galena.d.cts +24 -4
- package/dist/Galena.d.cts.map +1 -1
- package/dist/Galena.d.mts +24 -4
- package/dist/Galena.d.mts.map +1 -1
- package/dist/Galena.mjs +31 -4
- package/dist/Galena.mjs.map +1 -1
- package/dist/Logger.cjs +2 -2
- package/dist/Logger.mjs +2 -2
- package/dist/Logger.mjs.map +1 -1
- package/dist/Middleware.cjs +2 -2
- package/dist/Middleware.d.cts +2 -2
- package/dist/Middleware.d.mts +2 -2
- package/dist/Middleware.mjs +2 -2
- package/dist/Middleware.mjs.map +1 -1
- package/dist/Profiler.cjs +2 -2
- package/dist/Profiler.mjs +2 -2
- package/dist/Profiler.mjs.map +1 -1
- package/dist/State.cjs +17 -14
- package/dist/State.d.cts +14 -11
- package/dist/State.d.cts.map +1 -1
- package/dist/State.d.mts +14 -11
- package/dist/State.d.mts.map +1 -1
- package/dist/State.mjs +17 -14
- package/dist/State.mjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/types.d.cts +3 -2
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +3 -2
- package/dist/types.d.mts.map +1 -1
- package/package.json +9 -9
- package/src/API.ts +12 -0
- package/src/Galena.ts +37 -4
- package/src/Logger.ts +2 -2
- package/src/Middleware.ts +2 -2
- package/src/Profiler.ts +2 -2
- package/src/State.ts +17 -14
- package/src/types.ts +5 -1
package/src/Profiler.ts
CHANGED
|
@@ -26,7 +26,7 @@ export class Profiler<T = any> extends Middleware<T> {
|
|
|
26
26
|
|
|
27
27
|
public override onBeforeUpdate(state: State<T>) {
|
|
28
28
|
this.startTime = performance.now();
|
|
29
|
-
this.previousState = state.
|
|
29
|
+
this.previousState = state.getState();
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
public override onUpdate(state: State<T>) {
|
|
@@ -46,7 +46,7 @@ export class Profiler<T = any> extends Middleware<T> {
|
|
|
46
46
|
console.log(
|
|
47
47
|
" %cCurrent State ",
|
|
48
48
|
"color: rgb(17, 118, 249); font-weight: bold",
|
|
49
|
-
state.
|
|
49
|
+
state.getState(),
|
|
50
50
|
);
|
|
51
51
|
}
|
|
52
52
|
}
|
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
|
|
13
|
+
* There are two ways to create state instances
|
|
13
14
|
*
|
|
14
15
|
* ```typescript
|
|
15
16
|
* import { State, createState, Profiler } from "@figliolia/galena";
|
|
16
|
-
|
|
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
|
/**
|
|
@@ -68,14 +70,13 @@ export class State<T> {
|
|
|
68
70
|
public readonly reset = this.withEmission(() => this.initialState);
|
|
69
71
|
|
|
70
72
|
/**
|
|
71
|
-
* Get
|
|
73
|
+
* Get State
|
|
72
74
|
*
|
|
73
|
-
* Returns the current state
|
|
74
|
-
* `useSyncExternalStore`
|
|
75
|
+
* Returns the current state
|
|
75
76
|
*/
|
|
76
|
-
public
|
|
77
|
+
public getState() {
|
|
77
78
|
return this.state;
|
|
78
|
-
}
|
|
79
|
+
}
|
|
79
80
|
|
|
80
81
|
/**
|
|
81
82
|
* Subscribe
|
|
@@ -83,12 +84,12 @@ export class State<T> {
|
|
|
83
84
|
* Registers a callback to be executed each time state
|
|
84
85
|
* changes. Returns an `unsubscribe` function
|
|
85
86
|
*/
|
|
86
|
-
public
|
|
87
|
+
public subscribe(fn: Subscriber<T>) {
|
|
87
88
|
const ID = this.Emitter.on("change", fn);
|
|
88
89
|
return () => {
|
|
89
90
|
this.Emitter.off("change", ID);
|
|
90
91
|
};
|
|
91
|
-
}
|
|
92
|
+
}
|
|
92
93
|
|
|
93
94
|
/**
|
|
94
95
|
* Register Middleware
|
|
@@ -139,11 +140,11 @@ export class State<T> {
|
|
|
139
140
|
* as isolated instances or be part of your global app
|
|
140
141
|
* state (via `Galena` instances).
|
|
141
142
|
*
|
|
142
|
-
* There are
|
|
143
|
+
* There are two ways to create state instances
|
|
143
144
|
*
|
|
144
145
|
* ```typescript
|
|
145
146
|
* import { State, createState, Profiler } from "@figliolia/galena";
|
|
146
|
-
|
|
147
|
+
|
|
147
148
|
* const myState = new State("<any value>", ...middleware);
|
|
148
149
|
* // or
|
|
149
150
|
* const myState = createState("<any value>", ...middleware);
|
|
@@ -153,6 +154,8 @@ export class State<T> {
|
|
|
153
154
|
* myState.subscribe(nextValue => {});
|
|
154
155
|
* myState.registerMiddleware(new Profiler());
|
|
155
156
|
* myState.reset(); // reset back to it's original value
|
|
157
|
+
* // to get the current value at any point in time
|
|
158
|
+
* const currentValue = myState.getState();
|
|
156
159
|
* ```
|
|
157
160
|
*/
|
|
158
161
|
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["
|
|
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
|
+
};
|