Skip to content

Commit

Permalink
fix(doc): Fix best practice example
Browse files Browse the repository at this point in the history
* chore: update dependencies

* doc: improve long lines

* fix(doc): fix best practice example
  • Loading branch information
unional committed Mar 15, 2018
1 parent d073eb4 commit 2af1c61
Show file tree
Hide file tree
Showing 5 changed files with 1,501 additions and 265 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,25 @@ One of the benefits of using events is decoupling.
Here is one way to organize your code:

```ts
// actions.ts
import { createEvent, Emitter } from 'fsa-emitter'
// app.ts
import { doWork } from './doWork'

export const count = createEvent<number>('count')
const emitter = new Emitter()

// app.ts
export const emitter = new Emitter()
doWork({ emitter })

// logic.ts
import { emitter } from './app'
import { count } from './actions'
// doWork.ts
import { createEvent, Emitter } from 'fsa-emitter'

emitter.emit(count(1, undefined))
export const count = createEvent<number>('count')

export function doWork({ emitter }) {
emitter.emit(count(1, undefined))
}

// in UI
import { emitter } from './app'
import { count } from './actions'
import { count } from './doWork'

emitter.on(count, payload => {
console.log('payload is typed and is a number: ', payload)
})
Expand All @@ -62,8 +64,7 @@ npm install fsa-emitter

`Emitter` uses [`FluxStandardAction`](https://github.com/acdlite/flux-standard-action) as the standard event format.

Another key differences between `Emitter` and NodeJS `EventEmitter`,
is that `Emitter` will capture any error thrown in listener and send it to `console.error()`.
Another key difference between `Emitter` and NodeJS `EventEmitter` is that `Emitter` will capture any error thrown in listener and send it to `console.error()`.
This avoid any listener code throws error and break the event emitting logic.

To create event, use one of the helper methods below:
Expand All @@ -84,12 +85,16 @@ const createMyModuleEvent = createScopedCreateEvent('myModule')
// `scopedCount` will emit FSA with `type: 'myModule/count'`
const scopedCount = createMyModuleEvent<number>('count')

const add = createAction</* input */ { a: number, b: number }, /* payload */ number>('add', ({ a, b }) => emit => emit(a + b))
const add = createAction<
/* input */ { a: number, b: number },
/* payload */ number>('add', ({ a, b }) => emit => emit(a + b))

// create scoped action
const createMyModuleAction = createScopedCreateEventAction('myModule')
// `scopedCountAction` will emit FSA with `type: 'myModule/add'`
const scopedAdd = createMyModuleAction</* input */ { a: number, b: number }, /* payload */ number>('add', ({ a, b }) => emit => emit(a + b))
const scopedAdd = createMyModuleAction<
/* input */ { a: number, b: number },
/* payload */ number>('add', ({ a, b }) => emit => emit(a + b))

const emitter = new Emitter()

Expand Down
Loading

0 comments on commit 2af1c61

Please sign in to comment.