@figliolia/galena 2.3.5 → 3.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.
Files changed (106) hide show
  1. package/README.md +172 -476
  2. package/dist/Galena.cjs +102 -0
  3. package/dist/Galena.d.cts +80 -0
  4. package/dist/Galena.d.cts.map +1 -0
  5. package/dist/Galena.d.mts +80 -0
  6. package/dist/Galena.d.mts.map +1 -0
  7. package/dist/Galena.mjs +103 -0
  8. package/dist/Galena.mjs.map +1 -0
  9. package/dist/Logger.cjs +46 -0
  10. package/dist/Logger.d.cts +35 -0
  11. package/dist/Logger.d.cts.map +1 -0
  12. package/dist/Logger.d.mts +35 -0
  13. package/dist/Logger.d.mts.map +1 -0
  14. package/dist/Logger.mjs +48 -0
  15. package/dist/Logger.mjs.map +1 -0
  16. package/dist/Middleware.cjs +62 -0
  17. package/dist/Middleware.d.cts +65 -0
  18. package/dist/Middleware.d.cts.map +1 -0
  19. package/dist/Middleware.d.mts +65 -0
  20. package/dist/Middleware.d.mts.map +1 -0
  21. package/dist/Middleware.mjs +64 -0
  22. package/dist/Middleware.mjs.map +1 -0
  23. package/dist/Profiler.cjs +41 -0
  24. package/dist/Profiler.d.cts +31 -0
  25. package/dist/Profiler.d.cts.map +1 -0
  26. package/dist/Profiler.d.mts +31 -0
  27. package/dist/Profiler.d.mts.map +1 -0
  28. package/dist/Profiler.mjs +43 -0
  29. package/dist/Profiler.mjs.map +1 -0
  30. package/dist/State.cjs +147 -0
  31. package/dist/State.d.cts +114 -0
  32. package/dist/State.d.cts.map +1 -0
  33. package/dist/State.d.mts +114 -0
  34. package/dist/State.d.mts.map +1 -0
  35. package/dist/State.mjs +148 -0
  36. package/dist/State.mjs.map +1 -0
  37. package/dist/index.cjs +13 -0
  38. package/dist/index.d.cts +7 -0
  39. package/dist/index.d.mts +7 -0
  40. package/dist/index.mjs +6 -0
  41. package/dist/types.d.cts +16 -0
  42. package/dist/types.d.cts.map +1 -0
  43. package/dist/types.d.mts +16 -0
  44. package/dist/types.d.mts.map +1 -0
  45. package/media/Logging.png +0 -0
  46. package/media/Profiling.png +0 -0
  47. package/package.json +38 -59
  48. package/src/Galena.ts +120 -0
  49. package/src/{Middlewares/Logger.ts → Logger.ts} +15 -14
  50. package/src/Middleware.ts +62 -0
  51. package/src/Profiler.ts +53 -0
  52. package/src/State.ts +167 -0
  53. package/src/index.ts +6 -3
  54. package/src/types.ts +28 -0
  55. package/dist/cjs/Galena/Galena.js +0 -223
  56. package/dist/cjs/Galena/Guards.js +0 -40
  57. package/dist/cjs/Galena/Scheduler.js +0 -84
  58. package/dist/cjs/Galena/State.js +0 -314
  59. package/dist/cjs/Galena/index.js +0 -22
  60. package/dist/cjs/Galena/types.js +0 -9
  61. package/dist/cjs/Middleware/Middleware.js +0 -46
  62. package/dist/cjs/Middleware/index.js +0 -20
  63. package/dist/cjs/Middleware/types.js +0 -8
  64. package/dist/cjs/Middlewares/Logger.js +0 -51
  65. package/dist/cjs/Middlewares/Profiler.js +0 -38
  66. package/dist/cjs/Middlewares/index.js +0 -7
  67. package/dist/cjs/index.js +0 -19
  68. package/dist/cjs/package.json +0 -3
  69. package/dist/mjs/Galena/Galena.js +0 -218
  70. package/dist/mjs/Galena/Guards.js +0 -36
  71. package/dist/mjs/Galena/Scheduler.js +0 -79
  72. package/dist/mjs/Galena/State.js +0 -313
  73. package/dist/mjs/Galena/index.js +0 -3
  74. package/dist/mjs/Galena/types.js +0 -6
  75. package/dist/mjs/Middleware/Middleware.js +0 -42
  76. package/dist/mjs/Middleware/index.js +0 -2
  77. package/dist/mjs/Middleware/types.js +0 -5
  78. package/dist/mjs/Middlewares/Logger.js +0 -44
  79. package/dist/mjs/Middlewares/Profiler.js +0 -35
  80. package/dist/mjs/Middlewares/index.js +0 -2
  81. package/dist/mjs/index.js +0 -3
  82. package/dist/mjs/package.json +0 -4
  83. package/dist/types/Galena/Galena.d.ts +0 -160
  84. package/dist/types/Galena/Guards.d.ts +0 -29
  85. package/dist/types/Galena/Scheduler.d.ts +0 -51
  86. package/dist/types/Galena/State.d.ts +0 -235
  87. package/dist/types/Galena/index.d.ts +0 -3
  88. package/dist/types/Galena/types.d.ts +0 -13
  89. package/dist/types/Middleware/Middleware.d.ts +0 -43
  90. package/dist/types/Middleware/index.d.ts +0 -2
  91. package/dist/types/Middleware/types.d.ts +0 -4
  92. package/dist/types/Middlewares/Logger.d.ts +0 -27
  93. package/dist/types/Middlewares/Profiler.d.ts +0 -22
  94. package/dist/types/Middlewares/index.d.ts +0 -2
  95. package/dist/types/index.d.ts +0 -3
  96. package/src/Galena/Galena.ts +0 -252
  97. package/src/Galena/Guards.ts +0 -49
  98. package/src/Galena/Scheduler.ts +0 -85
  99. package/src/Galena/State.ts +0 -344
  100. package/src/Galena/index.ts +0 -3
  101. package/src/Galena/types.ts +0 -18
  102. package/src/Middleware/Middleware.ts +0 -45
  103. package/src/Middleware/index.ts +0 -2
  104. package/src/Middleware/types.ts +0 -4
  105. package/src/Middlewares/Profiler.ts +0 -41
  106. package/src/Middlewares/index.ts +0 -2
@@ -0,0 +1,65 @@
1
+ import { State } from "./State.cjs";
2
+
3
+ //#region src/Middleware.d.ts
4
+ /**
5
+ * Middleware
6
+ *
7
+ * Galena's middleware API is designed to provide hooks
8
+ * for state changes that you can tap into to run your
9
+ * own logic.
10
+ *
11
+ * Middleware is great for logging, analytics, and profiling:
12
+ *
13
+ * ```typescript
14
+ * export class Profiler<T = any> extends Middleware<T> {
15
+ * private previousState: T | null = null;
16
+ * private startTime: null | number = null;
17
+ * constructor(public readonly threshold: number = 16) {
18
+ * super();
19
+ * }
20
+ *
21
+ * public override onBeforeUpdate(state: State<T>) {
22
+ * this.startTime = performance.now();
23
+ * this.previousState = state.getSnapshot();
24
+ * }
25
+ *
26
+ * public override onUpdate(state: T) {
27
+ * const diff = performance.now() - this.startTime;
28
+ * if(diff >= this.threshold) {
29
+ * console.warn(`A slow state transition was detected when transitioning the following piece of state`);
30
+ * console.log('Previous state', this.previousState);
31
+ * console.log('Current state', state.getSnapshot());
32
+ * }
33
+ * }
34
+ * }
35
+ * ```
36
+ *
37
+ * To register your middleware, simply add it when constructing
38
+ * a `State` or `Galena` instance.
39
+ *
40
+ * ```typescript
41
+ * import { State } from "@figliolia/galena";
42
+ * import { Profiler } from './myProfiler';
43
+ *
44
+ * const myState = new State(5, new Profiler());
45
+ * ```
46
+ */
47
+ declare class Middleware<T = any> {
48
+ /**
49
+ * On Before Update
50
+ *
51
+ * Executes prior to a `State` instance being updated.
52
+ * Receives the state prior to its update as a parameter
53
+ */
54
+ onBeforeUpdate(_state: State<T>): void;
55
+ /**
56
+ * On Update
57
+ *
58
+ * Executes after a `State` instance has been update.
59
+ * Receives the most recent state as a parameter
60
+ */
61
+ onUpdate(_state: State<T>): void;
62
+ }
63
+ //#endregion
64
+ export { Middleware };
65
+ //# sourceMappingURL=Middleware.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Middleware.d.cts","names":[],"sources":["../src/Middleware.ts"],"mappings":";;;;;AA6CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,UAAA;;;;;;;EAOJ,cAAA,CAAe,MAAA,EAAQ,KAAA,CAAM,CAAA;;;;;;;EAQ7B,QAAA,CAAS,MAAA,EAAQ,KAAA,CAAM,CAAA;AAAA"}
@@ -0,0 +1,65 @@
1
+ import { State } from "./State.mjs";
2
+
3
+ //#region src/Middleware.d.ts
4
+ /**
5
+ * Middleware
6
+ *
7
+ * Galena's middleware API is designed to provide hooks
8
+ * for state changes that you can tap into to run your
9
+ * own logic.
10
+ *
11
+ * Middleware is great for logging, analytics, and profiling:
12
+ *
13
+ * ```typescript
14
+ * export class Profiler<T = any> extends Middleware<T> {
15
+ * private previousState: T | null = null;
16
+ * private startTime: null | number = null;
17
+ * constructor(public readonly threshold: number = 16) {
18
+ * super();
19
+ * }
20
+ *
21
+ * public override onBeforeUpdate(state: State<T>) {
22
+ * this.startTime = performance.now();
23
+ * this.previousState = state.getSnapshot();
24
+ * }
25
+ *
26
+ * public override onUpdate(state: T) {
27
+ * const diff = performance.now() - this.startTime;
28
+ * if(diff >= this.threshold) {
29
+ * console.warn(`A slow state transition was detected when transitioning the following piece of state`);
30
+ * console.log('Previous state', this.previousState);
31
+ * console.log('Current state', state.getSnapshot());
32
+ * }
33
+ * }
34
+ * }
35
+ * ```
36
+ *
37
+ * To register your middleware, simply add it when constructing
38
+ * a `State` or `Galena` instance.
39
+ *
40
+ * ```typescript
41
+ * import { State } from "@figliolia/galena";
42
+ * import { Profiler } from './myProfiler';
43
+ *
44
+ * const myState = new State(5, new Profiler());
45
+ * ```
46
+ */
47
+ declare class Middleware<T = any> {
48
+ /**
49
+ * On Before Update
50
+ *
51
+ * Executes prior to a `State` instance being updated.
52
+ * Receives the state prior to its update as a parameter
53
+ */
54
+ onBeforeUpdate(_state: State<T>): void;
55
+ /**
56
+ * On Update
57
+ *
58
+ * Executes after a `State` instance has been update.
59
+ * Receives the most recent state as a parameter
60
+ */
61
+ onUpdate(_state: State<T>): void;
62
+ }
63
+ //#endregion
64
+ export { Middleware };
65
+ //# sourceMappingURL=Middleware.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Middleware.d.mts","names":[],"sources":["../src/Middleware.ts"],"mappings":";;;;;AA6CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,UAAA;;;;;;;EAOJ,cAAA,CAAe,MAAA,EAAQ,KAAA,CAAM,CAAA;;;;;;;EAQ7B,QAAA,CAAS,MAAA,EAAQ,KAAA,CAAM,CAAA;AAAA"}
@@ -0,0 +1,64 @@
1
+ //#region src/Middleware.ts
2
+ /**
3
+ * Middleware
4
+ *
5
+ * Galena's middleware API is designed to provide hooks
6
+ * for state changes that you can tap into to run your
7
+ * own logic.
8
+ *
9
+ * Middleware is great for logging, analytics, and profiling:
10
+ *
11
+ * ```typescript
12
+ * export class Profiler<T = any> extends Middleware<T> {
13
+ * private previousState: T | null = null;
14
+ * private startTime: null | number = null;
15
+ * constructor(public readonly threshold: number = 16) {
16
+ * super();
17
+ * }
18
+ *
19
+ * public override onBeforeUpdate(state: State<T>) {
20
+ * this.startTime = performance.now();
21
+ * this.previousState = state.getSnapshot();
22
+ * }
23
+ *
24
+ * public override onUpdate(state: T) {
25
+ * const diff = performance.now() - this.startTime;
26
+ * if(diff >= this.threshold) {
27
+ * console.warn(`A slow state transition was detected when transitioning the following piece of state`);
28
+ * console.log('Previous state', this.previousState);
29
+ * console.log('Current state', state.getSnapshot());
30
+ * }
31
+ * }
32
+ * }
33
+ * ```
34
+ *
35
+ * To register your middleware, simply add it when constructing
36
+ * a `State` or `Galena` instance.
37
+ *
38
+ * ```typescript
39
+ * import { State } from "@figliolia/galena";
40
+ * import { Profiler } from './myProfiler';
41
+ *
42
+ * const myState = new State(5, new Profiler());
43
+ * ```
44
+ */
45
+ var Middleware = class {
46
+ /**
47
+ * On Before Update
48
+ *
49
+ * Executes prior to a `State` instance being updated.
50
+ * Receives the state prior to its update as a parameter
51
+ */
52
+ onBeforeUpdate(_state) {}
53
+ /**
54
+ * On Update
55
+ *
56
+ * Executes after a `State` instance has been update.
57
+ * Receives the most recent state as a parameter
58
+ */
59
+ onUpdate(_state) {}
60
+ };
61
+ //#endregion
62
+ export { Middleware };
63
+
64
+ //# sourceMappingURL=Middleware.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Middleware.mjs","names":[],"sources":["../src/Middleware.ts"],"sourcesContent":["import type { State } from \"./State\";\n\n/**\n * Middleware\n *\n * Galena's middleware API is designed to provide hooks\n * for state changes that you can tap into to run your\n * own logic.\n *\n * Middleware is great for logging, analytics, and profiling:\n *\n * ```typescript\n * export class Profiler<T = any> extends Middleware<T> {\n * private previousState: T | null = null;\n * private startTime: null | number = null;\n * constructor(public readonly threshold: number = 16) {\n * super();\n * }\n *\n * public override onBeforeUpdate(state: State<T>) {\n * this.startTime = performance.now();\n * this.previousState = state.getSnapshot();\n * }\n *\n * public override onUpdate(state: T) {\n * const diff = performance.now() - this.startTime;\n * if(diff >= this.threshold) {\n * console.warn(`A slow state transition was detected when transitioning the following piece of state`);\n * console.log('Previous state', this.previousState);\n * console.log('Current state', state.getSnapshot());\n * }\n * }\n * }\n * ```\n *\n * To register your middleware, simply add it when constructing\n * a `State` or `Galena` instance.\n *\n * ```typescript\n * import { State } from \"@figliolia/galena\";\n * import { Profiler } from './myProfiler';\n *\n * const myState = new State(5, new Profiler());\n * ```\n */\nexport class Middleware<T = any> {\n /**\n * On Before Update\n *\n * Executes prior to a `State` instance being updated.\n * Receives the state prior to its update as a parameter\n */\n public onBeforeUpdate(_state: State<T>) {}\n\n /**\n * On Update\n *\n * Executes after a `State` instance has been update.\n * Receives the most recent state as a parameter\n */\n public onUpdate(_state: State<T>) {}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,IAAa,aAAb,MAAiC;;;;;;;CAO/B,eAAsB,QAAkB;;;;;;;CAQxC,SAAgB,QAAkB"}
@@ -0,0 +1,41 @@
1
+ const require_Middleware = require("./Middleware.cjs");
2
+ //#region src/Profiler.ts
3
+ /**
4
+ * Profiler
5
+ *
6
+ * A logger for state transitions exceeding a given
7
+ * millisecond threshold
8
+ *
9
+ * ```typescript
10
+ * const AppState = new Galena({}, new Profiler());
11
+ * // or
12
+ * AppState.registerMiddlerware(new Profiler());
13
+ * // or
14
+ * const MyState = new State(4, new Profiler());
15
+ * // or
16
+ * MyState.registerMiddleware(new Profiler());
17
+ * ```
18
+ */
19
+ var Profiler = class extends require_Middleware.Middleware {
20
+ previousState = null;
21
+ startTime = null;
22
+ constructor(threshold = 16) {
23
+ super();
24
+ this.threshold = threshold;
25
+ }
26
+ onBeforeUpdate(state) {
27
+ this.startTime = performance.now();
28
+ this.previousState = state.getSnapshot();
29
+ }
30
+ onUpdate(state) {
31
+ if (this.startTime === null) return;
32
+ const diff = performance.now() - this.startTime;
33
+ if (diff >= this.threshold) {
34
+ console.warn(`A slow state transition of ${diff.toFixed(1)}ms was detected when transitioning the following piece of state`);
35
+ console.log(" %cPrevious State", "color: #26ad65; font-weight: bold", this.previousState);
36
+ console.log(" %cCurrent State ", "color: rgb(17, 118, 249); font-weight: bold", state.getSnapshot());
37
+ }
38
+ }
39
+ };
40
+ //#endregion
41
+ exports.Profiler = Profiler;
@@ -0,0 +1,31 @@
1
+ import { State } from "./State.cjs";
2
+ import { Middleware } from "./Middleware.cjs";
3
+
4
+ //#region src/Profiler.d.ts
5
+ /**
6
+ * Profiler
7
+ *
8
+ * A logger for state transitions exceeding a given
9
+ * millisecond threshold
10
+ *
11
+ * ```typescript
12
+ * const AppState = new Galena({}, new Profiler());
13
+ * // or
14
+ * AppState.registerMiddlerware(new Profiler());
15
+ * // or
16
+ * const MyState = new State(4, new Profiler());
17
+ * // or
18
+ * MyState.registerMiddleware(new Profiler());
19
+ * ```
20
+ */
21
+ declare class Profiler<T = any> extends Middleware<T> {
22
+ readonly threshold: number;
23
+ private previousState;
24
+ private startTime;
25
+ constructor(threshold?: number);
26
+ onBeforeUpdate(state: State<T>): void;
27
+ onUpdate(state: State<T>): void;
28
+ }
29
+ //#endregion
30
+ export { Profiler };
31
+ //# sourceMappingURL=Profiler.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Profiler.d.cts","names":[],"sources":["../src/Profiler.ts"],"mappings":";;;;;;AAmBA;;;;;;;;;;;;;;cAAa,QAAA,kBAA0B,UAAA,CAAW,CAAA;EAAA,SAGpB,SAAA;EAAA,QAFpB,aAAA;EAAA,QACA,SAAA;cACoB,SAAA;EAIZ,cAAA,CAAe,KAAA,EAAO,KAAA,CAAM,CAAA;EAK5B,QAAA,CAAS,KAAA,EAAO,KAAA,CAAM,CAAA;AAAA"}
@@ -0,0 +1,31 @@
1
+ import { State } from "./State.mjs";
2
+ import { Middleware } from "./Middleware.mjs";
3
+
4
+ //#region src/Profiler.d.ts
5
+ /**
6
+ * Profiler
7
+ *
8
+ * A logger for state transitions exceeding a given
9
+ * millisecond threshold
10
+ *
11
+ * ```typescript
12
+ * const AppState = new Galena({}, new Profiler());
13
+ * // or
14
+ * AppState.registerMiddlerware(new Profiler());
15
+ * // or
16
+ * const MyState = new State(4, new Profiler());
17
+ * // or
18
+ * MyState.registerMiddleware(new Profiler());
19
+ * ```
20
+ */
21
+ declare class Profiler<T = any> extends Middleware<T> {
22
+ readonly threshold: number;
23
+ private previousState;
24
+ private startTime;
25
+ constructor(threshold?: number);
26
+ onBeforeUpdate(state: State<T>): void;
27
+ onUpdate(state: State<T>): void;
28
+ }
29
+ //#endregion
30
+ export { Profiler };
31
+ //# sourceMappingURL=Profiler.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Profiler.d.mts","names":[],"sources":["../src/Profiler.ts"],"mappings":";;;;;;AAmBA;;;;;;;;;;;;;;cAAa,QAAA,kBAA0B,UAAA,CAAW,CAAA;EAAA,SAGpB,SAAA;EAAA,QAFpB,aAAA;EAAA,QACA,SAAA;cACoB,SAAA;EAIZ,cAAA,CAAe,KAAA,EAAO,KAAA,CAAM,CAAA;EAK5B,QAAA,CAAS,KAAA,EAAO,KAAA,CAAM,CAAA;AAAA"}
@@ -0,0 +1,43 @@
1
+ import { Middleware } from "./Middleware.mjs";
2
+ //#region src/Profiler.ts
3
+ /**
4
+ * Profiler
5
+ *
6
+ * A logger for state transitions exceeding a given
7
+ * millisecond threshold
8
+ *
9
+ * ```typescript
10
+ * const AppState = new Galena({}, new Profiler());
11
+ * // or
12
+ * AppState.registerMiddlerware(new Profiler());
13
+ * // or
14
+ * const MyState = new State(4, new Profiler());
15
+ * // or
16
+ * MyState.registerMiddleware(new Profiler());
17
+ * ```
18
+ */
19
+ var Profiler = class extends Middleware {
20
+ previousState = null;
21
+ startTime = null;
22
+ constructor(threshold = 16) {
23
+ super();
24
+ this.threshold = threshold;
25
+ }
26
+ onBeforeUpdate(state) {
27
+ this.startTime = performance.now();
28
+ this.previousState = state.getSnapshot();
29
+ }
30
+ onUpdate(state) {
31
+ if (this.startTime === null) return;
32
+ const diff = performance.now() - this.startTime;
33
+ if (diff >= this.threshold) {
34
+ console.warn(`A slow state transition of ${diff.toFixed(1)}ms was detected when transitioning the following piece of state`);
35
+ console.log(" %cPrevious State", "color: #26ad65; font-weight: bold", this.previousState);
36
+ console.log(" %cCurrent State ", "color: rgb(17, 118, 249); font-weight: bold", state.getSnapshot());
37
+ }
38
+ }
39
+ };
40
+ //#endregion
41
+ export { Profiler };
42
+
43
+ //# sourceMappingURL=Profiler.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Profiler.mjs","names":[],"sources":["../src/Profiler.ts"],"sourcesContent":["import { Middleware } from \"./Middleware\";\nimport type { State } from \"./State\";\n\n/**\n * Profiler\n *\n * A logger for state transitions exceeding a given\n * millisecond threshold\n *\n * ```typescript\n * const AppState = new Galena({}, new Profiler());\n * // or\n * AppState.registerMiddlerware(new Profiler());\n * // or\n * const MyState = new State(4, new Profiler());\n * // or\n * MyState.registerMiddleware(new Profiler());\n * ```\n */\nexport class Profiler<T = any> extends Middleware<T> {\n private previousState: T | null = null;\n private startTime: null | number = null;\n constructor(public readonly threshold = 16) {\n super();\n }\n\n public override onBeforeUpdate(state: State<T>) {\n this.startTime = performance.now();\n this.previousState = state.getSnapshot();\n }\n\n public override onUpdate(state: State<T>) {\n if (this.startTime === null) {\n return;\n }\n const diff = performance.now() - this.startTime;\n if (diff >= this.threshold) {\n console.warn(\n `A slow state transition of ${diff.toFixed(1)}ms was detected when transitioning the following piece of state`,\n );\n console.log(\n \" %cPrevious State\",\n \"color: #26ad65; font-weight: bold\",\n this.previousState,\n );\n console.log(\n \" %cCurrent State \",\n \"color: rgb(17, 118, 249); font-weight: bold\",\n state.getSnapshot(),\n );\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,IAAa,WAAb,cAAuC,WAAc;CACnD,gBAAkC;CAClC,YAAmC;CACnC,YAAY,YAA4B,IAAI;AAC1C,SAAO;AADmB,OAAA,YAAA;;CAI5B,eAA+B,OAAiB;AAC9C,OAAK,YAAY,YAAY,KAAK;AAClC,OAAK,gBAAgB,MAAM,aAAa;;CAG1C,SAAyB,OAAiB;AACxC,MAAI,KAAK,cAAc,KACrB;EAEF,MAAM,OAAO,YAAY,KAAK,GAAG,KAAK;AACtC,MAAI,QAAQ,KAAK,WAAW;AAC1B,WAAQ,KACN,8BAA8B,KAAK,QAAQ,EAAE,CAAC,iEAC/C;AACD,WAAQ,IACN,uBACA,qCACA,KAAK,cACN;AACD,WAAQ,IACN,0BACA,+CACA,MAAM,aAAa,CACpB"}
package/dist/State.cjs ADDED
@@ -0,0 +1,147 @@
1
+ let _figliolia_event_emitter = require("@figliolia/event-emitter");
2
+ //#region src/State.ts
3
+ /**
4
+ * State
5
+ *
6
+ * The unit of reactivity for Galena. `State`'s can act
7
+ * as isolated instances or be part of your global app
8
+ * state (via `Galena` instances).
9
+ *
10
+ * There are three ways to create state instances
11
+ *
12
+ * ```typescript
13
+ * import { State, createState, useState, Profiler } from "@figliolia/galena";
14
+ * // for island states that can be shared between react components
15
+ * const myState = new State("<any value>", ...middleware);
16
+ * // or
17
+ * const myState = createState("<any value>", ...middleware);
18
+ *
19
+ * myState.set("<new-value>");
20
+ * myState.update(previousValue => "<new-value>");
21
+ * myState.subscribe(nextValue => {});
22
+ * myState.registerMiddleware(new Profiler());
23
+ *
24
+ * // Similarly if you wish to use your state inside a react component
25
+ * const MyComponent = () => {
26
+ * const [state, setState] = useState(myState);
27
+ * // or
28
+ * const [state, setState] = useMyState("<any-value>", ...middlware);
29
+ *
30
+ * return (
31
+ * // your jsx
32
+ * );
33
+ * }
34
+ * ```
35
+ */
36
+ var State = class {
37
+ state;
38
+ middleware = [];
39
+ Emitter = new _figliolia_event_emitter.EventEmitter();
40
+ constructor(initialState, ...middleware) {
41
+ this.initialState = initialState;
42
+ this.state = initialState;
43
+ this.registerMiddleware(...middleware);
44
+ }
45
+ /**
46
+ * Set
47
+ *
48
+ * Updates the current value of state notifying
49
+ * all interested parties
50
+ */
51
+ set = this.withEmission((state) => state);
52
+ /**
53
+ * Update
54
+ *
55
+ * Updates the current value of state using a setter function
56
+ * receiving the previous state as a parameter. Notifies all
57
+ * interested parties
58
+ */
59
+ update = this.withEmission((setter) => {
60
+ if (this.diffSetter(setter)) return setter;
61
+ return setter(this.state);
62
+ });
63
+ /**
64
+ * Reset
65
+ *
66
+ * Resets the current state back to the state which the instance
67
+ * was initialized with. Notifies all interested parties
68
+ */
69
+ reset = this.withEmission(() => this.initialState);
70
+ /**
71
+ * Get Snapshot
72
+ *
73
+ * Returns the current state. Designed for compatibility with
74
+ * `useSyncExternalStore`
75
+ */
76
+ getSnapshot = () => {
77
+ return this.state;
78
+ };
79
+ /**
80
+ * Subscribe
81
+ *
82
+ * Registers a callback to be executed each time state
83
+ * changes. Returns an `unsubscribe` function
84
+ */
85
+ subscribe = (fn) => {
86
+ const ID = this.Emitter.on("change", fn);
87
+ return () => {
88
+ this.Emitter.off("change", ID);
89
+ };
90
+ };
91
+ /**
92
+ * Register Middleware
93
+ *
94
+ * Registers any number of `Middleware` instances on the
95
+ * current instance of `State`. Your middleware will begin
96
+ * executing at the next state transition
97
+ */
98
+ registerMiddleware(...middleware) {
99
+ this.middleware.push(...middleware);
100
+ }
101
+ withEmission(fn) {
102
+ return (...args) => {
103
+ const result = fn(...args);
104
+ if (result instanceof Promise) {
105
+ result.then((resolved) => this.emit(resolved));
106
+ return;
107
+ }
108
+ return this.emit(result);
109
+ };
110
+ }
111
+ emit(nextState) {
112
+ this.invokeMiddleware("onBeforeUpdate");
113
+ this.state = nextState;
114
+ this.Emitter.emit("change", this.state);
115
+ this.invokeMiddleware("onUpdate");
116
+ }
117
+ diffSetter(setter) {
118
+ return typeof setter !== "function";
119
+ }
120
+ invokeMiddleware(fn) {
121
+ for (const middleware of this.middleware) middleware[fn](this);
122
+ }
123
+ };
124
+ /**
125
+ * Create State
126
+ *
127
+ * Returns the unit of reactivity for Galena. `State`'s can act
128
+ * as isolated instances or be part of your global app
129
+ * state (via `Galena` instances);
130
+ *
131
+ * ```typescript
132
+ * import { createState, Profiler } from "@figliolia/galena";
133
+ *
134
+ * const myState = createState("<any value>", ...middleware);
135
+ *
136
+ * myState.set("<new-value>");
137
+ * myState.update(previousValue => "<new-value>");
138
+ * myState.subscribe(nextValue => {});
139
+ * myState.registerMiddleware(new Profiler());
140
+ * ```
141
+ */
142
+ function createState(...args) {
143
+ return new State(...args);
144
+ }
145
+ //#endregion
146
+ exports.State = State;
147
+ exports.createState = createState;
@@ -0,0 +1,114 @@
1
+ import { NonFunction, Setter, Subscriber } from "./types.cjs";
2
+ import { Middleware } from "./Middleware.cjs";
3
+
4
+ //#region src/State.d.ts
5
+ /**
6
+ * State
7
+ *
8
+ * The unit of reactivity for Galena. `State`'s can act
9
+ * as isolated instances or be part of your global app
10
+ * state (via `Galena` instances).
11
+ *
12
+ * There are three ways to create state instances
13
+ *
14
+ * ```typescript
15
+ * import { State, createState, useState, Profiler } from "@figliolia/galena";
16
+ * // for island states that can be shared between react components
17
+ * const myState = new State("<any value>", ...middleware);
18
+ * // or
19
+ * const myState = createState("<any value>", ...middleware);
20
+ *
21
+ * myState.set("<new-value>");
22
+ * myState.update(previousValue => "<new-value>");
23
+ * myState.subscribe(nextValue => {});
24
+ * myState.registerMiddleware(new Profiler());
25
+ *
26
+ * // Similarly if you wish to use your state inside a react component
27
+ * const MyComponent = () => {
28
+ * const [state, setState] = useState(myState);
29
+ * // or
30
+ * const [state, setState] = useMyState("<any-value>", ...middlware);
31
+ *
32
+ * return (
33
+ * // your jsx
34
+ * );
35
+ * }
36
+ * ```
37
+ */
38
+ declare class State<T> {
39
+ readonly initialState: NonFunction<T>;
40
+ private state;
41
+ readonly middleware: Middleware<T>[];
42
+ private readonly Emitter;
43
+ constructor(initialState: NonFunction<T>, ...middleware: Middleware<T>[]);
44
+ /**
45
+ * Set
46
+ *
47
+ * Updates the current value of state notifying
48
+ * all interested parties
49
+ */
50
+ readonly set: (state: NonFunction<T>) => void;
51
+ /**
52
+ * Update
53
+ *
54
+ * Updates the current value of state using a setter function
55
+ * receiving the previous state as a parameter. Notifies all
56
+ * interested parties
57
+ */
58
+ readonly update: (setter: Setter<T>) => void;
59
+ /**
60
+ * Reset
61
+ *
62
+ * Resets the current state back to the state which the instance
63
+ * was initialized with. Notifies all interested parties
64
+ */
65
+ readonly reset: () => void;
66
+ /**
67
+ * Get Snapshot
68
+ *
69
+ * Returns the current state. Designed for compatibility with
70
+ * `useSyncExternalStore`
71
+ */
72
+ readonly getSnapshot: () => NonFunction<T>;
73
+ /**
74
+ * Subscribe
75
+ *
76
+ * Registers a callback to be executed each time state
77
+ * changes. Returns an `unsubscribe` function
78
+ */
79
+ readonly subscribe: (fn: Subscriber<T>) => () => void;
80
+ /**
81
+ * Register Middleware
82
+ *
83
+ * Registers any number of `Middleware` instances on the
84
+ * current instance of `State`. Your middleware will begin
85
+ * executing at the next state transition
86
+ */
87
+ registerMiddleware(...middleware: Middleware<T>[]): void;
88
+ private withEmission;
89
+ private emit;
90
+ protected diffSetter(setter: Setter<T>): setter is NonFunction<T>;
91
+ private invokeMiddleware;
92
+ }
93
+ /**
94
+ * Create State
95
+ *
96
+ * Returns the unit of reactivity for Galena. `State`'s can act
97
+ * as isolated instances or be part of your global app
98
+ * state (via `Galena` instances);
99
+ *
100
+ * ```typescript
101
+ * import { createState, Profiler } from "@figliolia/galena";
102
+ *
103
+ * const myState = createState("<any value>", ...middleware);
104
+ *
105
+ * myState.set("<new-value>");
106
+ * myState.update(previousValue => "<new-value>");
107
+ * myState.subscribe(nextValue => {});
108
+ * myState.registerMiddleware(new Profiler());
109
+ * ```
110
+ */
111
+ declare function createState<T>(...args: ConstructorParameters<typeof State<T>>): State<T>;
112
+ //#endregion
113
+ export { State, createState };
114
+ //# sourceMappingURL=State.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"State.d.cts","names":[],"sources":["../src/State.ts"],"mappings":";;;;;;AAqCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,KAAA;EAAA,SAKO,YAAA,EAAc,WAAA,CAAY,CAAA;EAAA,QAJpC,KAAA;EAAA,SACQ,UAAA,EAAY,UAAA,CAAW,CAAA;EAAA,iBACtB,OAAA;cAEC,YAAA,EAAc,WAAA,CAAY,CAAA,MACvC,UAAA,EAAY,UAAA,CAAW,CAAA;EADI;;;;;;EAAA,SAahB,GAAA,GAAG,KAAA,EAAA,WAAA,CAAA,CAAA;EAAA;;;;;;;EAAA,SASH,MAAA,GAAM,MAAA,EAAA,MAAA,CAAA,CAAA;EAqBN;;;;;;EAAA,SARA,KAAA;EAgCT;;;;;;EAAA,SAxBS,WAAA,QAAW,WAAA,CAAA,CAAA;EAgDE;;;;;;EAAA,SAtCb,SAAA,GAAa,EAAA,EAAI,UAAA,CAAW,CAAA;EA0CpB;;AAyB1B;;;;;EArDS,kBAAA,CAAA,GAAsB,UAAA,EAAY,UAAA,CAAW,CAAA;EAAA,QAI5C,YAAA;EAAA,QAaA,IAAA;EAAA,UAOE,UAAA,CAAW,MAAA,EAAQ,MAAA,CAAO,CAAA,IAAK,MAAA,IAAU,WAAA,CAAY,CAAA;EAAA,QAIvD,gBAAA;AAAA;;;;;;;;;;;;;;;;;;;iBAyBM,WAAA,GAAA,CAAA,GACX,IAAA,EAAM,qBAAA,QAA6B,KAAA,CAAM,CAAA,KAAG,KAAA,CAAA,CAAA"}