Skip to content

Commit a302382

Browse files
committed
Allow cond to be sent directly
1 parent 79bf15b commit a302382

File tree

3 files changed

+124
-9
lines changed

3 files changed

+124
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,5 @@ dist
114114
.yarn/build-state.yml
115115
.yarn/install-state.gz
116116
.pnp.*
117+
118+
.DS_Store

src/index.test.ts

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ describe("Form Field Machine with always()", () => {
136136
yield on("BLUR", validating);
137137
}
138138
function* validating() {
139-
yield always(cond(isValid, valid));
139+
yield cond(isValid, valid);
140+
// yield cond(true, invalid);
140141
yield always(invalid);
141142
}
142143
function* invalid() {
@@ -247,9 +248,9 @@ describe("Hierarchical Traffic Lights Machine", () => {
247248
expect(machine.changeCount).toEqual(1);
248249

249250
machine.next("TIMER");
250-
// expect(machine.current).toEqual("red");
251-
// expect(machine.current).toEqual(["red", "walk"]);
252251
expect(machine.current).toEqual({ "red": "walk" });
252+
// expect(machine.current).toEqual([["red", "walk"]]); // Like a Map key
253+
// expect(machine.currentMap).toEqual(new Map([["red", "walk"]]));
253254
expect(machine.changeCount).toEqual(3);
254255

255256
machine.next("TIMER");
@@ -265,3 +266,116 @@ describe("Hierarchical Traffic Lights Machine", () => {
265266
expect(machine.changeCount).toEqual(7);
266267
});
267268
});
269+
270+
describe("Switch", () => {
271+
function* Switch() {
272+
function* OFF() {
273+
yield on("FLICK", ON);
274+
}
275+
function* ON() {
276+
yield on("FLICK", OFF);
277+
}
278+
279+
return OFF;
280+
}
281+
282+
test("sending events", () => {
283+
const machine = start(Switch);
284+
expect(machine).toBeDefined();
285+
expect(machine.current).toEqual("OFF");
286+
287+
machine.next("FLICK");
288+
expect(machine.current).toEqual("ON");
289+
expect(machine.changeCount).toEqual(1);
290+
291+
machine.next("FLICK");
292+
expect(machine.current).toEqual("OFF");
293+
expect(machine.changeCount).toEqual(2);
294+
});
295+
});
296+
297+
/*describe("Counter", () => {
298+
function* Counter() {
299+
function* initial() {
300+
yield entry(function counter() { return 0 });
301+
}
302+
function* positive() {
303+
yield entry(function counter(n) {
304+
console.log({ n });
305+
return 1
306+
});
307+
// yield on("INCREMENT", action(function counter(n) { return n + 1 }));
308+
}
309+
310+
// yield association(function *(events) {
311+
// let n = 0;
312+
// yield n;
313+
314+
// for (const event of events()) {
315+
// if (event.type === "INCREMENT") {
316+
// n += 1;
317+
// yield n;
318+
// }
319+
// }
320+
// })
321+
322+
yield on("RESET", compound(initial));
323+
yield on("INCREMENT", compound(positive));
324+
325+
// const counter = yield reducer("counter", 0, n => n + 1);
326+
// const counter = yield reducer(0, {
327+
// increment: n => n + 1,
328+
// reset: () => 0
329+
// });
330+
331+
// yield on("INCREMENT", counter.increment);
332+
// yield on("RESET", counter.reset);
333+
334+
//////////
335+
336+
const Counter = yield atom(0);
337+
yield output("counter", Counter);
338+
// yield output("counter", Counter(n => n));
339+
// yield output("counter", Counter, n => n);
340+
341+
// const Counter = yield atom(function counter() {
342+
// return 0;
343+
// });
344+
345+
// const { increment, reset } = (yield atom(function counter() {
346+
// return 0;
347+
// }))({ increment: (n) => n + 1, reset: () => 0 });
348+
349+
350+
yield on("INCREMENT", Counter(n => n + 1));
351+
yield on("RESET", Counter(0));
352+
353+
// yield on("INCREMENT", counter(n => n + 1));
354+
// yield on("RESET", counter(() => 0));
355+
356+
return initial;
357+
}
358+
359+
test("sending events", () => {
360+
const machine = start(Counter);
361+
expect(machine.current).toEqual("initial");
362+
expect(machine.results).resolves.toEqual({ counter: 0 });
363+
364+
machine.next("INCREMENT");
365+
expect(machine.current).toEqual("positive");
366+
expect(machine.changeCount).toEqual(1);
367+
expect(machine.results).resolves.toEqual({ counter: 1 });
368+
369+
machine.next("INCREMENT");
370+
expect(machine.current).toEqual("positive");
371+
expect(machine.changeCount).toEqual(1);
372+
expect(machine.results).resolves.toEqual({ counter: 1 });
373+
374+
machine.next("RESET");
375+
expect(machine.current).toEqual("initial");
376+
expect(machine.changeCount).toEqual(2);
377+
expect(machine.results).resolves.toEqual({ counter: 0 });
378+
379+
380+
});
381+
});*/

src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface Always {
3838
target: Target;
3939
}
4040

41-
export type Yielded = On | Always | EntryAction | ExitAction | Call<any>;
41+
export type Yielded = On | Always | Cond | EntryAction | ExitAction | Call<any>;
4242

4343
export function call<Arguments extends Array<any>>(
4444
f: (...args: Arguments) => void,
@@ -119,6 +119,8 @@ class Handlers {
119119
this.eventsMap.set(value.on, value.target);
120120
} else if (value.type === "always") {
121121
this.alwaysArray.push(value.target);
122+
} else if (value.type === "cond") {
123+
this.alwaysArray.push(value);
122124
}
123125
}
124126

@@ -175,13 +177,9 @@ class InternalInstance {
175177
if (this.child === null) {
176178
return this.definition.name;
177179
} else {
180+
// return [[this.definition.name], this.child.current];
178181
return { [this.definition.name]: this.child.current };
179182
}
180-
// if (this.child === null) {
181-
// return null;
182-
// }
183-
//
184-
// return this.child.definition.name;
185183
}
186184

187185
private *generateActions(): Generator<EntryAction, void, undefined> {
@@ -214,6 +212,7 @@ class InternalInstance {
214212
}
215213

216214
return build().then(objects => Object.assign({}, ...objects));
215+
// return build().then(pairs => Object.fromEntries(pairs as any));
217216
}
218217

219218
consume(stateGenerator: (() => StateDefinition) | (() => Generator<Yielded, StateDefinition, never>)) {

0 commit comments

Comments
 (0)