Allow "when (...)" clause on "on" endpoints

This commit is contained in:
Tony Garnock-Jones 2024-04-17 13:26:06 +02:00
parent f4f2ad0783
commit ebe7700cee
3 changed files with 69 additions and 10 deletions

View File

@ -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.assertDataflow(() => (${walk(s.test)})
? ({ target: currentSyndicateTarget, assertion: ${assertion} })
: ({ target: void 0, assertion: void 0 }));`);
}
} else {
return wrap(
t`__SYNDICATE__.Turn.active.replace(currentSyndicateTarget, void 0, ${assertion});`
);
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);`);
}
}
});

View File

@ -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))))));
});

View File

@ -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
}));`));
});