| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 | 1x
1x
1x
1x
45x
84x
84x
45x
212x
45x
84x
84x
84x
84x
45x
34x
72x
37x
37x
37x
37x
11x
12x
10x
21x
21x
1x
62x
62x
62x
62x
62x
62x
62x
11x
11x
11x
11x
11x
11x
10x
10x
10x
11x
11x
1x
73x
1x
73x
1x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
13x
13x
5x
13x
| import Rx from '@evo/rx';
import { INIT } from './coreActions';
import { actionIs } from './helpers.js';
const WRONG_UPDATE_HANDLER = 'Dispatcher must be subclass of TanokDispatcher or iterable';
function isIterable (object) {
return object != null && typeof object[Symbol.iterator] === 'function';
}
function isFunction(x) {
return Object.prototype.toString.call(x) === '[object Function]';
}
function maybeWrapActionIs(condition) {
return isFunction(condition) ? condition : actionIs(condition);
}
function commonDispatch(stream, updateHandlers, filterName, mapperFn) {
Iif (!isIterable(updateHandlers)) {
throw new Error(WRONG_UPDATE_HANDLER)
}
const currentStream = stream.filter(({ streamName }) => streamName === filterName);
const dispatcherArray = Array.from(updateHandlers)
.map((actionPair) => {
const actionCondition = actionPair[0];
const actionHandler = actionPair[1];
return actionCondition
.reduce((accStream, cond) => maybeWrapActionIs(cond).call(accStream), currentStream)
.map(mapperFn(actionHandler))
});
return Rx.Observable.merge.apply(undefined, dispatcherArray);
}
export function dispatch(stream, updateHandlers, filterName) {
return commonDispatch(
stream, updateHandlers, filterName,
(actionHandler) => (params) => (state) => {
const actionResult = actionHandler(params.payload, state, params);
const newState = actionResult[0];
const effects = actionResult.slice(1);
return {state: newState, effects, params};
})
}
function dispatchSub(stream, updateHandlers, filterName) {
return commonDispatch(
stream, updateHandlers, filterName,
(actionHandler) => (params) => {
return [(state) => actionHandler(params.payload, state, params), params];
}
)
}
function getSubNamePath(parentStreamNamePath, streamName) {
return `${parentStreamNamePath}.${streamName}`;
}
function createSubStreamWrapper(parentStream, streamNamePath) {
return new StreamWrapper(parentStream.stream, streamNamePath);
}
export const StreamWrapper = function(stream, streamName) {
this.stream = stream;
this.streamName = streamName;
this.disposable = null;
this.metadata = [];
this.shutdownActions = [];
this.subs = {};
this.subsWithMeta = new WeakMap();
}
StreamWrapper.prototype.subStream = function(subName, subUpdate) {
const subNamePath = getSubNamePath(this.streamName, subName);
const subStreamWrapper = createSubStreamWrapper(this, subNamePath);
subStreamWrapper.metadata = Array.prototype.concat(this.metadata, null);
this.subs[subNamePath] = subStreamWrapper;
subStreamWrapper.disposable = dispatchSub(this.stream, subUpdate, subNamePath)
.do((parentPayload) => {
const stateMutator = parentPayload[0];
const params = parentPayload[1];
return this.stream.onNext({
action: subName,
payload: stateMutator,
streamName: this.streamName,
metadata: params.metadataArray[params.metadataArray.length - 1],
metadataArray: params.metadataArray.slice(0, -1),
stack: params,
})
})
.subscribe(
Rx.helpers.noop,
console.error.bind(console)
);
subStreamWrapper.send(INIT);
return subStreamWrapper;
};
StreamWrapper.prototype.send = function(action, payload) {
this.stream.onNext(this.getStreamPayload(action, payload));
}
StreamWrapper.prototype.getStreamPayload = function(action, payload) {
return {
action,
payload,
streamName: this.streamName,
metadataArray: this.metadata,
};
}
StreamWrapper.prototype.subWithMeta = function(subName, metadata) {
const subNamePath = getSubNamePath(this.streamName, subName);
const subStream = this.subs[subNamePath];
Iif (!subStream) {
return null;
}
const key = {subNamePath, metadata};
const storage = this.subsWithMeta;
Iif (storage.has(key)) {
return storage.get(key)
}
const mock = createSubStreamWrapper(subStream, subStream.streamName);
mock.metadata = this.metadata.concat(metadata);
mock.subs = subStream.subs;
storage.set(key, mock);
return mock;
}
StreamWrapper.prototype.onShutdown = function() {
Object.keys(this.subs).forEach((subStreamKey) => {
this.subs[subStreamKey].onShutdown();
});
this.shutdownActions.forEach((shutdownAction) => shutdownAction());
}
|