ferp-js
Search…
Composing Custom Effects

Being composable

Use the core effects is meant to be easy. That's because they are composable, which means you could nest the effects inside each other. For example, if you want to run an action through a thunk and promise, you could easily write:
1
effects.thunk(() => (
2
effects.defer((resolve) => {
3
resolve(effects.act((state) => [state, effects.none()]);
4
})
5
))
Copied!
Of course, if you do this frequently, it's probably a good time to wrap up the common pattern in a custom effect.

Composing Complex Effects

Let's start off with a larger effect, one that I actually wrote for a demo project, and we'll go through the refactoring steps I had to making this two composable and powerful effects.
noncomposableNotificationEffect.js
1
import { effects } from 'ferp';
2
3
export const notificationEffect = (title, body) => effects.defer(
4
Notification.requestPermission()
5
.then((result) => {
6
if (result === 'granted') {
7
const notification = new Notification(title, { body });
8
}
9
})
10
.catch(() => {})
11
.then(() => {
12
return effects.none();
13
});
14
);
15
16
// usage: notificationEffect('Ferp.JS', 'Hello world!');
Copied!
This is not awful, but it's also not great. There is no way to react to different permission settings, and the permission request is locked to showing individual notifications. My first refactor was to separate out the permission request from the actual notification:
notificationEffects.js
1
import { effects } from 'ferp'
2
3
export const notificationPermissionEffect = (
4
grantEffect,
5
denyEffect = effects.none,
6
dismissEffect = effects.none,
7
errorEffect = effects.none
8
) => effects.defer(
9
Notification.requestPermission()
10
.then((result) => {
11
switch (result) {
12
case 'granted':
13
return grantEffect();
14
case 'denied':
15
return denyEffect();
16
default:
17
return dismissEffect();
18
}
19
})
20
.catch(errorEffect)
21
);
22
23
export const notificationEffect = (title, body) => notificationPermissionEffect(
24
() => effects.none(new Notification(title, { body })),
25
);
26
27
// usage: notificationEffect('Ferp.JS', 'Hello world');
Copied!
That feels better and keeps the same signature as the first, but the notification effect will eat permission denied, dismissed and error, and it's limited in how I can react to the notification further. Let's explore an alternative composition:
composableNotificationEffects.js
1
import { effects } from 'ferp'
2
3
export const notificationPermissionEffect = (
4
grantEffect,
5
denyEffect = effects.none,
6
dismissEffect = effects.none,
7
errorEffect = effects.none
8
) => effects.defer(
9
Notification.requestPermission()
10
.then((result) => {
11
switch (result) {
12
case 'granted':
13
return grantEffect();
14
case 'denied':
15
return denyEffect();
16
default:
17
return dismissEffect();
18
}
19
})
20
.catch(errorEffect)
21
);
22
23
export const notificationEffect = (title, options = {}, resultEffect = effects.none) => effects.thunk(() =>
24
resultEffect(new Notification(title, options))
25
);
26
27
// Usage:
28
// notifiationPermissionEffect(notificationEffect('Ferp.JS', { body: 'Hello world!' }))
Copied!
This is a little more dense, but it offers the greatest flexibility. It would be easy to compose a message effect to store the notification in the state to react to it's events in a subscription. That could look like this:
notificationExample.js
1
import { app, effects, sub } from 'ferp';
2
import { notificationPermissionEffect, notificationEffect } from './composableNotificationEffects.js';
3
4
const INITIAL_STATE = {
5
notifications: [],
6
};
7
8
const NotificationAdd = (notification) => (state) => [
9
{ ...state, notifications: state.notifications.concat(notification) },
10
effects.none(),
11
];
12
13
const NotificationRemove = (notification) => (state) => [
14
{ ...state, notifications: state.notifications((n) => n !== notification) },
15
effects.none(),
16
];
17
18
const NotificationCreate = (title) => (state) => [
19
state,
20
notificationEffect(title, undefined, (notification) => effects.act(NotificationAdd, notification)),
21
];
22
23
const delayEffect = (timeout, nextEffect) effects.thunk(() => effects.defer((resolve) => {
24
setTimeout(resolve, timeout, nextEffect);
25
});
26
27
const notificationSubscription = (notification, onInteract) => (dispatch) => {
28
const onClose = (event) => {
29
dispatch(onInteract(notification));
30
};
31
32
const onClick = (event) => {
33
dispatch(onInteract(notification));
34
};
35
36
notification.addEventListener('click', onClick);
37
notification.addEventListener('close', onClose);
38
39
return () => {
40
notification.removeEventListener('click', onClick);
41
notification.removeEventListener('close', onClose);
42
};
43
};
44
45
46
app({
47
init: [
48
INITIAL_STATE,
49
effects.batch([
50
notifyEffect('First Notification'),
51
delayEffect(1000, () => effects.batch([
52
notifyEffect('Second Notification')),
53
delayEffect(3000, () => notifyEffect('Third Notification')),
54
]),
55
]),
56
],
57
subscribe: (state) => [
58
...state.notifications.map(notification => sub(
59
notificationSubscription,
60
notification,
61
NotificationRemove,
62
)),
63
],
64
});
Copied!
Last modified 8mo ago