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