ferp-js
Search…
The Boilerplate

Application Anatomy

Every ferp application uses the app function, and must have init, which declares your applications initial state, and any initial side effect. In the helloWorld example, there is no state, null, and a custom side effect that adds text to the html page or console, writeEffect.

init: [state, effect]

1
import { app, effects } from 'ferp';
2
3
const INITIAL_STATE = { counter: 1 };
4
5
app({ init: [INITIAL_STATE, effects.none()] });
Copied!
The init key holds a special structure that is used everywhere in ferp. The state-effect tuple tells ferp what the next state should look like, and if there are any effects that need to be ran. The state can take on whatever shape you need it to, but it is pretty common to have large nested objects. This, however, will depend on what you need your application to do, and the data sources it uses. The second part of this tuple is the effect. You can read more about effects in the Core Effects section. In this example, there's an effect that adds text to an HTML document, but more importantly, returns effects.none(), which tells ferp that we're done doing work until some other event triggers our application later.

subscribe: (state) => []

1
import { app, effects } from 'ferp';
2
3
import mySubscription from './mySubscription.js';
4
5
const INITIAL_STATE = { counter: 1 };
6
7
app({
8
init: [INITIAL_STATE, effects.none()],
9
10
subscribe: (state) => [mySubscription, state.counter < 10]
11
});
Copied!
This method lets you run subscriptions, which are long-running effects that react to state by starting, restarting, or stopping. Subscriptions are great ways to interact with third-party objects, like websockets, dom watching mechanisms, or other event-based long-running tasks. You can read more about them in the What is a Ferp Subscription? section.

observe: ([currentState, currentEffect], actionAnnotation) => {}

1
import { app, effects } from 'ferp';
2
3
const INITIAL_STATE = { counter: 1 };
4
5
const incrementCounter = (state) => [
6
{ counter: state.counter + 1 },
7
effects.none(),
8
];
9
10
app({
11
init: [INITIAL_STATE, effects.act(incrementCounter)],
12
13
observe: ([state, effect], annotation) => {
14
console.groupCollapsed(annotation, state);
15
console.log(effect);
16
console.groupEnd();
17
}
18
});
Copied!
Observe is an optional app property that lets you see the current state and effect being ran, as well as the action that triggered it (if available). This can be a great way to react to every state change, and not just the changes a subscription would see. For example, if you write a canvas application that must draw after every state update, putting the draw logic in observe may be what you're looking for. Another example would be for an application debug tool, where you output the current state, action, and effects being ran.

const dispatch = app({ ... })

1
import { app, effects } from 'ferp';
2
3
const INITIAL_STATE = { counter: 1 };
4
5
// Action
6
const incrementCounter = (state) => [
7
{ counter: state.counter + 1 },
8
effects.none(),
9
];
10
11
// Action-creator
12
const incrementCounterBy = (amount) => (state) => [
13
{ counter: state.counter + amount },
14
state.none();
15
];
16
17
const dispatch = app({
18
init: [INITIAL_STATE, effects.none()],
19
});
20
21
dispatch(incrementCounter); // Dispatch an action
22
dispatch(incrementBy(5), 'incrementBy'); // Dispatch an action creator, with an annotation
Copied!
Dispatch is how you run new actions to update your state and run additional side effects, and allows subscriptions to react to your state changes. You can call dispatch from anywhere with any action-function. Dispatch can take an action or an action creator, and an optional action annotation. Annotations can be handle for action creators, which don't have function names by default.

The Counter App

counter.browser.js
counter.cli.js
1
import { app, effects } from 'https://unpkg.com/ferp?module=1';
2
3
const createElement = (tag, id, text) => {
4
const element = document.createElement(tag);
5
element.id = id;
6
element.innerText = text;
7
return element;
8
};
9
10
document.body.appendChild(createElement('h1', 'value', '0'));
11
document.body.appendChild(createElement('button', 'incr', '+1'));
12
document.body.appendChild(createElement('button', 'decr', '-1'));
13
14
const elementSetTextEffect = (elementId, text) => effects.thunk(() => {
15
const element = document.getElementById(elementId);
16
if (element) {
17
element.innerText = text.toString();
18
}
19
20
return effects.none();
21
});
22
const elementSetText = (elementId) => function elementSetTextAction(state) { return [
23
state,
24
elementSetTextEffect(elementId, state.toString()),
25
]; };
26
27
const Incr = (state) => [state + 1, effects.act(elementSetText('value'))];
28
const Decr = (state) => [state - 1, effects.act(elementSetText('value'))];
29
30
const elementClickSubscription = (dispatch, elementId, clickAction) => {
31
const callback = (event) => {
32
dispatch(clickAction, `click:${elementId}`);
33
};
34
35
const element = document.getElementById(elementId);
36
element.addEventListener('click', callback);
37
38
return () => {
39
element.removeEventListener('click', callback);
40
};
41
};
42
43
const initialState = 0;
44
45
app({
46
init: [
47
initialState,
48
effects.act(elementSetText('value'))
49
],
50
51
subscribe: () => [
52
[elementClickSubscription, 'incr', Incr],
53
[elementClickSubscription, 'decr', Decr],
54
],
55
});
Copied!
1
import { app, effects } from 'ferp';
2
3
const writeEffect = (value) => effects.thunk(() => {
4
console.log(value);
5
return effects.none();
6
});
7
8
const delayEffect = (milliseconds, nextEffect) => effects.thunk(() => (
9
effects.defer(new Promise((resolve) => {
10
setTimeout(resolve, milliseconds, nextEffect);
11
}))
12
));
13
14
const Tick = (state) => {
15
const times = state.times - 1;
16
const value = state.value + state.changeBy;
17
18
return [
19
{ ...state, times, value },
20
effects.batch([
21
writeEffect(value),
22
times > 0
23
? delayEffect(state.delay, effects.act(Tick))
24
: effects.none(),
25
]),
26
];
27
};
28
29
const initialState = {
30
value: 0,
31
changeBy: 1,
32
times: 10,
33
delay: 30,
34
};
35
36
const dispatch = app({
37
init: [initialState, effects.none()],
38
});
39
40
dispatch(Tick);
41
42
/*
43
// Could also be written as:
44
45
app({
46
init: [initialState, effects.act(Tick)],
47
});
48
*/
Copied!
Last modified 3mo ago