@figliolia/galena 2.3.5 → 3.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 (144) 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/GalenaProvider.cjs +50 -0
  10. package/dist/GalenaProvider.d.cts +35 -0
  11. package/dist/GalenaProvider.d.cts.map +1 -0
  12. package/dist/GalenaProvider.d.mts +35 -0
  13. package/dist/GalenaProvider.d.mts.map +1 -0
  14. package/dist/GalenaProvider.mjs +52 -0
  15. package/dist/GalenaProvider.mjs.map +1 -0
  16. package/dist/Logger.cjs +46 -0
  17. package/dist/Logger.d.cts +35 -0
  18. package/dist/Logger.d.cts.map +1 -0
  19. package/dist/Logger.d.mts +35 -0
  20. package/dist/Logger.d.mts.map +1 -0
  21. package/dist/Logger.mjs +48 -0
  22. package/dist/Logger.mjs.map +1 -0
  23. package/dist/Middleware.cjs +62 -0
  24. package/dist/Middleware.d.cts +65 -0
  25. package/dist/Middleware.d.cts.map +1 -0
  26. package/dist/Middleware.d.mts +65 -0
  27. package/dist/Middleware.d.mts.map +1 -0
  28. package/dist/Middleware.mjs +64 -0
  29. package/dist/Middleware.mjs.map +1 -0
  30. package/dist/Profiler.cjs +41 -0
  31. package/dist/Profiler.d.cts +31 -0
  32. package/dist/Profiler.d.cts.map +1 -0
  33. package/dist/Profiler.d.mts +31 -0
  34. package/dist/Profiler.d.mts.map +1 -0
  35. package/dist/Profiler.mjs +43 -0
  36. package/dist/Profiler.mjs.map +1 -0
  37. package/dist/State.cjs +147 -0
  38. package/dist/State.d.cts +114 -0
  39. package/dist/State.d.cts.map +1 -0
  40. package/dist/State.d.mts +114 -0
  41. package/dist/State.d.mts.map +1 -0
  42. package/dist/State.mjs +148 -0
  43. package/dist/State.mjs.map +1 -0
  44. package/dist/commonHooks.cjs +17 -0
  45. package/dist/commonHooks.mjs +18 -0
  46. package/dist/commonHooks.mjs.map +1 -0
  47. package/dist/connect-multi.cjs +42 -0
  48. package/dist/connect-multi.d.cts +10 -0
  49. package/dist/connect-multi.d.cts.map +1 -0
  50. package/dist/connect-multi.d.mts +10 -0
  51. package/dist/connect-multi.d.mts.map +1 -0
  52. package/dist/connect-multi.mjs +44 -0
  53. package/dist/connect-multi.mjs.map +1 -0
  54. package/dist/connect.cjs +41 -0
  55. package/dist/connect.d.cts +9 -0
  56. package/dist/connect.d.cts.map +1 -0
  57. package/dist/connect.d.mts +9 -0
  58. package/dist/connect.d.mts.map +1 -0
  59. package/dist/connect.mjs +43 -0
  60. package/dist/connect.mjs.map +1 -0
  61. package/dist/createUseState.cjs +47 -0
  62. package/dist/createUseState.d.cts +46 -0
  63. package/dist/createUseState.d.cts.map +1 -0
  64. package/dist/createUseState.d.mts +46 -0
  65. package/dist/createUseState.d.mts.map +1 -0
  66. package/dist/createUseState.mjs +49 -0
  67. package/dist/createUseState.mjs.map +1 -0
  68. package/dist/index.cjs +23 -0
  69. package/dist/index.d.cts +12 -0
  70. package/dist/index.d.mts +12 -0
  71. package/dist/index.mjs +11 -0
  72. package/dist/types.d.cts +23 -0
  73. package/dist/types.d.cts.map +1 -0
  74. package/dist/types.d.mts +23 -0
  75. package/dist/types.d.mts.map +1 -0
  76. package/dist/useState.cjs +45 -0
  77. package/dist/useState.d.cts +43 -0
  78. package/dist/useState.d.cts.map +1 -0
  79. package/dist/useState.d.mts +43 -0
  80. package/dist/useState.d.mts.map +1 -0
  81. package/dist/useState.mjs +47 -0
  82. package/dist/useState.mjs.map +1 -0
  83. package/media/Logging.png +0 -0
  84. package/media/Profiling.png +0 -0
  85. package/package.json +38 -59
  86. package/src/Galena.ts +120 -0
  87. package/src/{Middlewares/Logger.ts → Logger.ts} +15 -14
  88. package/src/Middleware.ts +62 -0
  89. package/src/Profiler.ts +53 -0
  90. package/src/State.ts +167 -0
  91. package/src/index.ts +6 -3
  92. package/src/types.ts +28 -0
  93. package/dist/cjs/Galena/Galena.js +0 -223
  94. package/dist/cjs/Galena/Guards.js +0 -40
  95. package/dist/cjs/Galena/Scheduler.js +0 -84
  96. package/dist/cjs/Galena/State.js +0 -314
  97. package/dist/cjs/Galena/index.js +0 -22
  98. package/dist/cjs/Galena/types.js +0 -9
  99. package/dist/cjs/Middleware/Middleware.js +0 -46
  100. package/dist/cjs/Middleware/index.js +0 -20
  101. package/dist/cjs/Middleware/types.js +0 -8
  102. package/dist/cjs/Middlewares/Logger.js +0 -51
  103. package/dist/cjs/Middlewares/Profiler.js +0 -38
  104. package/dist/cjs/Middlewares/index.js +0 -7
  105. package/dist/cjs/index.js +0 -19
  106. package/dist/cjs/package.json +0 -3
  107. package/dist/mjs/Galena/Galena.js +0 -218
  108. package/dist/mjs/Galena/Guards.js +0 -36
  109. package/dist/mjs/Galena/Scheduler.js +0 -79
  110. package/dist/mjs/Galena/State.js +0 -313
  111. package/dist/mjs/Galena/index.js +0 -3
  112. package/dist/mjs/Galena/types.js +0 -6
  113. package/dist/mjs/Middleware/Middleware.js +0 -42
  114. package/dist/mjs/Middleware/index.js +0 -2
  115. package/dist/mjs/Middleware/types.js +0 -5
  116. package/dist/mjs/Middlewares/Logger.js +0 -44
  117. package/dist/mjs/Middlewares/Profiler.js +0 -35
  118. package/dist/mjs/Middlewares/index.js +0 -2
  119. package/dist/mjs/index.js +0 -3
  120. package/dist/mjs/package.json +0 -4
  121. package/dist/types/Galena/Galena.d.ts +0 -160
  122. package/dist/types/Galena/Guards.d.ts +0 -29
  123. package/dist/types/Galena/Scheduler.d.ts +0 -51
  124. package/dist/types/Galena/State.d.ts +0 -235
  125. package/dist/types/Galena/index.d.ts +0 -3
  126. package/dist/types/Galena/types.d.ts +0 -13
  127. package/dist/types/Middleware/Middleware.d.ts +0 -43
  128. package/dist/types/Middleware/index.d.ts +0 -2
  129. package/dist/types/Middleware/types.d.ts +0 -4
  130. package/dist/types/Middlewares/Logger.d.ts +0 -27
  131. package/dist/types/Middlewares/Profiler.d.ts +0 -22
  132. package/dist/types/Middlewares/index.d.ts +0 -2
  133. package/dist/types/index.d.ts +0 -3
  134. package/src/Galena/Galena.ts +0 -252
  135. package/src/Galena/Guards.ts +0 -49
  136. package/src/Galena/Scheduler.ts +0 -85
  137. package/src/Galena/State.ts +0 -344
  138. package/src/Galena/index.ts +0 -3
  139. package/src/Galena/types.ts +0 -18
  140. package/src/Middleware/Middleware.ts +0 -45
  141. package/src/Middleware/index.ts +0 -2
  142. package/src/Middleware/types.ts +0 -4
  143. package/src/Middlewares/Profiler.ts +0 -41
  144. package/src/Middlewares/index.ts +0 -2
@@ -0,0 +1,35 @@
1
+ import { Middleware } from "./Middleware.mjs";
2
+ import { State } from "./State.mjs";
3
+
4
+ //#region src/Logger.d.ts
5
+ /**
6
+ * Logger
7
+ *
8
+ * A middleware for Redux-style logging! Each state transition
9
+ * will log to the console the `State` instance that changed
10
+ * along with a before and after snapshot of the current state:
11
+ *
12
+ * ```typescript
13
+ * const AppState = new Galena({}, new Logger());
14
+ * // or
15
+ * AppState.registerMiddlerware(new Logger());
16
+ * // or
17
+ * const MyState = new State(4, new Logger());
18
+ * // or
19
+ * MyState.registerMiddleware(new Logger());
20
+ * ```
21
+ */
22
+ declare class Logger<T = any> extends Middleware {
23
+ private previousState;
24
+ onBeforeUpdate(state: State<T>): void;
25
+ onUpdate(state: State<T>): void;
26
+ /**
27
+ * Time
28
+ *
29
+ * Returns the time in which a given state transition completed
30
+ */
31
+ private get time();
32
+ }
33
+ //#endregion
34
+ export { Logger };
35
+ //# sourceMappingURL=Logger.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Logger.d.mts","names":[],"sources":["../src/Logger.ts"],"mappings":";;;;;;AAoBA;;;;;;;;;;;;;;;cAAa,MAAA,kBAAwB,UAAA;EAAA,QAC3B,aAAA;EAEC,cAAA,CAAe,KAAA,EAAO,KAAA,CAAM,CAAA;EAI5B,QAAA,CAAS,KAAA,EAAO,KAAA,CAAM,CAAA;EAAA;;;;;EAAA,YAwBnB,IAAA,CAAA;AAAA"}
@@ -0,0 +1,48 @@
1
+ import { Middleware } from "./Middleware.mjs";
2
+ //#region src/Logger.ts
3
+ /**
4
+ * Logger
5
+ *
6
+ * A middleware for Redux-style logging! Each state transition
7
+ * will log to the console the `State` instance that changed
8
+ * along with a before and after snapshot of the current state:
9
+ *
10
+ * ```typescript
11
+ * const AppState = new Galena({}, new Logger());
12
+ * // or
13
+ * AppState.registerMiddlerware(new Logger());
14
+ * // or
15
+ * const MyState = new State(4, new Logger());
16
+ * // or
17
+ * MyState.registerMiddleware(new Logger());
18
+ * ```
19
+ */
20
+ var Logger = class extends Middleware {
21
+ previousState = null;
22
+ onBeforeUpdate(state) {
23
+ this.previousState = state.getSnapshot();
24
+ }
25
+ onUpdate(state) {
26
+ console.log("%cMutation:", "color: rgb(187, 186, 186); font-weight: bold", "@", this.time);
27
+ console.log(" %cPrevious State", "color: #26ad65; font-weight: bold", this.previousState);
28
+ console.log(" %cNext State ", "color: rgb(17, 118, 249); font-weight: bold", state.getSnapshot());
29
+ }
30
+ /**
31
+ * Time
32
+ *
33
+ * Returns the time in which a given state transition completed
34
+ */
35
+ get time() {
36
+ const date = /* @__PURE__ */ new Date();
37
+ const mHours = date.getHours();
38
+ const hours = mHours > 12 ? mHours - 12 : mHours;
39
+ const mins = date.getMinutes();
40
+ const minutes = mins.toString().length === 1 ? `0${mins}` : mins;
41
+ const secs = date.getSeconds();
42
+ return `${hours}:${minutes}:${secs.toString().length === 1 ? `0${secs}` : secs}:${date.getMilliseconds()}`;
43
+ }
44
+ };
45
+ //#endregion
46
+ export { Logger };
47
+
48
+ //# sourceMappingURL=Logger.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Logger.mjs","names":[],"sources":["../src/Logger.ts"],"sourcesContent":["import { Middleware } from \"./Middleware\";\nimport type { State } from \"./State\";\n\n/**\n * Logger\n *\n * A middleware for Redux-style logging! Each state transition\n * will log to the console the `State` instance that changed\n * along with a before and after snapshot of the current state:\n *\n * ```typescript\n * const AppState = new Galena({}, new Logger());\n * // or\n * AppState.registerMiddlerware(new Logger());\n * // or\n * const MyState = new State(4, new Logger());\n * // or\n * MyState.registerMiddleware(new Logger());\n * ```\n */\nexport class Logger<T = any> extends Middleware {\n private previousState: T | null = null;\n\n override onBeforeUpdate(state: State<T>) {\n this.previousState = state.getSnapshot();\n }\n\n override onUpdate(state: State<T>) {\n console.log(\n \"%cMutation:\",\n \"color: rgb(187, 186, 186); font-weight: bold\",\n \"@\",\n this.time,\n );\n console.log(\n \" %cPrevious State\",\n \"color: #26ad65; font-weight: bold\",\n this.previousState,\n );\n console.log(\n \" %cNext State \",\n \"color: rgb(17, 118, 249); font-weight: bold\",\n state.getSnapshot(),\n );\n }\n\n /**\n * Time\n *\n * Returns the time in which a given state transition completed\n */\n private get time() {\n const date = new Date();\n const mHours = date.getHours();\n const hours = mHours > 12 ? mHours - 12 : mHours;\n const mins = date.getMinutes();\n const minutes = mins.toString().length === 1 ? `0${mins}` : mins;\n const secs = date.getSeconds();\n const seconds = secs.toString().length === 1 ? `0${secs}` : secs;\n const milliseconds = date.getMilliseconds();\n return `${hours}:${minutes}:${seconds}:${milliseconds}`;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAoBA,IAAa,SAAb,cAAqC,WAAW;CAC9C,gBAAkC;CAElC,eAAwB,OAAiB;AACvC,OAAK,gBAAgB,MAAM,aAAa;;CAG1C,SAAkB,OAAiB;AACjC,UAAQ,IACN,eACA,gDACA,KACA,KAAK,KACN;AACD,UAAQ,IACN,uBACA,qCACA,KAAK,cACN;AACD,UAAQ,IACN,uBACA,+CACA,MAAM,aAAa,CACpB;;;;;;;CAQH,IAAY,OAAO;EACjB,MAAM,uBAAO,IAAI,MAAM;EACvB,MAAM,SAAS,KAAK,UAAU;EAC9B,MAAM,QAAQ,SAAS,KAAK,SAAS,KAAK;EAC1C,MAAM,OAAO,KAAK,YAAY;EAC9B,MAAM,UAAU,KAAK,UAAU,CAAC,WAAW,IAAI,IAAI,SAAS;EAC5D,MAAM,OAAO,KAAK,YAAY;AAG9B,SAAO,GAAG,MAAM,GAAG,QAAQ,GAFX,KAAK,UAAU,CAAC,WAAW,IAAI,IAAI,SAAS,KAEtB,GADjB,KAAK,iBAAiB"}
@@ -0,0 +1,62 @@
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
+ exports.Middleware = Middleware;
@@ -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}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 { Middleware } from "./Middleware.cjs";
2
+ import { State } from "./State.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 { Middleware } from "./Middleware.mjs";
2
+ import { State } from "./State.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}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}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,iEACpC;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;