1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createActions = exports.createAction = exports.connect = void 0;
|
7 | var _fantasyLand = _interopRequireDefault(require("fantasy-land"));
|
8 | var _forceArray = _interopRequireDefault(require("force-array"));
|
9 | var _utils = require("./utils");
|
10 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
11 | const rFANTASY = /^fantasy-land\//;
|
12 | const alias = c => Object.getOwnPropertyNames(c).filter(n => rFANTASY.test(n)).map(n => [n.replace(rFANTASY, ''), c[n]]);
|
13 | const aliasType = c => {
|
14 |
|
15 | alias(c).forEach(([alias, fn]) => {
|
16 | c[alias] = fn;
|
17 | });
|
18 |
|
19 | alias(c.prototype).forEach(([alias, fn]) => {
|
20 | c.prototype[alias] = fn;
|
21 | });
|
22 | return c;
|
23 | };
|
24 | const createAction = name => {
|
25 |
|
26 | var action = {
|
27 | [name]: class {
|
28 | constructor(value) {
|
29 | this.__value = value;
|
30 | this['@@type'] = name;
|
31 | }
|
32 | static [_fantasyLand.default.of](value) {
|
33 | return new action[name](value);
|
34 | }
|
35 | static of(value) {
|
36 | return new action[name](value);
|
37 | }
|
38 | static is(type) {
|
39 | return type instanceof action[name];
|
40 | }
|
41 | [_fantasyLand.default.map](fn) {
|
42 | return action[name].of(fn(this.join()));
|
43 | }
|
44 | [_fantasyLand.default.ap](m) {
|
45 | return m.chain(fn => this.map(fn));
|
46 | }
|
47 | [_fantasyLand.default.chain](fn) {
|
48 | return fn(this.join());
|
49 | }
|
50 | [_fantasyLand.default.equals](a) {
|
51 | return a.join() === this.join();
|
52 | }
|
53 | lift(m) {
|
54 | return m.map(this.__value);
|
55 | }
|
56 | join() {
|
57 | return this.__value;
|
58 | }
|
59 | unwrapOrElse(_default) {
|
60 | return typeof this.join() === 'undefined' ? _default : this.join();
|
61 | }
|
62 | }
|
63 | };
|
64 | return aliasType(action[name]);
|
65 | };
|
66 | exports.createAction = createAction;
|
67 | const createActions = actions => (0, _forceArray.default)(actions).filter(_utils.isString).map(createAction);
|
68 | exports.createActions = createActions;
|
69 | const connect = signal => actions => createActions(actions).map(Action => {
|
70 | Action.of = value => {
|
71 | const action = new Action(value);
|
72 | signal.emit(action);
|
73 | return action;
|
74 | };
|
75 | Action[_fantasyLand.default.of] = Action.of;
|
76 | return Action;
|
77 | });
|
78 | exports.connect = connect; |
\ | No newline at end of file |