Skip to content

Instantly share code, notes, and snippets.

@zerobias
Last active July 18, 2019 22:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerobias/ac19a37f7a147223068d5377e3e3e7fc to your computer and use it in GitHub Desktop.
Save zerobias/ac19a37f7a147223068d5377e3e3e7fc to your computer and use it in GitHub Desktop.
effector proposal: tasks

Tasks

try it

Current effect behavior

const fx = createEffect({
  handler(params) {
    console.log(params)
  },
})

fx('arg')
// => arg

Proposal

const foo = createStore(1)

const fx = createEffect({
  store: {foo},
  handler(params, {store}) {
    console.log(params, store)
  },
})

fx('arg')
// => arg, {foo: 1}
const users = createStore([{
  nick: 'jack',
  email: 'jack@example.com',
}])
const userID = createStore(0)

const fx = createEffect({
  store: {id: userID, users},
  handler(message, {store: {id, users}}) {
    const user = users[id]
    console.log('[', user.nick, ']: ', message)
  },
})

await fx('hi all!')

// => [jack]: hi all!

/* lets use real request */

fx.use(async (message, {store: {id, users}}) => {
  const user = users[id]
  const req = await fetch('/our/backend', {
    method: 'POST',
    body: JSON.stringify({user, message}),
  })
  await req.json()
})

await fx('hi all!')

// ~> request to backend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment