Subscriptions

Recipes

Passing effect creators

A great way to make a reusable subscription is to allow dynamic effects to be dispatched.

const { app, effects } = require('ferp');
const tickEffect = () => ({ type: 'tick' });
const tickSubscription = (delay, effectFn) => dispatch => {
console.log('Create tick sub every', delay, 'milliseconds');
const handle = setInterval(effects.thunk, delay, effectFn);
return () => {
console.log('Destroy tick sub every', delay, 'milliseconds');
clearInterval(handle);
};
}
app({
init: [{}, effects.none()],
update: (message, state) => [state, effects.none()],
subscribe: state => [
[tickSubscription, 1000, tickEffect], // Try swapping tickEffect for your favorite time-sensitive effect for your app!
],
});