kiss-for-react 1.0.3 → 2.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 (155) hide show
  1. package/README.md +699 -156
  2. package/lib/cjs/ClassPersistor.js +177 -112
  3. package/lib/cjs/Esserializer/DeserializationOptions.js +4 -4
  4. package/lib/cjs/Esserializer/SerializeOptions.js +4 -4
  5. package/lib/cjs/Esserializer/builtins.js +258 -0
  6. package/lib/cjs/Esserializer/constant.js +39 -131
  7. package/lib/cjs/Esserializer/deserializer.js +226 -313
  8. package/lib/cjs/Esserializer/general.js +91 -39
  9. package/lib/cjs/Esserializer/index.js +47 -43
  10. package/lib/cjs/Esserializer/serializer.js +168 -133
  11. package/lib/cjs/Evt.js +132 -0
  12. package/lib/cjs/Hooks.js +1472 -969
  13. package/lib/cjs/KissAction.js +4635 -1333
  14. package/lib/cjs/LoadPages.js +336 -0
  15. package/lib/cjs/Persistor.js +189 -120
  16. package/lib/cjs/ProcessPersistence.js +579 -262
  17. package/lib/cjs/Store.js +4278 -1534
  18. package/lib/cjs/StoreException.js +162 -75
  19. package/lib/cjs/StoreProvider.js +88 -0
  20. package/lib/cjs/UserException.js +212 -218
  21. package/lib/cjs/Wait.js +309 -309
  22. package/lib/cjs/index.js +69 -42
  23. package/lib/cjs/server.js +54 -0
  24. package/lib/cjs/types/ClassPersistor.d.ts +143 -112
  25. package/lib/cjs/types/ClassPersistor.d.ts.map +1 -1
  26. package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +13 -6
  27. package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
  28. package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +11 -5
  29. package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -1
  30. package/lib/cjs/types/Esserializer/builtins.d.ts +36 -0
  31. package/lib/cjs/types/Esserializer/builtins.d.ts.map +1 -0
  32. package/lib/cjs/types/Esserializer/constant.d.ts +25 -49
  33. package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -1
  34. package/lib/cjs/types/Esserializer/deserializer.d.ts +27 -17
  35. package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
  36. package/lib/cjs/types/Esserializer/general.d.ts +28 -4
  37. package/lib/cjs/types/Esserializer/general.d.ts.map +1 -1
  38. package/lib/cjs/types/Esserializer/index.d.ts +33 -29
  39. package/lib/cjs/types/Esserializer/index.d.ts.map +1 -1
  40. package/lib/cjs/types/Esserializer/serializer.d.ts +14 -3
  41. package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -1
  42. package/lib/cjs/types/Evt.d.ts +124 -0
  43. package/lib/cjs/types/Evt.d.ts.map +1 -0
  44. package/lib/cjs/types/Hooks.d.ts +1174 -837
  45. package/lib/cjs/types/Hooks.d.ts.map +1 -1
  46. package/lib/cjs/types/KissAction.d.ts +5199 -1398
  47. package/lib/cjs/types/KissAction.d.ts.map +1 -1
  48. package/lib/cjs/types/LoadPages.d.ts +287 -0
  49. package/lib/cjs/types/LoadPages.d.ts.map +1 -0
  50. package/lib/cjs/types/Persistor.d.ts +187 -118
  51. package/lib/cjs/types/Persistor.d.ts.map +1 -1
  52. package/lib/cjs/types/ProcessPersistence.d.ts +197 -99
  53. package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -1
  54. package/lib/cjs/types/Store.d.ts +2253 -1042
  55. package/lib/cjs/types/Store.d.ts.map +1 -1
  56. package/lib/cjs/types/StoreException.d.ts +97 -28
  57. package/lib/cjs/types/StoreException.d.ts.map +1 -1
  58. package/lib/cjs/types/StoreProvider.d.ts +80 -0
  59. package/lib/cjs/types/StoreProvider.d.ts.map +1 -0
  60. package/lib/cjs/types/UserException.d.ts +178 -169
  61. package/lib/cjs/types/UserException.d.ts.map +1 -1
  62. package/lib/cjs/types/index.d.ts +12 -9
  63. package/lib/cjs/types/index.d.ts.map +1 -1
  64. package/lib/cjs/types/server.d.ts +12 -0
  65. package/lib/cjs/types/server.d.ts.map +1 -0
  66. package/lib/cjs/types/utils.d.ts +15 -1
  67. package/lib/cjs/types/utils.d.ts.map +1 -1
  68. package/lib/cjs/utils.js +45 -7
  69. package/lib/esm/ClassPersistor.mjs +177 -0
  70. package/lib/esm/Esserializer/{DeserializationOptions.js → DeserializationOptions.mjs} +3 -3
  71. package/lib/esm/Esserializer/{SerializeOptions.js → SerializeOptions.mjs} +3 -3
  72. package/lib/esm/Esserializer/builtins.mjs +254 -0
  73. package/lib/esm/Esserializer/constant.mjs +28 -0
  74. package/lib/esm/Esserializer/deserializer.mjs +221 -0
  75. package/lib/esm/Esserializer/general.mjs +81 -0
  76. package/lib/esm/Esserializer/index.mjs +43 -0
  77. package/lib/esm/Esserializer/serializer.mjs +164 -0
  78. package/lib/esm/Evt.mjs +151 -0
  79. package/lib/esm/Hooks.mjs +1446 -0
  80. package/lib/esm/KissAction.mjs +4689 -0
  81. package/lib/esm/LoadPages.mjs +340 -0
  82. package/lib/esm/Persistor.mjs +180 -0
  83. package/lib/esm/ProcessPersistence.mjs +581 -0
  84. package/lib/esm/Store.mjs +4400 -0
  85. package/lib/esm/StoreException.mjs +153 -0
  86. package/lib/esm/StoreProvider.mjs +80 -0
  87. package/lib/esm/{UserException.js → UserException.mjs} +240 -246
  88. package/lib/esm/{Wait.js → Wait.mjs} +309 -309
  89. package/lib/esm/index.mjs +12 -9
  90. package/lib/esm/server.mjs +23 -0
  91. package/lib/esm/types/ClassPersistor.d.ts +143 -112
  92. package/lib/esm/types/ClassPersistor.d.ts.map +1 -1
  93. package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +13 -6
  94. package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
  95. package/lib/esm/types/Esserializer/SerializeOptions.d.ts +11 -5
  96. package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -1
  97. package/lib/esm/types/Esserializer/builtins.d.ts +36 -0
  98. package/lib/esm/types/Esserializer/builtins.d.ts.map +1 -0
  99. package/lib/esm/types/Esserializer/constant.d.ts +25 -49
  100. package/lib/esm/types/Esserializer/constant.d.ts.map +1 -1
  101. package/lib/esm/types/Esserializer/deserializer.d.ts +27 -17
  102. package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
  103. package/lib/esm/types/Esserializer/general.d.ts +28 -4
  104. package/lib/esm/types/Esserializer/general.d.ts.map +1 -1
  105. package/lib/esm/types/Esserializer/index.d.ts +33 -29
  106. package/lib/esm/types/Esserializer/index.d.ts.map +1 -1
  107. package/lib/esm/types/Esserializer/serializer.d.ts +14 -3
  108. package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -1
  109. package/lib/esm/types/Evt.d.ts +124 -0
  110. package/lib/esm/types/Evt.d.ts.map +1 -0
  111. package/lib/esm/types/Hooks.d.ts +1174 -837
  112. package/lib/esm/types/Hooks.d.ts.map +1 -1
  113. package/lib/esm/types/KissAction.d.ts +5199 -1398
  114. package/lib/esm/types/KissAction.d.ts.map +1 -1
  115. package/lib/esm/types/LoadPages.d.ts +287 -0
  116. package/lib/esm/types/LoadPages.d.ts.map +1 -0
  117. package/lib/esm/types/Persistor.d.ts +187 -118
  118. package/lib/esm/types/Persistor.d.ts.map +1 -1
  119. package/lib/esm/types/ProcessPersistence.d.ts +197 -99
  120. package/lib/esm/types/ProcessPersistence.d.ts.map +1 -1
  121. package/lib/esm/types/Store.d.ts +2253 -1042
  122. package/lib/esm/types/Store.d.ts.map +1 -1
  123. package/lib/esm/types/StoreException.d.ts +97 -28
  124. package/lib/esm/types/StoreException.d.ts.map +1 -1
  125. package/lib/esm/types/StoreProvider.d.ts +80 -0
  126. package/lib/esm/types/StoreProvider.d.ts.map +1 -0
  127. package/lib/esm/types/UserException.d.ts +178 -169
  128. package/lib/esm/types/UserException.d.ts.map +1 -1
  129. package/lib/esm/types/index.d.ts +12 -9
  130. package/lib/esm/types/index.d.ts.map +1 -1
  131. package/lib/esm/types/server.d.ts +12 -0
  132. package/lib/esm/types/server.d.ts.map +1 -0
  133. package/lib/esm/types/utils.d.ts +15 -1
  134. package/lib/esm/types/utils.d.ts.map +1 -1
  135. package/lib/esm/utils.mjs +38 -0
  136. package/package.json +55 -31
  137. package/lib/cjs/UnmodifiableSetView.js +0 -53
  138. package/lib/cjs/types/UnmodifiableSetView.d.ts +0 -24
  139. package/lib/cjs/types/UnmodifiableSetView.d.ts.map +0 -1
  140. package/lib/esm/ClassPersistor.js +0 -112
  141. package/lib/esm/Esserializer/constant.js +0 -81
  142. package/lib/esm/Esserializer/deserializer.js +0 -307
  143. package/lib/esm/Esserializer/general.js +0 -34
  144. package/lib/esm/Esserializer/index.js +0 -39
  145. package/lib/esm/Esserializer/serializer.js +0 -130
  146. package/lib/esm/Hooks.js +0 -955
  147. package/lib/esm/KissAction.js +0 -1459
  148. package/lib/esm/Persistor.js +0 -113
  149. package/lib/esm/ProcessPersistence.js +0 -262
  150. package/lib/esm/Store.js +0 -1606
  151. package/lib/esm/StoreException.js +0 -69
  152. package/lib/esm/UnmodifiableSetView.js +0 -50
  153. package/lib/esm/types/UnmodifiableSetView.d.ts +0 -24
  154. package/lib/esm/types/UnmodifiableSetView.d.ts.map +0 -1
  155. package/lib/esm/utils.js +0 -3
@@ -1,113 +0,0 @@
1
- import { KissAction } from './KissAction';
2
- import { Store } from './Store';
3
- /**
4
- * Use it like this:
5
- *
6
- * ```ts
7
- * const persistor = new MyPersistor();
8
- *
9
- * let initialState = await persistor.readState();
10
- *
11
- * if (initialState === null) {
12
- * initialState = AppState.initialState();
13
- * await persistor.saveInitialState(initialState);
14
- * }
15
- *
16
- * const store = createStore<AppState>({
17
- * initialState: initialState,
18
- * persistor: persistor,
19
- * });
20
- * ```
21
- *
22
- * IMPORTANT: When the store is created with a Persistor, it assumes
23
- * the provided initial state was already persisted. Ensure this is the case.
24
- */
25
- export class Persistor {
26
- /**
27
- * The default throttle is 2 seconds (2000 milliseconds).
28
- * Return `null` to turn off the throttle.
29
- */
30
- get throttle() {
31
- return 2000; // Default throttle is 2 seconds.
32
- }
33
- }
34
- /**
35
- * A decorator to print persistor information to the console.
36
- * Use it like this:
37
- *
38
- * ```ts
39
- * const store = createStore<AppState>({
40
- * ...otherOptions,
41
- * persistor: new PersistorPrinterDecorator<AppState>(persistor),
42
- * });
43
- * ```
44
- */
45
- export class PersistorPrinterDecorator {
46
- _persistor;
47
- constructor(persistor) {
48
- this._persistor = persistor;
49
- }
50
- async readState() {
51
- Store.log('Persistor: read state.');
52
- return this._persistor.readState();
53
- }
54
- async deleteState() {
55
- Store.log('Persistor: delete state.');
56
- return this._persistor.deleteState();
57
- }
58
- async persistDifference(lastPersistedState, newState) {
59
- Store.log(`Persistor: persist difference:
60
- lastPersistedState = ${lastPersistedState}
61
- newState = ${newState}`);
62
- return this._persistor.persistDifference(lastPersistedState, newState);
63
- }
64
- async saveInitialState(state) {
65
- Store.log('Persistor: save initial state.');
66
- return this._persistor.saveInitialState(state);
67
- }
68
- get throttle() {
69
- return this._persistor.throttle;
70
- }
71
- }
72
- /**
73
- * A dummy persistor.
74
- */
75
- export class PersistorDummy {
76
- async readState() {
77
- return null;
78
- }
79
- async deleteState() {
80
- return;
81
- }
82
- async persistDifference(_lastPersistedState, _newState) {
83
- return;
84
- }
85
- async saveInitialState(_state) {
86
- return;
87
- }
88
- get throttle() {
89
- return null;
90
- }
91
- }
92
- export class PersistException extends Error {
93
- constructor(message) {
94
- super(message);
95
- this.name = 'PersistException';
96
- // Maintains proper stack trace for where our error was thrown (only available on V8).
97
- if (Error.captureStackTrace) {
98
- Error.captureStackTrace(this, PersistException);
99
- }
100
- }
101
- }
102
- /**
103
- * An action that can be dispatched to force the Persistor to persist the state immediately.
104
- * It does not change the state.
105
- *
106
- * Note: This only works if the Persistor is waiting for the throttle period.
107
- * If the Persistor is currently persisting the state, this action will be ignored.
108
- */
109
- export class PersistAction extends KissAction {
110
- reduce() {
111
- return null;
112
- }
113
- }
@@ -1,262 +0,0 @@
1
- import { PersistAction } from "./Persistor";
2
- import { UpdateStateAction } from "./KissAction";
3
- import { Store } from "./Store";
4
- export class ProcessPersistence {
5
- persistor;
6
- lastPersistedState;
7
- newestState;
8
- isPersisting = false;
9
- isANewStateAvailable = false;
10
- lastPersistTime = new Date();
11
- timer; // Timer's type
12
- isPaused = false;
13
- isInit = false;
14
- finishedPersistingCallback = null;
15
- constructor(persistor, lastPersistedState) {
16
- this.persistor = persistor;
17
- this.lastPersistedState = lastPersistedState;
18
- this.newestState = null;
19
- }
20
- async readInitialState(store, initialState) {
21
- let stateReadFromPersistor = null;
22
- try {
23
- stateReadFromPersistor = await this.persistor.readState();
24
- }
25
- catch (error) {
26
- this.log('Error reading state:' + error + '. State will reset.');
27
- try {
28
- await this.persistor.deleteState();
29
- }
30
- catch (error) {
31
- this.log('Error deleting the state:' + error + '.');
32
- }
33
- }
34
- if (stateReadFromPersistor === null) {
35
- try {
36
- // If it was not possible to read the persisted state,
37
- // we persist the initial-state passed to the Store constructor.
38
- await this.persistor.saveInitialState(initialState);
39
- }
40
- catch (error) {
41
- this.log('Error saving initial state:' + error + '.');
42
- }
43
- }
44
- //
45
- else {
46
- // If the saved state was read successfully, we replace the store state with it.
47
- // In this case, the initial-state passed in the Store constructor was used
48
- // only while the persisted state is loading.
49
- await store.dispatchAndWait(new UpdateStateAction((_) => stateReadFromPersistor, false));
50
- }
51
- }
52
- log(message) {
53
- try {
54
- Store.log(message);
55
- }
56
- catch (error) {
57
- // Discard error.
58
- }
59
- }
60
- get throttle() {
61
- return this.persistor.throttle || 0;
62
- }
63
- async saveInitialState(initialState) {
64
- this.lastPersistedState = initialState;
65
- await this.persistor.saveInitialState(initialState);
66
- }
67
- /**
68
- * Same as `Persistor.readState` but will remember the read state as the `lastPersistedState`.
69
- */
70
- async readState() {
71
- const state = await this.persistor.readState();
72
- this.lastPersistedState = state;
73
- return state;
74
- }
75
- /**
76
- * Same as `Persistor.deleteState` but will clear the `lastPersistedState`.
77
- */
78
- async deleteState() {
79
- this.lastPersistedState = null;
80
- await this.persistor.deleteState();
81
- }
82
- /**
83
- * Call `logOut()` when you want to delete the persisted state, and return the store
84
- * state to the given initial-state. That's usually necessary when the user logs out
85
- * of your app, or the user deletes its account, so that another user may log in,
86
- * or start a new sign-up process.
87
- *
88
- * Note: If you know about any timers or async processes that you may have started,
89
- * you should stop/cancel them all before calling this method.
90
- *
91
- * You may opt to:
92
- *
93
- * - Wait for `throttle` milliseconds to make sure all async processes that the app may
94
- * have started have time to finish. The default `throttle` is 3000 milliseconds (3 seconds).
95
- *
96
- * - Wait for all actions currently running to finish, but wait at most `actionsThrottle`
97
- * milliseconds. If the actions are not finished by then, the state will be deleted anyway.
98
- * The default `actionsThrottle` is 6000 milliseconds (6 seconds).
99
- */
100
- async logOut({ store, initialState, throttle = 3000, actionsThrottle = 6000, }) {
101
- // Pauses the persistor, so it doesn't start a new persistence process.
102
- this.pause();
103
- // Cancel the persistence timer.
104
- this._cancelTimer();
105
- // Set the store to shut down, so it doesn't start any new actions.
106
- store.setShutDown(true);
107
- // If the state is NOT currently being persisted,
108
- if (!this.isPersisting) {
109
- // Clear the callback that is called when it finishes persisting.
110
- this.finishedPersistingCallback = null;
111
- // Wait for the throttle period to finish.
112
- await new Promise(resolve => setTimeout(resolve, throttle));
113
- // Wait for all actions currently running to finish,
114
- // but wait at most `actionsThrottle` milliseconds.
115
- await store.waitAllActions(null, {
116
- timeoutMillis: actionsThrottle,
117
- completeImmediately: true
118
- })
119
- .catch((error) => {
120
- });
121
- // Delete the old persisted state.
122
- await this.deleteState();
123
- // Synchronously change the store state to the initial-state.
124
- store.dispatchSync(new UpdateStateAction((state) => initialState));
125
- // Persist the new initial-state.
126
- await this._persist(new Date(), initialState).then();
127
- // Restart the store accepting new actions.
128
- store.setShutDown(false);
129
- }
130
- //
131
- // If it's currently being persisted, we can't delete the state right now.
132
- else {
133
- // But as soon as it finishes persisting, try again.
134
- this.finishedPersistingCallback = async () => {
135
- await this.logOut({
136
- store, initialState, throttle, actionsThrottle
137
- });
138
- };
139
- }
140
- }
141
- /**
142
- * 1) If we're still persisting the last time, don't persist no matter what.
143
- * 2) If action is of type `UpdateStateAction` with `ifPersists` false,
144
- * don't persist, but consider the state as persisted.
145
- * 3) If throttle period is done (or if action is PersistAction), persist.
146
- * 4) If throttle period is NOT done, create a timer to persist as soon as it finishes.
147
- *
148
- * Return true if the persist process started.
149
- * Return false if persistence was postponed.
150
- *
151
- */
152
- process(action, newState) {
153
- this.isInit = true;
154
- this.newestState = newState;
155
- if (this.isPaused || this.lastPersistedState === newState)
156
- return false;
157
- if ((action instanceof UpdateStateAction) && (!action.ifPersists)) {
158
- this.lastPersistedState = this.newestState;
159
- return false;
160
- }
161
- // 1) If we're still persisting the last time, don't persist no matter what.
162
- if (this.isPersisting) {
163
- this.isANewStateAvailable = true;
164
- return false;
165
- }
166
- //
167
- else {
168
- const now = new Date();
169
- // 2) If throttle period is done (or if action is PersistAction), persist.
170
- if (now.valueOf() - this.lastPersistTime.valueOf() >= this.throttle
171
- || (action instanceof PersistAction)) {
172
- this._cancelTimer();
173
- this._persist(now, newState).then();
174
- return true;
175
- }
176
- // 3) If throttle period is NOT done, create a timer to persist as soon as it finishes.
177
- else {
178
- if (!this.timer) {
179
- const asSoonAsThrottleFinishes = this.throttle - (now.valueOf() - this.lastPersistTime.valueOf());
180
- this.timer = setTimeout(() => {
181
- this.timer = undefined;
182
- this.process(null, this.newestState); // TypeScript forced cast
183
- }, asSoonAsThrottleFinishes);
184
- }
185
- return false;
186
- }
187
- }
188
- }
189
- _cancelTimer() {
190
- if (this.timer) {
191
- clearTimeout(this.timer);
192
- this.timer = undefined;
193
- }
194
- }
195
- async _persist(now, newState) {
196
- this.isPersisting = true;
197
- this.lastPersistTime = now;
198
- this.isANewStateAvailable = false;
199
- try {
200
- await this.persistor.persistDifference(this.lastPersistedState, newState);
201
- }
202
- //
203
- finally {
204
- this.lastPersistedState = newState;
205
- this.isPersisting = false;
206
- this.finishedPersistingCallback?.();
207
- // If a new state became available while the present state was saving, save again.
208
- if (this.isANewStateAvailable) {
209
- this.isANewStateAvailable = false;
210
- this.process(null, this.newestState);
211
- }
212
- }
213
- }
214
- /**
215
- * Pause the `Persistor` temporarily.
216
- *
217
- * When `pause` is called, the Persistor will not start a new persistence process, until method
218
- * `resume` is called. This will not affect the current persistence process, if one is currently
219
- * running.
220
- *
221
- * Note: A persistence process starts when the `persistDifference` method is called, and
222
- * finishes when the promise returned by that method completes.
223
- *
224
- */
225
- pause() {
226
- this.isPaused = true;
227
- }
228
- /**
229
- * Persists the current state (if it's not yet persisted), then pauses the `Persistor`
230
- * temporarily.
231
- *
232
- *
233
- * When `persistAndPause` is called, this will not affect the current persistence process, if
234
- * one is currently running. If no persistence process was running, it will immediately start a
235
- * new persistence process (ignoring `throttle`).
236
- *
237
- * Then, the Persistor will not start another persistence process, until method `resume` is
238
- * called.
239
- *
240
- * Note: A persistence process starts when the `persistDifference` method is called, and
241
- * finishes when the promise returned by that method completes.
242
- *
243
- */
244
- async persistAndPause() {
245
- this.isPaused = true;
246
- this._cancelTimer();
247
- if (this.isInit //
248
- && !this.isPersisting //
249
- && (this.lastPersistedState !== this.newestState)) {
250
- const now = new Date();
251
- return this._persist(now, this.newestState); // TypeScript forced cast
252
- }
253
- }
254
- /**
255
- * Resumes persistence by the `Persistor`, after calling `pause` or `persistAndPause`.
256
- */
257
- resume() {
258
- this.isPaused = false;
259
- if (this.isInit)
260
- this.process(null, this.newestState);
261
- }
262
- }