-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpeditor.js
234 lines (186 loc) · 4.83 KB
/
expeditor.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import noop from 'underscore/modules/noop.js';
import { applyOwn, applyTo } from 'helpers/apply';
import { indexBy } from 'helpers/arrayBy';
import { ymd } from 'helpers/datetime';
import { empty } from 'helpers/empty';
import { log, raise } from 'helpers/log';
import { uniq } from 'helpers/uniq';
import { cascade } from './cascade';
import { model } from './data';
import { Loader, addParam } from './loader';
export class Expeditor {
constructor(o) {
this.aborted = o.aborted || noop;
this.inversed = o.inversed || [ 'id' ];
this.model = model(o.model).aka;
if (o.fields) {
this.fields = uniq(o.fields);
}
for (var field in o.params) {
addParam(field, o.params[field], this);
}
applyTo(this, o, '_parent', 'field', 'index', 'name', 'nullable');
this.range(o.range);
this.names();
if (this.initialize) {
this.initialize(o);
}
}
range(range) {
var p = this.params;
if (p && !range && this.isRoot()) {
if (p.year) {
p.minYmd =
ymd(new Date(p.year, (p.month || 1) - 1, p.day || 1));
p.maxYmd = p.day ? p.minYmd :
ymd(new Date(p.year, p.month || 12, 0));
delete p.year;
delete p.month;
delete p.day;
}
if (p.minYmd || p.maxYmd) {
range = [
p.minYmd || p.maxYmd,
p.maxYmd || p.minYmd,
];
delete p.minYmd;
delete p.maxYmd;
}
}
this.range = range;
}
names() {
if (
!(this.names = this.getNames()) ||
!this.names.ymd && !this.names.edge
) {
this.names = {};
return;
}
// find nearest range
this.bubble(expeditor => this.range = expeditor.range);
if (!this.range && empty(this.params)) {
raise(this, '"' + this.model + '" is timebased',
'and must be fetched with params or range', { expeditor: this });
}
}
getNames() {
return names(model(this.model).fieldsMap);
}
spawn(o) {
return new Expeditor(applyOwn({
_parent: this,
aborted: this.aborted,
name: this.name,
range: this.expanded,
}, o));
}
bubble(fn, buffer) {
for (var expeditor = this; expeditor; expeditor = expeditor._parent) {
if (fn && fn(expeditor, buffer) || !expeditor._parent) {
return buffer || expeditor;
}
}
}
isRoot() {
return !this._parent;
}
async sequent(data) {
if (this.aborted()) {
return [];
}
data = await this.load(data);
data = this.filter(data);
data = this.ranger(data);
this.expand(data);
await this.cascade(data);
return data;
}
load(data) {
log(this, 'load "' + this.model + '"', this.params, { expeditor: this });
return this.getLoader()
.load(this, data);
}
getLoader() {
var o = {
model: this.model,
params: this.params,
};
if (!empty(this.names)) {
o.names = this.names;
o.range = this.range;
}
return new Loader(o);
}
filter(data) {
// filter extra only in root request
if (this.isRoot()) {
data = data.filter(record => !record.extra);
}
return data;
}
isRanged() {
return Boolean(this.range && this.names.edge);
}
ranger(data) {
if (this.isRanged()) {
data = data.filter(this._ranger, this);
}
return data;
}
_ranger(record) {
var min = this.range[0];
var max = this.range[1];
var begin = record[this.names.edge[0]];
var end = record[this.names.edge[1]];
return (!begin || begin <= max) && (!end || min <= end);
}
expand(data) {
if (this.isRanged() && !this.expanded) {
this.expanded = [
this._expand('shift', 0, data),
this._expand('pop', 1, data),
];
}
}
_expand(method, i, data) {
var k = this.names.edge[i];
var v = this.range[i];
var hash = indexBy(data, k, k);
hash[v] = v;
var values = Object.values(hash);
while (values.length > 0) {
if ((v = values[method]())) {
return v;
}
}
raise(this, '"' + this.model + '" expand require range value', { expeditor: this });
}
cascade(data) {
if (this.fields && this.isRoot() && !this.aborted()) {
log(this, 'dependencies', { fields: this.fields }, { expeditor: this });
return cascade(this.data = data, this.fields, this);
}
}
}
const aymd = [ 'year', 'month', 'day' ];
const aym = [ 'year', 'month' ];
const ay = [ 'year' ];
const abe = [ 'begin', 'end' ];
function names(fieldsMap) {
return {
ymd:
fieldsMap.year >= 0 ? // eslint-disable-line no-nested-ternary
fieldsMap.month >= 0 ? // eslint-disable-line no-nested-ternary
fieldsMap.day >= 0 ?
aymd :
aym :
ay :
false,
edge:
fieldsMap.begin >= 0 &&
fieldsMap.end >= 0 ?
abe :
false,
};
}