Create truly pure functional runtime envs
- Separate pure functions from impure functions in a clear, composable manner, using ES2015.
- Create easily testable logics, even impure ones
- Easy inter-op with "regular" JS
npm i -S feffect
// index.jsconsteffect // the most basic atom for `feffect`impurerun} = ;const world = ;// all this function does is to create an effect-intention.// nothing actually gets executed here, this is truly pure.const request = ;const log = ;const main =;// calling an impure function simply returns another effect, thoughconst mainEffect = ;// the `run` function converts an effect (intention) into a Promise (action),// according to the world's interpretation of the effect's type;// world.jsconst request = ;// A world object must handle every type of effect the program uses.// the handler receives 3 parameters:// - the object passed as the effect's second parameter// - a resolver function that marks the effect as successful with an optional value// - a rejecter function that marks the effect as failed with an optional value// You might notice that we don't explicitly handle any effect type which starts with// the `impure:` prefix. These are reserved for internal usage.moduleexports =;
feffect returns a function that, when called, creates an entirely new functional environment,
with the API below.
This is the most basic part of
effect has a type, and an optional parameters object.
Effects are immutable, with no way to gain direct access to their properties (not even a
get access), except via
Every effect type should be handled explicitly as part of a
world object (again, below).
env.ensure(effect, [type, [params]])
When provided a single parameter, this function simply ensures it is an effect belonging to this
env. When given a
type, it also checks for type equality. When also given
params, it checks that for every key in the
params object, its value equals (
===) the effect's.
const eft =// true// true// true// false// false// false
This function accepts a generator function that can
effect objects, and get back their resolved values.
It returns a function, that when called, does nothing but return an
effect object of type
This function converts an
effect (which is a symbol for an intent) into a
Promise (which is a symbol for an action), via the
world parameter's interpretation of the
effect type not be handled by the
world, this function
This function returns an effect of the
which has a default interpretation of interpreting all effects in its
effects parameter, according to the same world that interprets itself, and resolves with an array of the return values in the same order as their respective effects. Basically,
concurrent is to
effect objects, as
Promise.all is to
world object passed to the
run function is not magic, but a plain JS object. For every type of
env uses, you must include it as a property of the world, with a value that looks like so:
(effectParams, resolve, reject) => effectParams.shouldWork ? resolve(10) : reject(new Error('Meow'))
resolve to mark (with a possible value) a successful side-effect, and
reject to mark a failed one - just like with a
Usage in testing
It is highly advised to test your entire program's logic with different
world objects to simulate as many possible scenarios as you feel appropriate, while testing your actual
world object separately, using