algebraic-effects
Version:
Algebraic effects in react using generators
52 lines (37 loc) • 1.87 kB
Markdown
Ya need to kno da lingo to be talkin wid us yo. So letz get yaz up ta speed.
A program is a generator that calls an effects. This is where all your core logic exists free from the implementation details. All effects by design have a consistent api so dont worry about any of the outside world.
```js
// program :: Program<IOEffect, ConsoleEffect> String ()
const program = function *(greetText) {
const name = yield IOEffect.getInput('What is your name?');
yield ConsoleEffect.log('>> Name', name);
yield IOEffect.showMessage(`Hello, ${name}! ${greetText}`);
};
```
An operation is basically something to do. An operation is technically just an instruction with no behavior implementation. So you can use an operation in your program without worrying about anything other than the operation signature.
In the above example `IOEffect.getInput`, `ConsoleEffect.log` and `IOEffect.showMessage` are operations being run.
An effect is a group of operations.
In the above example `IOEffect` and `ConsoleEffect` are effects. This package comes with a few built in effects like State, Exception, etc.
A handler is a set of curried functions that decides how the program flows when the given operation is called
```js
const withIO = IOEffect.handler({
getInput: ({ resume }) => label => showModal({ label, onSubmit: resume }),
showMessage: ({ resume }) => /* whatever */,
});
```
`.handler` method call returns a function. You can use that function to call your program. This function is called the runner.
I like the `with*` prefix for my runners.
It accepts a program and its arguments.
```js
withIO(program, 'Hello world'); // Program, ...arguments
```
Alternatively you can call the .run method on it.
```js
withIO.run(program, 'Hello world'); // Program, ...arguments
```