Skip to content

Commit

Permalink
Add watcher.fail event
Browse files Browse the repository at this point in the history
  • Loading branch information
goodmind committed Jun 20, 2019
1 parent 323f40e commit ba67d4c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/effector/event/__tests__/fail.test.js
@@ -0,0 +1,30 @@
// @flow

import {createEvent, is} from 'effector'
import {argumentHistory} from 'effector/fixtures'

test('watcher.fail is event', () => {
const foo = createEvent('foo')
const sub = foo.watch(() => {})
expect(is.event(sub.fail)).toBe(true)
})

it('triggers after failed .watch', () => {
const fn = jest.fn()
const foo = createEvent()
const sub = foo.watch(() => {
throw new Error('Unknown error')
})
sub.fail.watch(e => fn(e))

expect(fn).not.toBeCalled()
foo()
expect(fn).toBeCalledTimes(1)
expect(argumentHistory(fn)).toMatchInlineSnapshot(`
Array [
Object {
"error": [Error: Unknown error],
},
]
`)
})
14 changes: 11 additions & 3 deletions src/effector/event/eventFabric.js
Expand Up @@ -199,11 +199,15 @@ function filterEvent(
}

function watchEvent<Payload>(
event: Unit,
event: Event<Payload>,
watcher: (payload: Payload, type: string) => any,
): Subscription {
return createLink(event, {
scope: {trigger: event, handler: watcher},
const fail = eventFabric({
name: 'fail ' + event.shortName,
parent: event.domainName,
})
const subscription = createLink(event, {
scope: {trigger: event, handler: watcher, fail},
//prettier-ignore
node: [
noop,
Expand All @@ -212,6 +216,7 @@ function watchEvent<Payload>(
payload,
getDisplayName(trigger),
),
fail: (error: mixed, {fail}) => fail({error})
}),
],
meta: {
Expand All @@ -222,4 +227,7 @@ function watchEvent<Payload>(
},
},
})
//$todo
subscription.fail = fail
return subscription
}

1 comment on commit ba67d4c

@vercel
Copy link

@vercel vercel bot commented on ba67d4c Jun 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.