kiss-for-react 1.1.0 → 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.
- package/README.md +699 -156
- package/lib/cjs/ClassPersistor.js +177 -112
- package/lib/cjs/Esserializer/DeserializationOptions.js +4 -4
- package/lib/cjs/Esserializer/SerializeOptions.js +4 -4
- package/lib/cjs/Esserializer/builtins.js +258 -0
- package/lib/cjs/Esserializer/constant.js +39 -131
- package/lib/cjs/Esserializer/deserializer.js +226 -313
- package/lib/cjs/Esserializer/general.js +91 -39
- package/lib/cjs/Esserializer/index.js +47 -43
- package/lib/cjs/Esserializer/serializer.js +168 -133
- package/lib/cjs/Evt.js +132 -0
- package/lib/cjs/Hooks.js +1472 -969
- package/lib/cjs/KissAction.js +4635 -1333
- package/lib/cjs/LoadPages.js +336 -0
- package/lib/cjs/Persistor.js +189 -120
- package/lib/cjs/ProcessPersistence.js +579 -262
- package/lib/cjs/Store.js +4278 -1534
- package/lib/cjs/StoreException.js +162 -75
- package/lib/cjs/StoreProvider.js +88 -0
- package/lib/cjs/UserException.js +212 -218
- package/lib/cjs/Wait.js +309 -309
- package/lib/cjs/index.js +69 -42
- package/lib/cjs/server.js +54 -0
- package/lib/cjs/types/ClassPersistor.d.ts +143 -112
- package/lib/cjs/types/ClassPersistor.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/builtins.d.ts +36 -0
- package/lib/cjs/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/constant.d.ts +25 -49
- package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/general.d.ts +28 -4
- package/lib/cjs/types/Esserializer/general.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/index.d.ts +33 -29
- package/lib/cjs/types/Esserializer/index.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/serializer.d.ts +14 -3
- package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/cjs/types/Evt.d.ts +124 -0
- package/lib/cjs/types/Evt.d.ts.map +1 -0
- package/lib/cjs/types/Hooks.d.ts +1174 -837
- package/lib/cjs/types/Hooks.d.ts.map +1 -1
- package/lib/cjs/types/KissAction.d.ts +5199 -1398
- package/lib/cjs/types/KissAction.d.ts.map +1 -1
- package/lib/cjs/types/LoadPages.d.ts +287 -0
- package/lib/cjs/types/LoadPages.d.ts.map +1 -0
- package/lib/cjs/types/Persistor.d.ts +187 -118
- package/lib/cjs/types/Persistor.d.ts.map +1 -1
- package/lib/cjs/types/ProcessPersistence.d.ts +197 -99
- package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/cjs/types/Store.d.ts +2253 -1042
- package/lib/cjs/types/Store.d.ts.map +1 -1
- package/lib/cjs/types/StoreException.d.ts +97 -28
- package/lib/cjs/types/StoreException.d.ts.map +1 -1
- package/lib/cjs/types/StoreProvider.d.ts +80 -0
- package/lib/cjs/types/StoreProvider.d.ts.map +1 -0
- package/lib/cjs/types/UserException.d.ts +178 -169
- package/lib/cjs/types/UserException.d.ts.map +1 -1
- package/lib/cjs/types/index.d.ts +12 -9
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/server.d.ts +12 -0
- package/lib/cjs/types/server.d.ts.map +1 -0
- package/lib/cjs/types/utils.d.ts +15 -1
- package/lib/cjs/types/utils.d.ts.map +1 -1
- package/lib/cjs/utils.js +45 -7
- package/lib/esm/ClassPersistor.mjs +177 -0
- package/lib/esm/Esserializer/{DeserializationOptions.js → DeserializationOptions.mjs} +3 -3
- package/lib/esm/Esserializer/{SerializeOptions.js → SerializeOptions.mjs} +3 -3
- package/lib/esm/Esserializer/builtins.mjs +254 -0
- package/lib/esm/Esserializer/constant.mjs +28 -0
- package/lib/esm/Esserializer/deserializer.mjs +221 -0
- package/lib/esm/Esserializer/general.mjs +81 -0
- package/lib/esm/Esserializer/index.mjs +43 -0
- package/lib/esm/Esserializer/serializer.mjs +164 -0
- package/lib/esm/Evt.mjs +151 -0
- package/lib/esm/Hooks.mjs +1446 -0
- package/lib/esm/KissAction.mjs +4689 -0
- package/lib/esm/LoadPages.mjs +340 -0
- package/lib/esm/Persistor.mjs +180 -0
- package/lib/esm/ProcessPersistence.mjs +581 -0
- package/lib/esm/Store.mjs +4400 -0
- package/lib/esm/StoreException.mjs +153 -0
- package/lib/esm/StoreProvider.mjs +80 -0
- package/lib/esm/{UserException.js → UserException.mjs} +240 -246
- package/lib/esm/{Wait.js → Wait.mjs} +309 -309
- package/lib/esm/index.mjs +12 -9
- package/lib/esm/server.mjs +23 -0
- package/lib/esm/types/ClassPersistor.d.ts +143 -112
- package/lib/esm/types/ClassPersistor.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/builtins.d.ts +36 -0
- package/lib/esm/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/constant.d.ts +25 -49
- package/lib/esm/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/general.d.ts +28 -4
- package/lib/esm/types/Esserializer/general.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/index.d.ts +33 -29
- package/lib/esm/types/Esserializer/index.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/serializer.d.ts +14 -3
- package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/esm/types/Evt.d.ts +124 -0
- package/lib/esm/types/Evt.d.ts.map +1 -0
- package/lib/esm/types/Hooks.d.ts +1174 -837
- package/lib/esm/types/Hooks.d.ts.map +1 -1
- package/lib/esm/types/KissAction.d.ts +5199 -1398
- package/lib/esm/types/KissAction.d.ts.map +1 -1
- package/lib/esm/types/LoadPages.d.ts +287 -0
- package/lib/esm/types/LoadPages.d.ts.map +1 -0
- package/lib/esm/types/Persistor.d.ts +187 -118
- package/lib/esm/types/Persistor.d.ts.map +1 -1
- package/lib/esm/types/ProcessPersistence.d.ts +197 -99
- package/lib/esm/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/esm/types/Store.d.ts +2253 -1042
- package/lib/esm/types/Store.d.ts.map +1 -1
- package/lib/esm/types/StoreException.d.ts +97 -28
- package/lib/esm/types/StoreException.d.ts.map +1 -1
- package/lib/esm/types/StoreProvider.d.ts +80 -0
- package/lib/esm/types/StoreProvider.d.ts.map +1 -0
- package/lib/esm/types/UserException.d.ts +178 -169
- package/lib/esm/types/UserException.d.ts.map +1 -1
- package/lib/esm/types/index.d.ts +12 -9
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/server.d.ts +12 -0
- package/lib/esm/types/server.d.ts.map +1 -0
- package/lib/esm/types/utils.d.ts +15 -1
- package/lib/esm/types/utils.d.ts.map +1 -1
- package/lib/esm/utils.mjs +38 -0
- package/package.json +54 -30
- package/lib/cjs/UnmodifiableSetView.js +0 -53
- package/lib/cjs/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/cjs/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/ClassPersistor.js +0 -112
- package/lib/esm/Esserializer/constant.js +0 -81
- package/lib/esm/Esserializer/deserializer.js +0 -307
- package/lib/esm/Esserializer/general.js +0 -34
- package/lib/esm/Esserializer/index.js +0 -39
- package/lib/esm/Esserializer/serializer.js +0 -130
- package/lib/esm/Hooks.js +0 -955
- package/lib/esm/KissAction.js +0 -1459
- package/lib/esm/Persistor.js +0 -113
- package/lib/esm/ProcessPersistence.js +0 -262
- package/lib/esm/Store.js +0 -1606
- package/lib/esm/StoreException.js +0 -69
- package/lib/esm/UnmodifiableSetView.js +0 -50
- package/lib/esm/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/esm/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/utils.js +0 -3
package/lib/cjs/Evt.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EvtState = exports.Evt = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* An event: a one-time notification kept in the store state, for an action to ask the UI to
|
|
6
|
+
* do something once, that is not a state change: scroll a list to the top, clear or focus an
|
|
7
|
+
* input, open a dialog. The UI keeps these things itself (DOM elements, refs, scroll
|
|
8
|
+
* positions), so the reducer can't reach them.
|
|
9
|
+
*
|
|
10
|
+
* Name event fields with the `Evt` suffix, like `scrollToTopEvt`. In the initial state, use
|
|
11
|
+
* `Evt.spent()`, an event that was already used, so that nothing fires when the app starts.
|
|
12
|
+
* Actions then put a new event in the state:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* class State {
|
|
16
|
+
* constructor(
|
|
17
|
+
* readonly messages: readonly Message[],
|
|
18
|
+
* readonly scrollToTopEvt: Evt, // A boolean event.
|
|
19
|
+
* readonly changeTextEvt: Evt<string>, // An event with a value.
|
|
20
|
+
* ) {}
|
|
21
|
+
*
|
|
22
|
+
* static initialState = new State([], Evt.spent(), Evt.spent());
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* class ScrollFeedToTop extends Action {
|
|
26
|
+
* reduce() { return this.state.copy({ scrollToTopEvt: new Evt() }); }
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* class ChangeText extends Action {
|
|
30
|
+
* async reduce() {
|
|
31
|
+
* const text = await api.fetchText();
|
|
32
|
+
* return (state: State) => state.copy({ changeTextEvt: new Evt(text) });
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* Components handle the event with the `useEvent` hook (or `useLayoutEvent`), which calls the
|
|
38
|
+
* callback once per event, with its value, and then marks the event as **spent**:
|
|
39
|
+
*
|
|
40
|
+
* ```tsx
|
|
41
|
+
* useEvent((state: State) => state.scrollToTopEvt, () => listRef.current?.scrollTo({ top: 0 }));
|
|
42
|
+
* useEvent((state: State) => state.changeTextEvt, (text) => { inputRef.current!.value = text; });
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* `new Evt()` is a boolean event, whose `value` is `true`. `new Evt(value)` holds a value of
|
|
46
|
+
* any type. Each new event is a new object, so a new event always fires, even if its value is
|
|
47
|
+
* the same as the previous one. Reading `value` doesn't spend the event.
|
|
48
|
+
*
|
|
49
|
+
* The event object itself is immutable: the store keeps which events were spent (see
|
|
50
|
+
* `store.isEventSpent`). So the state stays immutable, and two stores that share the same
|
|
51
|
+
* state (in two tests, for example) each spend the event on their own.
|
|
52
|
+
*
|
|
53
|
+
* Each event is handled by one component only: the first one whose effect runs. For two
|
|
54
|
+
* components to react, create two events in the state. Or use `EvtState`, when it's fine to
|
|
55
|
+
* miss events that happen while the component is not mounted.
|
|
56
|
+
*
|
|
57
|
+
* Persistence: the `ClassPersistor` saves events as spent, and restores them as `Evt.spent()`,
|
|
58
|
+
* so an old event doesn't fire when the app starts. A custom persistor must do the same: never
|
|
59
|
+
* save events, or restore them as `Evt.spent()`.
|
|
60
|
+
*
|
|
61
|
+
* You don't need events when:
|
|
62
|
+
* - The component dispatched the action itself. It can wait for the action, and then act:
|
|
63
|
+
* `await dispatchAndWait(new SendMessage(text)); inputRef.current!.value = '';`
|
|
64
|
+
* - There's already a state change to react to. Do it in an effect: for example, scroll to
|
|
65
|
+
* the end when a message arrives: `useEffect(() => scrollToEnd(), [messages.length])`.
|
|
66
|
+
*/
|
|
67
|
+
class Evt {
|
|
68
|
+
/**
|
|
69
|
+
* Creates a new, unspent event. `new Evt()` is a boolean event, whose `value` is `true`.
|
|
70
|
+
* `new Evt(value)` holds a value of any type.
|
|
71
|
+
*/
|
|
72
|
+
constructor(value) {
|
|
73
|
+
this.value = (value === undefined ? true : value);
|
|
74
|
+
this._spentAtCreation = false;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* An event that was already used, for the initial state. It never fires, in any store, and
|
|
78
|
+
* has no value (`undefined`).
|
|
79
|
+
*/
|
|
80
|
+
static spent() {
|
|
81
|
+
const evt = new Evt();
|
|
82
|
+
return Object.assign(evt, { value: undefined, _spentAtCreation: true });
|
|
83
|
+
}
|
|
84
|
+
toString() {
|
|
85
|
+
return this._spentAtCreation ? 'Evt.spent()' : `Evt(${String(this.value)})`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.Evt = Evt;
|
|
89
|
+
/**
|
|
90
|
+
* An event that is never spent, so that all the mounted components that use it react to it,
|
|
91
|
+
* but only while they are mounted. It's an alternative to `Evt`, which fires once, in one
|
|
92
|
+
* component only, even if the component mounts later.
|
|
93
|
+
*
|
|
94
|
+
* Each `new EvtState(value)` is a new object. A component that uses it with `useEvent` (or
|
|
95
|
+
* `useLayoutEvent`) is called when the state has a different `EvtState` than the last one the
|
|
96
|
+
* component saw. So it doesn't fire when the component mounts (or remounts), nor on unrelated
|
|
97
|
+
* re-renders, and it fires in every mounted component that uses it, even if the value is the
|
|
98
|
+
* same as the previous one.
|
|
99
|
+
*
|
|
100
|
+
* In the initial state, use `EvtState.spent()`, which never fires, so that nothing fires after
|
|
101
|
+
* a `logOut` puts the initial state back. The `ClassPersistor` also restores an `EvtState` as
|
|
102
|
+
* `EvtState.spent()`.
|
|
103
|
+
*
|
|
104
|
+
* ```ts
|
|
105
|
+
* class State {
|
|
106
|
+
* constructor(readonly highlightEvt: EvtState<string>) {}
|
|
107
|
+
* static initialState = new State(EvtState.spent());
|
|
108
|
+
* }
|
|
109
|
+
*
|
|
110
|
+
* // In an action:
|
|
111
|
+
* reduce() { return this.state.copy({ highlightEvt: new EvtState(this.itemId) }); }
|
|
112
|
+
*
|
|
113
|
+
* // In any number of components:
|
|
114
|
+
* useEvent((state: State) => state.highlightEvt, (itemId) => highlight(itemId));
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
class EvtState {
|
|
118
|
+
/** Creates a new event. `new EvtState()` has the value `true`. `new EvtState(value)` holds a value. */
|
|
119
|
+
constructor(value) {
|
|
120
|
+
this.value = (value === undefined ? true : value);
|
|
121
|
+
this._spentAtCreation = false;
|
|
122
|
+
}
|
|
123
|
+
/** An event that never fires, for the initial state. It has no value (`undefined`). */
|
|
124
|
+
static spent() {
|
|
125
|
+
const evt = new EvtState();
|
|
126
|
+
return Object.assign(evt, { value: undefined, _spentAtCreation: true });
|
|
127
|
+
}
|
|
128
|
+
toString() {
|
|
129
|
+
return this._spentAtCreation ? 'EvtState.spent()' : `EvtState(${String(this.value)})`;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.EvtState = EvtState;
|