UNPKG

1.21 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.untypeEvent = exports.typeEvent = void 0;
7var _actions = require("./actions");
8const typeId = '@@type';
9const globalStore = {};
10
11/**
12 * Converts regular events in to typed events
13 * Can be used with update functions that expect type events
14 */
15const typeEvent = (update, store = globalStore, create = _actions.createAction) => {
16 const map = event => {
17 if (!event.type) {
18 return event;
19 }
20 const {
21 type,
22 payload = null
23 } = event;
24 const typed = store[type];
25 if (typed) {
26 return typed.of(payload);
27 }
28 store[type] = create(type);
29 return store[type].of(payload);
30 };
31 return (state, event) => update(state, map(event));
32};
33
34/**
35 * Converts typed events in to regular events
36 * Can be used with update functions that expect regular events
37 */
38exports.typeEvent = typeEvent;
39const untypeEvent = update => {
40 const map = event => {
41 if (!event.join || !event[typeId]) {
42 return event;
43 }
44 return {
45 type: event[typeId],
46 payload: event.join()
47 };
48 };
49 return (state, event) => update(state, map(event));
50};
51exports.untypeEvent = untypeEvent;
\No newline at end of file