Skip to content

Commit

Permalink
feat: added trigger option to effect
Browse files Browse the repository at this point in the history
  • Loading branch information
tmgulland committed May 31, 2024
1 parent acf1b5c commit 7f72977
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ type SourceValue<S> =

export const effect = <S extends (Signal<any> | Events<any>)[]>(
sources: [...S],
callback: (values: { [K in keyof S]: SourceValue<S[K]> }) => void
callback: (values: { [K in keyof S]: SourceValue<S[K]> }) => void,
{ trigger }: { trigger?: boolean } = {}
): Effect => {
const { use, dispose } = system()

Expand Down Expand Up @@ -42,6 +43,10 @@ export const effect = <S extends (Signal<any> | Events<any>)[]>(
}
})

if (trigger) {
updateValues()
}

return {
use,
dispose
Expand Down
10 changes: 10 additions & 0 deletions test/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ describe('effect', () => {
expect(mockSub).toHaveBeenCalledTimes(2)
expect(mockDispose).toHaveBeenCalledTimes(1)
})
it('triggers as expected', () => {
const exampleEvents = createEvents<{ something: number; else: string[] }>()
const exampleSignal = signal(() => 10)

const mockSub = mock(() => ({}))

effect([exampleEvents, exampleSignal], mockSub, { trigger: true })

expect(mockSub).toHaveBeenCalledTimes(1)
})
})

0 comments on commit 7f72977

Please sign in to comment.