Frappe is a library for defining UI components based on time and events.
npm i @framp/frappe react react-dom
The main source of inspiration are Arrowised Functional Reactive Programming (aFRP) libraries like Netwire and Yampa.
JavaScript is a vastly different language from Haskell - which translates to different developers expectations. When facing a choice between pragmatism / familiarity and purity, Frappe takes the pragmatic approach.
MIT
It's a Straw that returns always the value.
It's a Straw that returns always the time.
It creates a Straw that will keep state, given a function func and an initial state acc.
Similarly to a reducer, func accepts acc, val, time, event, emit and returns a single value that will be returned in the current execution and used as acc in the next execution of the Straw.
If you want your internal state to be different from what it's being returned, see accumState.
the reducer function
the initial state
a Straw that uses func to store state
It creates a Straw that will keep state, given a function func and an initial state acc.
Similarly to a reducer, func accepts acc, val, time, event, emit and returns a tuple [newAcc, output].
newAcc will be used as the acc in the next execution of the Straw, while output will be returned in the current execution.
the reducer function func
the initial state
a Straw that uses func to store state
It accepts an event targetEvent and returns a Straw that will return true only after that event happens (inclusive of when targetEvent happens).
an event to wait for
a Straw that will return true only after that event happens
It accepts a targetTime time and returns a Straw that will return true only after targetTime time (inclusive of targetTime) startTimeing from when the Straw has been executed first.
a time to wait for
a Straw that will return true until targetTime
It creates a Straw from a number of Straws which return a boolean to indicate if all the Straws outputs are truthy.
The equivalent of the && operator for Straws.
the Straws to evaluate
a Straw that will return the desired boolean
It accepts a targetTime time and returns a Straw that will return true when time is targetTime.
A optional margin parameter can be used to relax the comparison:
the resulting Straw will return true as long as time is within margin to targetTime.
a targetTime time
a margin to use to compare for equality
a Straw that will return true when time is targetTime
It accepts an event targetEvent and returns a Straw that will return true until that event happens (inclusive of when targetEvent happens).
an event to wait for
a Straw that will return true until that event happens
It accepts a targetTime time and returns a Straw that will return true until targetTime time (inclusive of targetTime) startTimeing from when the Straw has been executed first.
a time to wait for
a Straw that will return true until targetTime
It accepts two events eventStart and eventEnd and returns a Straw that will return true after eventStart and before eventEnd (inclusive of when the events happen).
an event to mark the beginning of the interval
an event to mark the end of the interval
a Straw that will return true after eventStart and before eventEnd
It accepts two times startTime and endTime and returns a Straw that will return true after startTime and before endTime (inclusive of when the events happen).
a time to mark the beginning of the interval
a time to mark the endTime of the interval
a Straw that will return true after startTime and before endTime
It composes a number of Straws together right to left.
The first val is fed into the rightward Straw, the result of which get fed into the next Straw.
The same action is repeated until all the Straws are processed.
The same time, event, emit are passed to all the Straws.
the Straws that will be composed together
a Straw that will execute all the straws
It composes a number of Straws together left to right.
The first val is fed into the leftward Straw, the result of which get fed into the next Straw.
The same action is repeated until all the Straws are processed.
The same time, event, emit are passed to all the Straws.
the Straws that will be composed together
a Straw that will execute all the straws
It composes a number of Straws together right to left.
The first val is fed into the rightward Straw, the result of which get fed into the next Straw.
The same action is repeated until all the Straws are processed.
The same time, event, emit are passed to all the Straws.
the Straws that will be composed together
a Straw that will execute all the straws
It accepts a value val and return a Straw that will always return that val.
a value to use
a Straw that will always return val
It accepts a value val and return val, if it's a Straw, or the Straw constant(val).
a value to use
a Straw, either val or constant(val)
It creates a Straw that will maintain state, adding and removing elements, by reading data from provided actions Straws.
Items will be inserted and removed into an Array.
an Object providing an add and remove Straws
a Straw that will retunr the current state
It creates a Straw that will maintain state, adding and removing elements, by reading data from provided actions Straws.
Items will be inserted and removed into an Object.
an Object providing an add and remove Straws
a Straw that will return the current state
It creates a Straw that will maintain state, adding and removing elements, by reading data from provided actions Straws.
It uses a provided driver to manage state.
If you're looking for a battery-included solution check out dynamicArray and dynamicMap.
an Object providing the new, remove, add functions
a Straw that will retunr the current state
It accepts an event targetEvent and returns a Straw that will emit it.
an event to emit
a Straw that will emit targetEvent
It accepts N straws and returns a Straw that will accept a single value val.
The resulting Straw will run each of the Straws using val.
The same time, event, emit are passed to all the Straws.
the Straws that will be used
a Straw that accepts val and that will execute all the straws
It accepts a function func and return a Straw that will execute func .
func will accept 4 parameters val, time, event, emit and returns just the result (unlike of).
If you want to change how the Straw behaves in the next executions you should use of.
a function to make into a Straw
a Straw that will call func at every run
It creates a Straw that holds all the truthy values returned by straw for all the executions.
the straw to run through
a holding values Straw
It creates a Straw that holds the first value returned by straw for all the executions.
the straw to run through
a holding values Straw
It creates a Straw that holds a value across executions of a Straw when a condition predicate matches.
condition accepts the current value acc being hold (if present), val, time, event, emit.
Based on the output of condition the current value acc will be kept or it will be swapped with val.
a function to check if the current execution
the straw to run through
a holding values Straw
It accepts a value val and return a boolean indicating whether val is a Straw or not.
a value to check
a boolean to indicate if val is a Straw
It accepts an event (or array of events) and a Straw returning a React element and return a Straw which returns the React element extended with the needed event listeners, setup using the type property of the event.
The events will be plugged via ReactRunner into the application and will be available to all the Straws.
an event (or array of event) to listen for
a Straw returning a React element to extend with the listeners from event
a Straw returning the React element from straw with the event listeners from event
It creates a Straw from a single Straws which return a boolean to indicate if the Straw output is falsy.
The equivalent of the ! operator for Straws.
the Straw to evaluate
a Straw that will return the desired boolean
It accepts a function func and return a Straw.
func will accept 4 parameters val, time, event, emit and returns a tuple [nextFunc, result].
nextFunc can be used to change the behaviour of func at the next execution of the Straw, result will be the output of the Straw at the current run.
Given a function behaves naturally like a Straw, the only thing of is doing is adding a __type: 'Straw' to the function for identification purposes (eg: isStraw).
a function to make into a Straw
a Straw made from func
It accepts an event and returns a Straw that will return that event when it happens.
An optional transformer function can be passed to transform the event before consumption.
function to be applied to event before return
a Straw that will look for event
It creates a Straw that invokes the given straw only once.
the straw to run through
a Straw that will return only the first value
It creates a Straw from a number of Straws which return a boolean to indicate if at least one of the Straws outputs is truthy.
The equivalent of the || operator for Straws.
the Straws to evaluate
a Straw that will return the desired boolean
It accepts a period and returns a Straw that will alternate returning true or false for period.
the length in time to use for the interval
a Straw that will return true or false for period
It accepts a promise returning function func and return a Straw that will execute func and fire a promise-resolve event on success or a promise-error event on failure.
The emitted event will use the returned Straw as a ref.
promise returning function
Straw that will call func when run
It creates a Straw that returns a reduce of all the given straws starting from an initial value acc.
The reducer func accepts acc, the current straw, val, time, event, emit and it's responsible for running each straw and return a new (or the same) straw for the next execution.
The and and or functions are implemented with reduce.
a Straw that will reduce all the straws
It creates a Straw from a condition and an effect Straws.
effect will be executed by default.
When condition returns a truthy value, the current effect Straw will be replaced with the original effect
the Straw used to check if the effect needs to be restarted
the Straw to execute and optionally restart
a Straw that will execute effect and restart it on occasion
It runs a Straw, simulating a set of inputs, times, events, emit functions and collect the results in an array.
Great for testing straws.
inputs will be used as a source of truth to count how many times the Straw will be run.
a Straw to run
the inputs to run the straw on
the times to use for every input being sent
the events to use for every input being sent
the emit functions to use for every input being sent
an array of values resulting from all the executions of straw
It accepts N straws and returns a Straw that accepts an array vals of N values.
The resulting Straw will run each of the Straws using a single value from vals, taken by index.
The same time, event, emit are passed to all the Straws.
the Straws that will be used
a Straw that accepts vals and that will execute all the straws
It creates a Straw that invokes the given straw at most n times.
the number of executions that will invoke straw
the straw to run through
a Straw that will return values for at most n executions
It creates a Straw from a number of Straws, paired up in conditions, effects.
All the Straws with a even index will be assumed to be conditions, all the Straws with an odd index will be assumed to be the Straws to be executed.
when will execute all conditions in order; when the first condition with a truthy value is found the respective effect will be executed and returned.
Unevaluated effects won't be executed.
effects Straws with the property __update set to a truthy value, will be executed in any case.
the Straws paired up in conditions and effects
a Straw that will return the first effect found with a truthy condition
An array-like driver for dynamicStructure that store data in a JavaScript Array.
Function used to add an element to the Array.
If id is not provided the element will be added at the end of the Array.
the index where the new element will be inserted
a value to insert
an Array with val inserted at the id index
Function used to return an initial value
an empty Array
Function used to remove an element from the Array.
an index to delete in the Array
an Array with the element at the id index removed
A map-like driver for dynamicStructure that store data in a JavaScript Object.
Function used to add an element to the Object.
the key where the new element will be inserted
a value to insert in the Object
an Object with val inserted at the id key
Function used to return an initial value
an empty Object
Function used to remove an element from the Object.
a key to delete in the Object
an Object with the element at the id key removed
It's a
Strawthat returns always the event.a tuple containing the
eventStrawand the event passed.