@figliolia/galena 3.0.2 → 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.
- package/README.md +42 -54
- 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 +80 -22
- package/dist/Galena.d.cts +69 -22
- package/dist/Galena.d.cts.map +1 -1
- package/dist/Galena.d.mts +69 -22
- package/dist/Galena.d.mts.map +1 -1
- package/dist/Galena.mjs +80 -22
- 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 +23 -24
- package/dist/State.d.cts +22 -23
- package/dist/State.d.cts.map +1 -1
- package/dist/State.d.mts +22 -23
- package/dist/State.d.mts.map +1 -1
- package/dist/State.mjs +23 -24
- 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 +4 -2
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +4 -2
- package/dist/types.d.mts.map +1 -1
- package/package.json +8 -8
- package/src/API.ts +12 -0
- package/src/Galena.ts +101 -23
- package/src/Logger.ts +2 -2
- package/src/Middleware.ts +2 -2
- package/src/Profiler.ts +2 -2
- package/src/State.ts +23 -24
- package/src/types.ts +7 -1
package/src/Middleware.ts
CHANGED
|
@@ -19,7 +19,7 @@ import type { State } from "./State";
|
|
|
19
19
|
*
|
|
20
20
|
* public override onBeforeUpdate(state: State<T>) {
|
|
21
21
|
* this.startTime = performance.now();
|
|
22
|
-
* this.previousState = state.
|
|
22
|
+
* this.previousState = state.getState();
|
|
23
23
|
* }
|
|
24
24
|
*
|
|
25
25
|
* public override onUpdate(state: T) {
|
|
@@ -27,7 +27,7 @@ import type { State } from "./State";
|
|
|
27
27
|
* if(diff >= this.threshold) {
|
|
28
28
|
* console.warn(`A slow state transition was detected when transitioning the following piece of state`);
|
|
29
29
|
* console.log('Previous state', this.previousState);
|
|
30
|
-
* console.log('Current state', state.
|
|
30
|
+
* console.log('Current state', state.getState());
|
|
31
31
|
* }
|
|
32
32
|
* }
|
|
33
33
|
* }
|
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,19 +1,20 @@
|
|
|
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
|
|
|
5
6
|
/**
|
|
6
|
-
* State
|
|
7
|
+
* ### State
|
|
7
8
|
*
|
|
8
9
|
* The unit of reactivity for Galena. `State`'s can act
|
|
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
|
-
* import { State, createState,
|
|
16
|
-
|
|
16
|
+
* import { State, createState, Profiler } from "@figliolia/galena";
|
|
17
|
+
|
|
17
18
|
* const myState = new State("<any value>", ...middleware);
|
|
18
19
|
* // or
|
|
19
20
|
* const myState = createState("<any value>", ...middleware);
|
|
@@ -22,29 +23,20 @@ import type { NonFunction, Setter, Subscriber } from "./types";
|
|
|
22
23
|
* myState.update(previousValue => "<new-value>");
|
|
23
24
|
* myState.subscribe(nextValue => {});
|
|
24
25
|
* myState.registerMiddleware(new Profiler());
|
|
25
|
-
*
|
|
26
|
-
* //
|
|
27
|
-
* const
|
|
28
|
-
* const [state, setState] = useState(myState);
|
|
29
|
-
* // or
|
|
30
|
-
* const [state, setState] = useMyState("<any-value>", ...middlware);
|
|
31
|
-
*
|
|
32
|
-
* return (
|
|
33
|
-
* // your jsx
|
|
34
|
-
* );
|
|
35
|
-
* }
|
|
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();
|
|
36
29
|
* ```
|
|
37
30
|
*/
|
|
38
|
-
export class State<T> {
|
|
31
|
+
export class State<T> extends API<T, NonFunction<T>> {
|
|
39
32
|
private state: NonFunction<T>;
|
|
40
|
-
public readonly middleware: Middleware<T>[] = [];
|
|
41
33
|
private readonly Emitter = new EventEmitter<{ change: NonFunction<T> }>();
|
|
42
34
|
constructor(
|
|
43
35
|
public readonly initialState: NonFunction<T>,
|
|
44
36
|
...middleware: Middleware<T>[]
|
|
45
37
|
) {
|
|
38
|
+
super(...middleware);
|
|
46
39
|
this.state = initialState;
|
|
47
|
-
this.registerMiddleware(...middleware);
|
|
48
40
|
}
|
|
49
41
|
|
|
50
42
|
/**
|
|
@@ -83,7 +75,7 @@ export class State<T> {
|
|
|
83
75
|
* Returns the current state. Designed for compatibility with
|
|
84
76
|
* `useSyncExternalStore`
|
|
85
77
|
*/
|
|
86
|
-
public readonly
|
|
78
|
+
public readonly getState = () => {
|
|
87
79
|
return this.state;
|
|
88
80
|
};
|
|
89
81
|
|
|
@@ -143,21 +135,28 @@ export class State<T> {
|
|
|
143
135
|
}
|
|
144
136
|
|
|
145
137
|
/**
|
|
146
|
-
*
|
|
138
|
+
* ### createState
|
|
147
139
|
*
|
|
148
|
-
*
|
|
140
|
+
* The unit of reactivity for Galena. `State`'s can act
|
|
149
141
|
* as isolated instances or be part of your global app
|
|
150
|
-
* state (via `Galena` instances)
|
|
142
|
+
* state (via `Galena` instances).
|
|
151
143
|
*
|
|
152
|
-
*
|
|
153
|
-
* import { createState, Profiler } from "@figliolia/galena";
|
|
144
|
+
* There are two ways to create state instances
|
|
154
145
|
*
|
|
146
|
+
* ```typescript
|
|
147
|
+
* import { State, createState, Profiler } from "@figliolia/galena";
|
|
148
|
+
|
|
149
|
+
* const myState = new State("<any value>", ...middleware);
|
|
150
|
+
* // or
|
|
155
151
|
* const myState = createState("<any value>", ...middleware);
|
|
156
152
|
*
|
|
157
153
|
* myState.set("<new-value>");
|
|
158
154
|
* myState.update(previousValue => "<new-value>");
|
|
159
155
|
* myState.subscribe(nextValue => {});
|
|
160
156
|
* myState.registerMiddleware(new Profiler());
|
|
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();
|
|
161
160
|
* ```
|
|
162
161
|
*/
|
|
163
162
|
export function createState<T>(
|
package/src/types.ts
CHANGED
|
@@ -24,5 +24,11 @@ export type AppSubscriber<
|
|
|
24
24
|
> = ((payload: GalenaSnapshot<T, K>) => void) | (() => void);
|
|
25
25
|
|
|
26
26
|
export type StateTypes<T extends Record<string, State<any>>> = ReturnType<
|
|
27
|
-
T[keyof T]
|
|
27
|
+
StateType<T[keyof T]>
|
|
28
28
|
>;
|
|
29
|
+
|
|
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
|
+
};
|