Skip to content

Instantly share code, notes, and snippets.

@popuguytheparrot
Created December 30, 2019 15:57
Show Gist options
  • Save popuguytheparrot/ef9b3e6a3c531239fee21443dda1a077 to your computer and use it in GitHub Desktop.
Save popuguytheparrot/ef9b3e6a3c531239fee21443dda1a077 to your computer and use it in GitHub Desktop.
import { createEvent, createStore, forward, guard, sample } from 'effector';
import nanoid from 'nanoid';
export const show = createEvent('show Notify');
export const hide = createEvent('hide Notify');
/**
* @type {Event<{variant: string, message: string}>}
* @property {string} variant info | success | warning | error
*/
export const notify = createEvent('notify');
export const $notifications = createStore([]);
const hideFirst = $notifications.map(store => store.length > 5);
const firstId = sample($notifications, show).map(notifications => {
const { id } = notifications[0];
return id;
});
$notifications.on(show, (state, notification) => state.concat(notification));
$notifications.on(hide, (state, id) =>
state.filter(notification => notification.id !== id)
);
forward({
from: notify.map(payload => ({ id: nanoid(), ...payload })),
to: show
});
guard({
source: firstId,
filter: hideFirst,
target: hide
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment