-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra.js
142 lines (109 loc) · 2.84 KB
/
extra.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
import { array } from 'helpers/array';
import { indexBy } from 'helpers/arrayBy';
import { log } from 'helpers/log';
import { ns } from 'helpers/ns';
import { collection, data, fetchRefId, model, opt } from './data';
const extras = data.extras;
export async function extra(expeditor) {
// skip nullable field
// skip root request
if (expeditor.nullable || expeditor.isRoot()) {
return;
}
// find absent records
var ids = plans(expeditor);
if (!ids || ids.length === 0) {
return;
}
var field = fields(expeditor).join('.');
var top = expeditor.bubble();
var k = [ field, ids ].join('/');
var n = ns(extras, top.model);
// fetch extra once
if (k in n) {
return n[k];
}
log('extra "' + top.model + '"', { [field]: ids }, { expeditor });
if ((n[k] = opt.extra)) {
n[k] = opt.extra(top.model, ids, field, expeditor.model);
await register(await n[k], expeditor);
n[k] = null;
}
}
function fields(expeditor) {
return expeditor
.bubble((expeditor, buffer) => {
buffer.unshift(expeditor.field);
}, [])
.slice(1);
}
function plans(expeditor) {
var ids =
opt.absent(expeditor) ||
absent(expeditor);
// skip full data
if (!ids || ids.length === 0) {
return;
}
log('extra "' + expeditor.model + '"', { absent: ids.slice() });
var field = fields(expeditor);
var top = expeditor.bubble();
var map = {};
if (expeditor.index !== 'id') {
field = field.slice(0, -1);
}
field = field.join('.');
// reduce source records poined to the same absent target record
top.data.some(record => {
var source = field ?
record.query(field, fetchRefId) :
[ record.id ];
source.forEach((id, i) => {
if ((i = ids.indexOf(id)) !== -1) {
ids.splice(i, 1);
map[record.id] = record.id;
}
});
return ids.length === 0;
});
return Object.values(map);
}
function absent(expeditor) {
var c = collection(expeditor.model);
if (c) {
var a = expeditor.params[expeditor.index];
var i = c.index(expeditor.index);
// find absent records
return array(a).filter(v =>
!i.contains(v)
);
}
}
function register(data, expeditor) {
if (!data || data.length === 0) {
return;
}
var target = expeditor.model;
var m = model(target);
data = data.map(row =>
m.create(row)
);
if (expeditor.index === 'id') {
collection(target).splice(data, 'extra');
}
else if (opt.classify) {
return classify(data, target);
}
}
async function classify(data, target) {
var ids = [];
var hash = indexBy(data, (record) =>
ids.push(record.id) &&
record.id
);
var result = await opt.classify(target, ids);
var valid = result.valid .map(id => hash[id]);
var invalid = result.invalid.map(id => hash[id]);
collection(target).splice(valid);
collection(target).splice(invalid, 'extra');
}