logic-runtime-react-z 3.1.3 → 3.1.5
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 +25 -15
- package/build/logic/createLogic.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -90,6 +90,11 @@ const counterLogic = createLogic({
|
|
|
90
90
|
})
|
|
91
91
|
})
|
|
92
92
|
},
|
|
93
|
+
actions: {
|
|
94
|
+
add({ emit }) {
|
|
95
|
+
return (n: number) => emit("add", n)
|
|
96
|
+
}
|
|
97
|
+
},
|
|
93
98
|
})
|
|
94
99
|
|
|
95
100
|
async function main() {
|
|
@@ -144,22 +149,27 @@ You have **2 integration styles**:
|
|
|
144
149
|
Keeps view pure and declarative.
|
|
145
150
|
|
|
146
151
|
```tsx
|
|
152
|
+
// full see: LIVE EXAMPLE
|
|
147
153
|
import { withLogic, LogicViewProps } from "logic-runtime-react-z"
|
|
148
154
|
import { counterLogic } from "./counter.logic"
|
|
149
155
|
|
|
156
|
+
interface MyProps {}
|
|
157
|
+
|
|
150
158
|
type CounterInjected =
|
|
151
|
-
LogicViewProps<typeof counterLogic>
|
|
159
|
+
LogicViewProps<typeof counterLogic> & MyProps
|
|
152
160
|
|
|
153
|
-
const CounterView = ({ state,
|
|
161
|
+
const CounterView = ({ state, actions, emit }: LogicViewProps) => {
|
|
162
|
+
// actions and emit => same emit (update state)
|
|
154
163
|
return (
|
|
155
164
|
<div>
|
|
156
165
|
<h2>Count: {state.count}</h2>
|
|
157
|
-
<p>Double: {
|
|
158
|
-
<p>Triple: {
|
|
166
|
+
<p>Double: {state.double}</p>
|
|
167
|
+
<p>Triple: {state.triple}</p>
|
|
159
168
|
|
|
160
|
-
<button onClick={() =>
|
|
161
|
-
<button onClick={() =>
|
|
162
|
-
<button onClick={() =>
|
|
169
|
+
<button onClick={() => emit("inc")}>+</button>
|
|
170
|
+
<button onClick={() => emit("dec")}>-</button>
|
|
171
|
+
<button onClick={() => actions.add(10)}>+10 (action)</button>
|
|
172
|
+
<button onClick={() => emit("asyncInc")}>
|
|
163
173
|
{state.loading ? "Loading..." : "Async +"}
|
|
164
174
|
</button>
|
|
165
175
|
</div>
|
|
@@ -188,17 +198,17 @@ import { useLogic } from "logic-runtime-react-z"
|
|
|
188
198
|
import { counterLogic } from "./counter.logic"
|
|
189
199
|
|
|
190
200
|
export function Counter() {
|
|
191
|
-
const { state,
|
|
201
|
+
const { state, actions, emit } = useLogic(counterLogic)
|
|
192
202
|
|
|
193
203
|
return (
|
|
194
204
|
<div>
|
|
195
205
|
<h2>Count: {state.count}</h2>
|
|
196
|
-
<p>Double: {
|
|
197
|
-
<p>Triple: {
|
|
206
|
+
<p>Double: {state.double}</p>
|
|
207
|
+
<p>Triple: {state.triple}</p>
|
|
198
208
|
|
|
199
|
-
<button onClick={() =>
|
|
200
|
-
<button onClick={() =>
|
|
201
|
-
<button onClick={() =>
|
|
209
|
+
<button onClick={() => emit("inc")}>+</button>
|
|
210
|
+
<button onClick={() => emit("dec")}>-</button>
|
|
211
|
+
<button onClick={() => emit("asyncInc")}>
|
|
202
212
|
{state.loading ? "Loading..." : "Async +"}
|
|
203
213
|
</button>
|
|
204
214
|
</div>
|
|
@@ -271,7 +281,7 @@ async function run() {
|
|
|
271
281
|
await runtime.emit("login")
|
|
272
282
|
await runtime.emit("logout")
|
|
273
283
|
|
|
274
|
-
console.log(runtime.
|
|
284
|
+
console.log(runtime.state)
|
|
275
285
|
}
|
|
276
286
|
|
|
277
287
|
run()
|
|
@@ -306,7 +316,7 @@ const logic = createLogic({
|
|
|
306
316
|
const runtime = logic.create()
|
|
307
317
|
|
|
308
318
|
await runtime.emit("set", 4)
|
|
309
|
-
expect(runtime.
|
|
319
|
+
expect(runtime.state.squared).toBe(16)
|
|
310
320
|
```
|
|
311
321
|
|
|
312
322
|
---
|
|
@@ -17,7 +17,7 @@ export declare function createLogic<S extends object, C extends ComputedDef<S>,
|
|
|
17
17
|
}) => (...args: any[]) => any>>(config: {
|
|
18
18
|
name?: string;
|
|
19
19
|
state: S;
|
|
20
|
-
computed
|
|
20
|
+
computed?: C;
|
|
21
21
|
intents?: (bus: {
|
|
22
22
|
on: LogicRuntime<S, C, any>["onIntent"];
|
|
23
23
|
effect: (type: string, eff: EffectDef) => void;
|