diff --git a/packages/lens/src/match.test.ts b/packages/lens/src/match.test.ts index f0ec49b3..70d8f17c 100644 --- a/packages/lens/src/match.test.ts +++ b/packages/lens/src/match.test.ts @@ -87,4 +87,21 @@ test('default should checks in the end', () => { ;`👍` //? }) +test('reactive change', () => { + const ctx = createTestCtx() + + const boolAtom = atom(true) + const compAtom = match(true) + .is(boolAtom, () => 'a') + .default(() => 'b') + + const track = ctx.subscribeTrack(compAtom) + + assert.equal(track.inputs(), ['a']) + + boolAtom(ctx, false) + boolAtom(ctx, true) + assert.equal(track.inputs(), ['a', 'b', 'a']) +}) + test.run() diff --git a/packages/lens/src/match.ts b/packages/lens/src/match.ts index 61fff6a8..de525c7a 100644 --- a/packages/lens/src/match.ts +++ b/packages/lens/src/match.ts @@ -56,7 +56,7 @@ export function match( name = __count('match'), ): Match { type Case = { - clause: (ctx: Ctx, expression: T) => boolean + clause: (ctx: CtxSpy, expression: T) => boolean statement: {} | Atom | ((ctx: Ctx, expression: T) => any) } const cases: Array = [] @@ -70,11 +70,12 @@ export function match( : typeof expression === 'function' ? (expression as Fn)(ctxSpy) : expression - const ctx = merge(ctxSpy, { spy: undefined }) + + const list = [...cases, _truthy, _falsy, _default].filter(Boolean) for (const { clause, statement } of list) { - if (clause(ctx, value)) { + if (clause(ctxSpy, value)) { return isAtom(statement) ? ctxSpy.spy(statement) : typeof statement === 'function' @@ -89,7 +90,7 @@ export function match( is(clause: any, statement: any) { cases.push({ clause: isAtom(clause) - ? (ctx, value) => Object.is(value, ctx.get(clause)) + ? (ctx, value) => Object.is(value, ctx.spy(clause)) : typeof clause === 'function' ? clause : (ctx, value) => Object.is(value, clause),