-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (151 loc) · 4.43 KB
/
index.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
var q = require('q');
module.exports = ({ access, batch, reference }) => ({
get access() { return access; },
set access(value) { access = value; },
get batch() { return batch; },
set batch(value) { batch = value; },
get reference() { return reference; },
set reference(value) { reference = value; },
join: schema =>
object => {
var queue = [];
var objectrequest = new Map();
var pending = new Set();
object = { $: object };
schema = { $: schema };
q.resolve().then(() => {
collect(object, schema);
flush();
if (!pending.size) deferred.resolve();
});
var deferred = q.defer();
var promise = deferred.promise.then(() => object.$);
Object.defineProperty(promise, '$', { get: () => object.$ });
Object.defineProperty(promise, 'pending', { get: () => pending });
return promise;
function collect(object, schema) {
if (schema instanceof Array) {
var schema = schema[0];
if (typeof schema != 'object')
schema = [schema, {}];
if (schema instanceof Array && schema.length == 2) {
var then = schema[1];
schema = schema[0];
}
if (typeof schema == 'function')
value = value(schema);
object.forEach(
typeof schema == 'string' ?
schema.startsWith('/') ?
(element, i) => {
var [, type, id, relation] = element.split('/');
enqueue(id, type, relation, object, i, then);
} : schema.startsWith('./') ?
(element, i) => {
var relation = element.substr(2);
enqueue(objectrequest[object].id, objectrequest[object].type, relation, object, i, then);
} :
(element, i) => {
enqueue(element, schema, undefined, object, i, then);
} :
!schema ?
(element, i) => {
var r = reference.parse(element);
enqueue(r.id, r.type, undefined, object, i, then);
} :
element => {
collect(element, schema);
}
);
} else if (typeof schema == 'object') {
forEach(schema, (value, key) => {
if (typeof value != 'object')
value = [value, {}];
if (value instanceof Array && value.length == 2) {
var then = value[1];
value = value[0];
}
if (typeof value == 'function')
value = value(object);
if (typeof value == 'string')
if (value.startsWith('/')) {
var [, type, id, relation] = value.split('/');
enqueue(id, type, relation, object, key, then);
} else if (value.startsWith('./')) {
var relation = value.substr(2);
enqueue(objectrequest[object].id, objectrequest[object].type, relation, object, key, then);
} else
enqueue(object[key], value, undefined, object, key, then);
else if (!value) (reference => {
enqueue(reference.id, reference.type, undefined, object, key, then);
})(reference.parse(object[key]));
else
collect(object[key], value);
});
}
}
function flush() {
var promise = [];
var batchrequest = batch(queue);
for (let [batch, request] of batchrequest) {
var p = access.get(batch);
promise.push(p);
p.batch = batch;
p.request = request;
pending.add(p);
}
promise.forEach(promise => {
promise.request.forEach(
request => {
request.object[request.i] = promise;
}
);
promise.then(object => {
pending.delete(promise);
promise.request.forEach(
object instanceof Array ?
(request, i) => {
request.object[request.i] = object[i];
} :
request => {
request.object[request.i] = object[request.id];
}
);
promise.request.forEach(
object instanceof Array ?
(request, i) => {
objectrequest[object[i]] = request;
collect(object[i], request.then);
} :
request => {
objectrequest[object[request.id]] = request;
collect(object[request.id], request.then);
}
);
flush();
if (!pending.size) deferred.resolve();
}, e => {
pending.delete(promise);
deferred.reject(e);
});
});
queue.length = 0;
deferred.notify();
}
function enqueue(id, type, relation, object, i, then) {
var request = {
id: id,
type: type,
relation: relation,
object: object,
i: i,
then: then
};
queue.push(request);
}
}
});
function forEach(object, callback) {
for (var key in object)
callback(object[key], key);
}