Allow "when (...)" clause on "on" endpoints
This commit is contained in:
parent
f4f2ad0783
commit
ebe7700cee
|
@ -296,14 +296,19 @@ ${joinItems(sa.captureBinders.map(binderTypeGuard(t)), '\n')}
|
|||
})`;
|
||||
|
||||
if (s.isDynamic) {
|
||||
return wrap(t`__SYNDICATE__.Turn.active.assertDataflow(() => ({
|
||||
target: currentSyndicateTarget,
|
||||
assertion: ${assertion},
|
||||
}));`);
|
||||
if (s.test === void 0) {
|
||||
return wrap(t`__SYNDICATE__.Turn.active.assertDataflow(() => ({ target: currentSyndicateTarget, assertion: ${assertion} }));`);
|
||||
} else {
|
||||
return wrap(
|
||||
t`__SYNDICATE__.Turn.active.replace(currentSyndicateTarget, void 0, ${assertion});`
|
||||
);
|
||||
return wrap(t`__SYNDICATE__.Turn.active.assertDataflow(() => (${walk(s.test)})
|
||||
? ({ target: currentSyndicateTarget, assertion: ${assertion} })
|
||||
: ({ target: void 0, assertion: void 0 }));`);
|
||||
}
|
||||
} else {
|
||||
if (s.test === void 0) {
|
||||
return wrap(t`__SYNDICATE__.Turn.active.replace(currentSyndicateTarget, void 0, ${assertion});`);
|
||||
} else {
|
||||
return wrap(t`__SYNDICATE__.Turn.active.replace(currentSyndicateTarget, void 0, (${walk(s.test)}) ? ${assertion} : void 0);`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -75,6 +75,7 @@ export interface PseudoEventEndpointStatement extends GenericEventEndpointStatem
|
|||
export interface AssertionEventEndpointStatement extends GenericEventEndpointStatement {
|
||||
triggerType: 'asserted' | 'retracted' | 'message';
|
||||
pattern: ValuePattern;
|
||||
test?: Expr,
|
||||
}
|
||||
|
||||
export type EventHandlerEndpointStatement =
|
||||
|
@ -325,7 +326,11 @@ export class SyndicateParser {
|
|||
atomString('message'))),
|
||||
option(map(kw('snapshot'), _ => o.isDynamic = false)),
|
||||
bind(o as AssertionEventEndpointStatement, 'pattern',
|
||||
this.valuePattern(1, atom('=>'))),
|
||||
this.valuePattern(1, atom('=>'),
|
||||
seq(atom('when'), group('(', discard)))),
|
||||
option(seq(atom('when'), group(
|
||||
'(', bind(o as AssertionEventEndpointStatement, 'test',
|
||||
this.expr())))),
|
||||
this.mandatoryIfNotTerminal(
|
||||
o, seq(atom('=>'), this.statement(o.body))))));
|
||||
});
|
||||
|
|
|
@ -187,7 +187,7 @@ __SYNDICATE__.Turn.active.facet(() => {
|
|||
}
|
||||
}
|
||||
}),
|
||||
}),
|
||||
})
|
||||
}));
|
||||
});`));
|
||||
|
||||
|
@ -206,8 +206,57 @@ __SYNDICATE__.Turn.active.facet(() => {
|
|||
}
|
||||
}
|
||||
}),
|
||||
}),
|
||||
})
|
||||
}));
|
||||
});`));
|
||||
|
||||
});
|
||||
|
||||
describe('on', () => {
|
||||
it('message with guard', () => expectCodeEqual(`
|
||||
on message S.Focus(entity) when (isLast.value) => {
|
||||
text.node.focus();
|
||||
}`, `__SYNDICATE__.Turn.active.assertDataflow(() => (isLast.value) ?
|
||||
({
|
||||
target: currentSyndicateTarget,
|
||||
assertion: __SYNDICATE__.Observe({
|
||||
pattern: __SYNDICATE__.QuasiValue.finish((__SYNDICATE__.QuasiValue.ctor(S.Focus, (__SYNDICATE__.QuasiValue.lit(__SYNDICATE__.fromJS(entity)))))),
|
||||
observer: __SYNDICATE__.Turn.ref({
|
||||
message: (__vs) => {
|
||||
if (Array.isArray(__vs)) {
|
||||
|
||||
|
||||
text.node.focus();
|
||||
|
||||
}
|
||||
}
|
||||
}),
|
||||
})
|
||||
}) :
|
||||
({
|
||||
target: void 0,
|
||||
assertion: void 0
|
||||
}));`));
|
||||
|
||||
it('asserted with guard', () => expectCodeEqual(`on asserted P when (someTest) => x;`, `
|
||||
__SYNDICATE__.Turn.active.assertDataflow(() => (someTest) ?
|
||||
({
|
||||
target: currentSyndicateTarget,
|
||||
assertion: __SYNDICATE__.Observe({
|
||||
pattern: __SYNDICATE__.QuasiValue.finish((__SYNDICATE__.QuasiValue.lit(__SYNDICATE__.fromJS(P)))),
|
||||
observer: __SYNDICATE__.Turn.ref({
|
||||
assert: (__vs, __handle) => {
|
||||
if (Array.isArray(__vs)) {
|
||||
|
||||
x;
|
||||
}
|
||||
}
|
||||
}),
|
||||
})
|
||||
}) :
|
||||
({
|
||||
target: void 0,
|
||||
assertion: void 0
|
||||
}));`));
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue