btford / react-palm

Work in progress, docs forthcoming i promise
MIT License
60 stars 7 forks source link

Refactor tasks #15

Closed btford closed 7 years ago

btford commented 7 years ago

In order to make combinators work uniformly (especially Task.all), we moved the success and error handlers off task instances.

BREAKING CHANGE:

Because success and error are not available, you now use succeedTaskInTest or errorTaskInTest:

before:

const successAction = task.success(value);
const errorAction = task.error(reason);

after:

const successAction = succeedTaskInTest(task, value);
const errorAction = errorTaskInTest(task, reason);

BREAKING CHANGE:

Previously, you created a task with createTaskType. The new API is taskCreator, which returns a function that takes a payload.

Before, when you created a task, you set success and error as properties on it. Now, you use map and bimap to do the same thing:

before:

// defining the "task creator"
const MY_TASK = ({payload, success, error}) => ({type: MY_TASK, payload, success, error});

// registering the handler
createTaskType(MY_TASK, task => doSideEffect(task.payload).then(task.success, task.error);

// creating a specific instance of the task type
const taskOne = MY_TASK({payload: 'hi there', success: CATS, error: DOGS});

after:

// defining the "task creator"
const MY_TASK = taskCreator((payload, success, error) =>
                  doSideEffect(payload).then(success, error), 'MY_TASK');

// creating a specific instance of the task type with a given payload
const taskOne = MY_TASK('hi there');

// Now you have the opportunity to use combinators before specifying the
// error/success handlers.
const taskTwo = taskOne.map(result => result + 1);

// When you're done composing and ready to hand the task off:
const taskWithHanders = taskTwo.bimap(CATS, DOGS);

Note that you use the same mechanism to add handlers (action creators) as you do to combine behavior after a task completes: .map and .bimap.