@graffy/core 0.16.10-alpha.2 → 0.16.11
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 +64 -62
- package/index.mjs +65 -63
- package/package.json +3 -3
package/index.cjs
CHANGED
|
@@ -2,40 +2,43 @@
|
|
|
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) {
|
|
@@ -118,43 +121,42 @@ function shiftGen(fn, path) {
|
|
|
118
121
|
yield* remainingNextStream ? common.mergeStreams(resultStream, remainingNextStream) : resultStream;
|
|
119
122
|
};
|
|
120
123
|
}
|
|
121
|
-
const
|
|
122
|
-
function
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
const splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
|
|
125
|
+
function validateCall(...args) {
|
|
126
|
+
if (args.length === 1) {
|
|
127
|
+
return [[], args[0], {}];
|
|
128
|
+
}
|
|
129
|
+
if (args.length === 2) {
|
|
130
|
+
if (common.isPlainObject(args[0])) {
|
|
131
|
+
if (!common.isPlainObject(args[1])) {
|
|
132
|
+
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
133
|
+
}
|
|
134
|
+
return [[], args[0], args[1]];
|
|
128
135
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
136
|
+
return [splitPath(args[0]), args[1], {}];
|
|
137
|
+
}
|
|
138
|
+
if (args.length === 3) {
|
|
139
|
+
if (!common.isPlainObject(args[2])) {
|
|
140
|
+
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
132
141
|
}
|
|
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
|
-
});
|
|
142
|
+
return [splitPath(args[0]), args[1], args[2]];
|
|
143
143
|
}
|
|
144
|
-
|
|
144
|
+
throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
|
|
145
145
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
this.handlers[type].push({ path: common.encodePath(path), handle });
|
|
146
|
+
function validateOn(...args) {
|
|
147
|
+
if (args.length === 1) {
|
|
148
|
+
if (typeof args[0] !== "function") {
|
|
149
|
+
throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[0])}`);
|
|
150
|
+
}
|
|
151
|
+
return [[], args[0]];
|
|
153
152
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
153
|
+
if (args.length === 2) {
|
|
154
|
+
if (typeof args[1] !== "function") {
|
|
155
|
+
throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
|
|
156
|
+
}
|
|
157
|
+
return [splitPath(args[0]), args[1]];
|
|
157
158
|
}
|
|
159
|
+
throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
|
|
158
160
|
}
|
|
159
161
|
class Graffy {
|
|
160
162
|
constructor(path = [], core = new Core()) {
|
|
@@ -182,9 +184,9 @@ class Graffy {
|
|
|
182
184
|
const subscription = handle(common.decodeQuery(query), options, () => {
|
|
183
185
|
throw Error(`porcelain.watch_next_unsupported: ${path}`);
|
|
184
186
|
});
|
|
185
|
-
(async
|
|
187
|
+
(async () => {
|
|
186
188
|
try {
|
|
187
|
-
|
|
189
|
+
const firstValue = (await subscription.next()).value;
|
|
188
190
|
push(firstValue && common.finalize(common.encodeGraph(firstValue), query));
|
|
189
191
|
for await (const value of subscription) {
|
|
190
192
|
push(value && common.encodeGraph(value));
|
package/index.mjs
CHANGED
|
@@ -1,40 +1,43 @@
|
|
|
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) {
|
|
@@ -117,43 +120,42 @@ function shiftGen(fn, path) {
|
|
|
117
120
|
yield* remainingNextStream ? mergeStreams(resultStream, remainingNextStream) : resultStream;
|
|
118
121
|
};
|
|
119
122
|
}
|
|
120
|
-
const
|
|
121
|
-
function
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
const splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
|
|
124
|
+
function validateCall(...args) {
|
|
125
|
+
if (args.length === 1) {
|
|
126
|
+
return [[], args[0], {}];
|
|
127
|
+
}
|
|
128
|
+
if (args.length === 2) {
|
|
129
|
+
if (isPlainObject(args[0])) {
|
|
130
|
+
if (!isPlainObject(args[1])) {
|
|
131
|
+
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
132
|
+
}
|
|
133
|
+
return [[], args[0], args[1]];
|
|
127
134
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
135
|
+
return [splitPath(args[0]), args[1], {}];
|
|
136
|
+
}
|
|
137
|
+
if (args.length === 3) {
|
|
138
|
+
if (!isPlainObject(args[2])) {
|
|
139
|
+
throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
|
|
131
140
|
}
|
|
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
|
-
});
|
|
141
|
+
return [splitPath(args[0]), args[1], args[2]];
|
|
142
142
|
}
|
|
143
|
-
|
|
143
|
+
throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
|
|
144
144
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
this.handlers[type].push({ path: encodePath(path), handle });
|
|
145
|
+
function validateOn(...args) {
|
|
146
|
+
if (args.length === 1) {
|
|
147
|
+
if (typeof args[0] !== "function") {
|
|
148
|
+
throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[0])}`);
|
|
149
|
+
}
|
|
150
|
+
return [[], args[0]];
|
|
152
151
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
152
|
+
if (args.length === 2) {
|
|
153
|
+
if (typeof args[1] !== "function") {
|
|
154
|
+
throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
|
|
155
|
+
}
|
|
156
|
+
return [splitPath(args[0]), args[1]];
|
|
156
157
|
}
|
|
158
|
+
throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
|
|
157
159
|
}
|
|
158
160
|
class Graffy {
|
|
159
161
|
constructor(path = [], core = new Core()) {
|
|
@@ -181,9 +183,9 @@ class Graffy {
|
|
|
181
183
|
const subscription = handle(decodeQuery(query), options, () => {
|
|
182
184
|
throw Error(`porcelain.watch_next_unsupported: ${path}`);
|
|
183
185
|
});
|
|
184
|
-
(async
|
|
186
|
+
(async () => {
|
|
185
187
|
try {
|
|
186
|
-
|
|
188
|
+
const firstValue = (await subscription.next()).value;
|
|
187
189
|
push(firstValue && finalize(encodeGraph(firstValue), query));
|
|
188
190
|
for await (const value of subscription) {
|
|
189
191
|
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.11",
|
|
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.11",
|
|
20
|
+
"@graffy/stream": "0.16.11",
|
|
21
21
|
"debug": "^4.3.3"
|
|
22
22
|
}
|
|
23
23
|
}
|