@graffy/core 0.16.10-alpha.2 → 0.16.12-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +93 -79
- package/index.mjs +94 -80
- package/package.json +3 -3
- package/types/Graffy.d.ts +1 -0
- package/types/shift.d.ts +1 -0
package/index.cjs
CHANGED
|
@@ -2,78 +2,93 @@
|
|
|
2
2
|
const common = require("@graffy/common");
|
|
3
3
|
const stream = require("@graffy/stream");
|
|
4
4
|
const debug = require("debug");
|
|
5
|
-
const
|
|
6
|
-
function
|
|
7
|
-
if (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (
|
|
11
|
-
|
|
12
|
-
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
13
|
-
}
|
|
14
|
-
return [[], args[0], args[1]];
|
|
15
|
-
} else {
|
|
16
|
-
return [splitPath(args[0]), args[1], {}];
|
|
5
|
+
const log = debug("graffy:core");
|
|
6
|
+
function resolve(handlers, firstPayload, firstOptions) {
|
|
7
|
+
if (!(handlers == null ? void 0 : handlers.length))
|
|
8
|
+
throw Error("resolve.no_provider");
|
|
9
|
+
function run(i, payload, options) {
|
|
10
|
+
if (i >= handlers.length) {
|
|
11
|
+
throw Error(`resolve.no_providers_for ${JSON.stringify(payload)}`);
|
|
17
12
|
}
|
|
18
|
-
|
|
19
|
-
if (!common.
|
|
20
|
-
|
|
13
|
+
const { path, handle } = handlers[i];
|
|
14
|
+
if (!common.unwrap(payload, path)) {
|
|
15
|
+
return run(i + 1, payload, options);
|
|
21
16
|
}
|
|
22
|
-
|
|
17
|
+
let nextCalled = false;
|
|
18
|
+
return handle(payload, options, (nextPayload, nextOptions) => {
|
|
19
|
+
if (nextCalled) {
|
|
20
|
+
throw Error(`resolve.duplicate_next_call: ${handlers[i].name}`);
|
|
21
|
+
}
|
|
22
|
+
nextCalled = true;
|
|
23
|
+
if (typeof nextPayload === "undefined" || !nextPayload.length)
|
|
24
|
+
return;
|
|
25
|
+
return run(i + 1, nextPayload, nextOptions || options);
|
|
26
|
+
});
|
|
23
27
|
}
|
|
24
|
-
|
|
28
|
+
return run(0, firstPayload, firstOptions);
|
|
25
29
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return
|
|
30
|
+
class Core {
|
|
31
|
+
constructor() {
|
|
32
|
+
this.handlers = {};
|
|
33
|
+
}
|
|
34
|
+
on(type, path, handle) {
|
|
35
|
+
this.handlers[type] = this.handlers[type] || [];
|
|
36
|
+
this.handlers[type].push({ path: common.encodePath(path), handle });
|
|
37
|
+
}
|
|
38
|
+
call(type, payload, options = {}) {
|
|
39
|
+
log("call", type, payload);
|
|
40
|
+
return resolve(this.handlers[type], payload, options);
|
|
37
41
|
}
|
|
38
|
-
throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
|
|
39
42
|
}
|
|
40
43
|
async function mapStream(stream2, fn) {
|
|
41
44
|
for await (const value of stream2) {
|
|
42
45
|
fn(value);
|
|
43
46
|
}
|
|
44
47
|
}
|
|
48
|
+
const unchanged = Symbol("Payload or result unchanged by handler");
|
|
45
49
|
function wrapProvider(fn, decodedPath, isRead) {
|
|
46
50
|
const decodePayload = isRead ? common.decodeQuery : common.decodeGraph;
|
|
47
51
|
const encodePayload = isRead ? common.encodeQuery : common.encodeGraph;
|
|
48
52
|
const path = common.encodePath(decodedPath);
|
|
49
53
|
return async function wrappedProvider(payload, options, next) {
|
|
50
54
|
let nextCalled = false;
|
|
55
|
+
let nextResult;
|
|
51
56
|
let remainingNextResult;
|
|
52
57
|
const porcelainPayload = common.unwrapObject(decodePayload(payload), decodedPath);
|
|
53
58
|
const remainingPayload = common.remove(payload, path) || [];
|
|
54
59
|
async function shiftedNext(porcelainNextPayload, nextOptions) {
|
|
55
60
|
nextCalled = true;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
let nextPayload;
|
|
62
|
+
if (porcelainNextPayload === unchanged) {
|
|
63
|
+
nextPayload = payload;
|
|
64
|
+
} else {
|
|
65
|
+
nextPayload = encodePayload(
|
|
66
|
+
common.wrapObject(porcelainNextPayload, decodedPath)
|
|
67
|
+
);
|
|
68
|
+
if (remainingPayload.length)
|
|
69
|
+
common.merge(nextPayload, remainingPayload);
|
|
70
|
+
}
|
|
71
|
+
nextResult = await next(nextPayload, nextOptions);
|
|
62
72
|
remainingNextResult = common.remove(nextResult, path) || [];
|
|
63
73
|
return common.unwrapObject(common.decodeGraph(nextResult), decodedPath);
|
|
64
74
|
}
|
|
65
75
|
const porcelainResult = await fn(porcelainPayload, options, shiftedNext);
|
|
66
|
-
let result
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
result = common.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
let result;
|
|
77
|
+
if (porcelainResult === unchanged) {
|
|
78
|
+
result = nextResult;
|
|
79
|
+
} else {
|
|
80
|
+
result = common.encodeGraph(common.wrapObject(porcelainResult, decodedPath));
|
|
81
|
+
if (isRead && !nextCalled) {
|
|
82
|
+
const appliedQuery = common.wrap(common.unwrap(payload, path), path);
|
|
83
|
+
result = common.finalize(result, appliedQuery);
|
|
84
|
+
result = common.wrap(common.unwrap(result, path), path);
|
|
85
|
+
}
|
|
86
|
+
if (!nextCalled && remainingPayload.length) {
|
|
87
|
+
remainingNextResult = await next(remainingPayload);
|
|
88
|
+
}
|
|
89
|
+
if (remainingNextResult == null ? void 0 : remainingNextResult.length) {
|
|
90
|
+
common.merge(result, remainingNextResult);
|
|
91
|
+
}
|
|
77
92
|
}
|
|
78
93
|
return result;
|
|
79
94
|
};
|
|
@@ -118,43 +133,42 @@ function shiftGen(fn, path) {
|
|
|
118
133
|
yield* remainingNextStream ? common.mergeStreams(resultStream, remainingNextStream) : resultStream;
|
|
119
134
|
};
|
|
120
135
|
}
|
|
121
|
-
const
|
|
122
|
-
function
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
136
|
+
const splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
|
|
137
|
+
function validateCall(...args) {
|
|
138
|
+
if (args.length === 1) {
|
|
139
|
+
return [[], args[0], {}];
|
|
140
|
+
}
|
|
141
|
+
if (args.length === 2) {
|
|
142
|
+
if (common.isPlainObject(args[0])) {
|
|
143
|
+
if (!common.isPlainObject(args[1])) {
|
|
144
|
+
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
145
|
+
}
|
|
146
|
+
return [[], args[0], args[1]];
|
|
128
147
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
148
|
+
return [splitPath(args[0]), args[1], {}];
|
|
149
|
+
}
|
|
150
|
+
if (args.length === 3) {
|
|
151
|
+
if (!common.isPlainObject(args[2])) {
|
|
152
|
+
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
132
153
|
}
|
|
133
|
-
|
|
134
|
-
return handle(payload, options, (nextPayload, nextOptions) => {
|
|
135
|
-
if (nextCalled) {
|
|
136
|
-
throw Error(`resolve.duplicate_next_call: ${handlers[i].name}`);
|
|
137
|
-
}
|
|
138
|
-
nextCalled = true;
|
|
139
|
-
if (typeof nextPayload === "undefined" || !nextPayload.length)
|
|
140
|
-
return;
|
|
141
|
-
return run(i + 1, nextPayload, nextOptions || options);
|
|
142
|
-
});
|
|
154
|
+
return [splitPath(args[0]), args[1], args[2]];
|
|
143
155
|
}
|
|
144
|
-
|
|
156
|
+
throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
|
|
145
157
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
this.handlers[type].push({ path: common.encodePath(path), handle });
|
|
158
|
+
function validateOn(...args) {
|
|
159
|
+
if (args.length === 1) {
|
|
160
|
+
if (typeof args[0] !== "function") {
|
|
161
|
+
throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[0])}`);
|
|
162
|
+
}
|
|
163
|
+
return [[], args[0]];
|
|
153
164
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
165
|
+
if (args.length === 2) {
|
|
166
|
+
if (typeof args[1] !== "function") {
|
|
167
|
+
throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
|
|
168
|
+
}
|
|
169
|
+
return [splitPath(args[0]), args[1]];
|
|
157
170
|
}
|
|
171
|
+
throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
|
|
158
172
|
}
|
|
159
173
|
class Graffy {
|
|
160
174
|
constructor(path = [], core = new Core()) {
|
|
@@ -182,9 +196,9 @@ class Graffy {
|
|
|
182
196
|
const subscription = handle(common.decodeQuery(query), options, () => {
|
|
183
197
|
throw Error(`porcelain.watch_next_unsupported: ${path}`);
|
|
184
198
|
});
|
|
185
|
-
(async
|
|
199
|
+
(async () => {
|
|
186
200
|
try {
|
|
187
|
-
|
|
201
|
+
const firstValue = (await subscription.next()).value;
|
|
188
202
|
push(firstValue && common.finalize(common.encodeGraph(firstValue), query));
|
|
189
203
|
for await (const value of subscription) {
|
|
190
204
|
push(value && common.encodeGraph(value));
|
package/index.mjs
CHANGED
|
@@ -1,78 +1,93 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { encodePath, unwrap, unwrapObject, remove, encodeGraph, wrapObject, wrap, finalize, merge, mergeStreams, decodeQuery, decodeGraph, encodeQuery, isPlainObject, decorate } from "@graffy/common";
|
|
2
2
|
import { makeStream, mapStream as mapStream$1 } from "@graffy/stream";
|
|
3
3
|
import debug from "debug";
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
if (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
12
|
-
}
|
|
13
|
-
return [[], args[0], args[1]];
|
|
14
|
-
} else {
|
|
15
|
-
return [splitPath(args[0]), args[1], {}];
|
|
4
|
+
const log = debug("graffy:core");
|
|
5
|
+
function resolve(handlers, firstPayload, firstOptions) {
|
|
6
|
+
if (!(handlers == null ? void 0 : handlers.length))
|
|
7
|
+
throw Error("resolve.no_provider");
|
|
8
|
+
function run(i, payload, options) {
|
|
9
|
+
if (i >= handlers.length) {
|
|
10
|
+
throw Error(`resolve.no_providers_for ${JSON.stringify(payload)}`);
|
|
16
11
|
}
|
|
17
|
-
|
|
18
|
-
if (!
|
|
19
|
-
|
|
12
|
+
const { path, handle } = handlers[i];
|
|
13
|
+
if (!unwrap(payload, path)) {
|
|
14
|
+
return run(i + 1, payload, options);
|
|
20
15
|
}
|
|
21
|
-
|
|
16
|
+
let nextCalled = false;
|
|
17
|
+
return handle(payload, options, (nextPayload, nextOptions) => {
|
|
18
|
+
if (nextCalled) {
|
|
19
|
+
throw Error(`resolve.duplicate_next_call: ${handlers[i].name}`);
|
|
20
|
+
}
|
|
21
|
+
nextCalled = true;
|
|
22
|
+
if (typeof nextPayload === "undefined" || !nextPayload.length)
|
|
23
|
+
return;
|
|
24
|
+
return run(i + 1, nextPayload, nextOptions || options);
|
|
25
|
+
});
|
|
22
26
|
}
|
|
23
|
-
|
|
27
|
+
return run(0, firstPayload, firstOptions);
|
|
24
28
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return
|
|
29
|
+
class Core {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.handlers = {};
|
|
32
|
+
}
|
|
33
|
+
on(type, path, handle) {
|
|
34
|
+
this.handlers[type] = this.handlers[type] || [];
|
|
35
|
+
this.handlers[type].push({ path: encodePath(path), handle });
|
|
36
|
+
}
|
|
37
|
+
call(type, payload, options = {}) {
|
|
38
|
+
log("call", type, payload);
|
|
39
|
+
return resolve(this.handlers[type], payload, options);
|
|
36
40
|
}
|
|
37
|
-
throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
|
|
38
41
|
}
|
|
39
42
|
async function mapStream(stream, fn) {
|
|
40
43
|
for await (const value of stream) {
|
|
41
44
|
fn(value);
|
|
42
45
|
}
|
|
43
46
|
}
|
|
47
|
+
const unchanged = Symbol("Payload or result unchanged by handler");
|
|
44
48
|
function wrapProvider(fn, decodedPath, isRead) {
|
|
45
49
|
const decodePayload = isRead ? decodeQuery : decodeGraph;
|
|
46
50
|
const encodePayload = isRead ? encodeQuery : encodeGraph;
|
|
47
51
|
const path = encodePath(decodedPath);
|
|
48
52
|
return async function wrappedProvider(payload, options, next) {
|
|
49
53
|
let nextCalled = false;
|
|
54
|
+
let nextResult;
|
|
50
55
|
let remainingNextResult;
|
|
51
56
|
const porcelainPayload = unwrapObject(decodePayload(payload), decodedPath);
|
|
52
57
|
const remainingPayload = remove(payload, path) || [];
|
|
53
58
|
async function shiftedNext(porcelainNextPayload, nextOptions) {
|
|
54
59
|
nextCalled = true;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
let nextPayload;
|
|
61
|
+
if (porcelainNextPayload === unchanged) {
|
|
62
|
+
nextPayload = payload;
|
|
63
|
+
} else {
|
|
64
|
+
nextPayload = encodePayload(
|
|
65
|
+
wrapObject(porcelainNextPayload, decodedPath)
|
|
66
|
+
);
|
|
67
|
+
if (remainingPayload.length)
|
|
68
|
+
merge(nextPayload, remainingPayload);
|
|
69
|
+
}
|
|
70
|
+
nextResult = await next(nextPayload, nextOptions);
|
|
61
71
|
remainingNextResult = remove(nextResult, path) || [];
|
|
62
72
|
return unwrapObject(decodeGraph(nextResult), decodedPath);
|
|
63
73
|
}
|
|
64
74
|
const porcelainResult = await fn(porcelainPayload, options, shiftedNext);
|
|
65
|
-
let result
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
result =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
let result;
|
|
76
|
+
if (porcelainResult === unchanged) {
|
|
77
|
+
result = nextResult;
|
|
78
|
+
} else {
|
|
79
|
+
result = encodeGraph(wrapObject(porcelainResult, decodedPath));
|
|
80
|
+
if (isRead && !nextCalled) {
|
|
81
|
+
const appliedQuery = wrap(unwrap(payload, path), path);
|
|
82
|
+
result = finalize(result, appliedQuery);
|
|
83
|
+
result = wrap(unwrap(result, path), path);
|
|
84
|
+
}
|
|
85
|
+
if (!nextCalled && remainingPayload.length) {
|
|
86
|
+
remainingNextResult = await next(remainingPayload);
|
|
87
|
+
}
|
|
88
|
+
if (remainingNextResult == null ? void 0 : remainingNextResult.length) {
|
|
89
|
+
merge(result, remainingNextResult);
|
|
90
|
+
}
|
|
76
91
|
}
|
|
77
92
|
return result;
|
|
78
93
|
};
|
|
@@ -117,43 +132,42 @@ function shiftGen(fn, path) {
|
|
|
117
132
|
yield* remainingNextStream ? mergeStreams(resultStream, remainingNextStream) : resultStream;
|
|
118
133
|
};
|
|
119
134
|
}
|
|
120
|
-
const
|
|
121
|
-
function
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
135
|
+
const splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
|
|
136
|
+
function validateCall(...args) {
|
|
137
|
+
if (args.length === 1) {
|
|
138
|
+
return [[], args[0], {}];
|
|
139
|
+
}
|
|
140
|
+
if (args.length === 2) {
|
|
141
|
+
if (isPlainObject(args[0])) {
|
|
142
|
+
if (!isPlainObject(args[1])) {
|
|
143
|
+
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
144
|
+
}
|
|
145
|
+
return [[], args[0], args[1]];
|
|
127
146
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
147
|
+
return [splitPath(args[0]), args[1], {}];
|
|
148
|
+
}
|
|
149
|
+
if (args.length === 3) {
|
|
150
|
+
if (!isPlainObject(args[2])) {
|
|
151
|
+
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
131
152
|
}
|
|
132
|
-
|
|
133
|
-
return handle(payload, options, (nextPayload, nextOptions) => {
|
|
134
|
-
if (nextCalled) {
|
|
135
|
-
throw Error(`resolve.duplicate_next_call: ${handlers[i].name}`);
|
|
136
|
-
}
|
|
137
|
-
nextCalled = true;
|
|
138
|
-
if (typeof nextPayload === "undefined" || !nextPayload.length)
|
|
139
|
-
return;
|
|
140
|
-
return run(i + 1, nextPayload, nextOptions || options);
|
|
141
|
-
});
|
|
153
|
+
return [splitPath(args[0]), args[1], args[2]];
|
|
142
154
|
}
|
|
143
|
-
|
|
155
|
+
throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
|
|
144
156
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
this.handlers[type].push({ path: encodePath(path), handle });
|
|
157
|
+
function validateOn(...args) {
|
|
158
|
+
if (args.length === 1) {
|
|
159
|
+
if (typeof args[0] !== "function") {
|
|
160
|
+
throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[0])}`);
|
|
161
|
+
}
|
|
162
|
+
return [[], args[0]];
|
|
152
163
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
164
|
+
if (args.length === 2) {
|
|
165
|
+
if (typeof args[1] !== "function") {
|
|
166
|
+
throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
|
|
167
|
+
}
|
|
168
|
+
return [splitPath(args[0]), args[1]];
|
|
156
169
|
}
|
|
170
|
+
throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
|
|
157
171
|
}
|
|
158
172
|
class Graffy {
|
|
159
173
|
constructor(path = [], core = new Core()) {
|
|
@@ -181,9 +195,9 @@ class Graffy {
|
|
|
181
195
|
const subscription = handle(decodeQuery(query), options, () => {
|
|
182
196
|
throw Error(`porcelain.watch_next_unsupported: ${path}`);
|
|
183
197
|
});
|
|
184
|
-
(async
|
|
198
|
+
(async () => {
|
|
185
199
|
try {
|
|
186
|
-
|
|
200
|
+
const firstValue = (await subscription.next()).value;
|
|
187
201
|
push(firstValue && finalize(encodeGraph(firstValue), query));
|
|
188
202
|
for await (const value of subscription) {
|
|
189
203
|
push(value && encodeGraph(value));
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graffy/core",
|
|
3
3
|
"description": "The main module for Graffy, a library for intuitive real-time data APIs.",
|
|
4
4
|
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
-
"version": "0.16.
|
|
5
|
+
"version": "0.16.12-alpha.1",
|
|
6
6
|
"main": "./index.cjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
"import": "./index.mjs",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "Apache-2.0",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@graffy/common": "0.16.
|
|
20
|
-
"@graffy/stream": "0.16.
|
|
19
|
+
"@graffy/common": "0.16.12-alpha.1",
|
|
20
|
+
"@graffy/stream": "0.16.12-alpha.1",
|
|
21
21
|
"debug": "^4.3.3"
|
|
22
22
|
}
|
|
23
23
|
}
|
package/types/Graffy.d.ts
CHANGED
package/types/shift.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export function wrapProvider(fn: any, decodedPath: any, isRead: any): (payload: any, options: any, next: any) => Promise<any>;
|
|
2
2
|
export function shiftGen(fn: any, path: any): (payload: any, options: any, next: any) => AsyncGenerator<any, void, any>;
|
|
3
|
+
export const unchanged: unique symbol;
|