@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 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 splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
6
- function validateCall(...args) {
7
- if (args.length === 1) {
8
- return [[], args[0], {}];
9
- } else if (args.length === 2) {
10
- if (common.isPlainObject(args[0])) {
11
- if (!common.isPlainObject(args[1])) {
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
- } else if (args.length === 3) {
19
- if (!common.isPlainObject(args[2])) {
20
- throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
13
+ const { path, handle } = handlers[i];
14
+ if (!common.unwrap(payload, path)) {
15
+ return run(i + 1, payload, options);
21
16
  }
22
- return [splitPath(args[0]), args[1], args[2]];
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
- throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
28
+ return run(0, firstPayload, firstOptions);
25
29
  }
26
- function validateOn(...args) {
27
- if (args.length === 1) {
28
- if (typeof args[0] !== "function") {
29
- throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[0])}`);
30
- }
31
- return [[], args[0]];
32
- } else if (args.length === 2) {
33
- if (typeof args[1] !== "function") {
34
- throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
35
- }
36
- return [splitPath(args[0]), args[1]];
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
- const nextPayload = encodePayload(
57
- common.wrapObject(porcelainNextPayload, decodedPath)
58
- );
59
- if (remainingPayload.length)
60
- common.merge(nextPayload, remainingPayload);
61
- const nextResult = await next(nextPayload, nextOptions);
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 = common.encodeGraph(common.wrapObject(porcelainResult, decodedPath));
67
- if (isRead && !nextCalled) {
68
- const appliedQuery = common.wrap(common.unwrap(payload, path), path);
69
- result = common.finalize(result, appliedQuery);
70
- result = common.wrap(common.unwrap(result, path), path);
71
- }
72
- if (!nextCalled && remainingPayload.length) {
73
- remainingNextResult = await next(remainingPayload);
74
- }
75
- if (remainingNextResult == null ? void 0 : remainingNextResult.length) {
76
- common.merge(result, remainingNextResult);
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 log = debug("graffy:core");
122
- function resolve(handlers, firstPayload, firstOptions) {
123
- if (!(handlers == null ? void 0 : handlers.length))
124
- throw Error("resolve.no_provider");
125
- function run(i, payload, options) {
126
- if (i >= handlers.length) {
127
- throw Error(`resolve.no_providers_for ${JSON.stringify(payload)}`);
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
- const { path, handle } = handlers[i];
130
- if (!common.unwrap(payload, path)) {
131
- return run(i + 1, payload, options);
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
- let nextCalled = false;
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
- return run(0, firstPayload, firstOptions);
156
+ throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
145
157
  }
146
- class Core {
147
- constructor() {
148
- this.handlers = {};
149
- }
150
- on(type, path, handle) {
151
- this.handlers[type] = this.handlers[type] || [];
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
- call(type, payload, options = {}) {
155
- log("call", type, payload);
156
- return resolve(this.handlers[type], payload, options);
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 function() {
199
+ (async () => {
186
200
  try {
187
- let firstValue = (await subscription.next()).value;
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 { isPlainObject, encodePath, unwrapObject, remove, encodeGraph, wrapObject, wrap, unwrap, finalize, merge, mergeStreams, decodeQuery, decodeGraph, encodeQuery, decorate } from "@graffy/common";
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 splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
5
- function validateCall(...args) {
6
- if (args.length === 1) {
7
- return [[], args[0], {}];
8
- } else if (args.length === 2) {
9
- if (isPlainObject(args[0])) {
10
- if (!isPlainObject(args[1])) {
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
- } else if (args.length === 3) {
18
- if (!isPlainObject(args[2])) {
19
- throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
12
+ const { path, handle } = handlers[i];
13
+ if (!unwrap(payload, path)) {
14
+ return run(i + 1, payload, options);
20
15
  }
21
- return [splitPath(args[0]), args[1], args[2]];
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
- throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
27
+ return run(0, firstPayload, firstOptions);
24
28
  }
25
- function validateOn(...args) {
26
- if (args.length === 1) {
27
- if (typeof args[0] !== "function") {
28
- throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[0])}`);
29
- }
30
- return [[], args[0]];
31
- } else if (args.length === 2) {
32
- if (typeof args[1] !== "function") {
33
- throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
34
- }
35
- return [splitPath(args[0]), args[1]];
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
- const nextPayload = encodePayload(
56
- wrapObject(porcelainNextPayload, decodedPath)
57
- );
58
- if (remainingPayload.length)
59
- merge(nextPayload, remainingPayload);
60
- const nextResult = await next(nextPayload, nextOptions);
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 = encodeGraph(wrapObject(porcelainResult, decodedPath));
66
- if (isRead && !nextCalled) {
67
- const appliedQuery = wrap(unwrap(payload, path), path);
68
- result = finalize(result, appliedQuery);
69
- result = wrap(unwrap(result, path), path);
70
- }
71
- if (!nextCalled && remainingPayload.length) {
72
- remainingNextResult = await next(remainingPayload);
73
- }
74
- if (remainingNextResult == null ? void 0 : remainingNextResult.length) {
75
- merge(result, remainingNextResult);
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 log = debug("graffy:core");
121
- function resolve(handlers, firstPayload, firstOptions) {
122
- if (!(handlers == null ? void 0 : handlers.length))
123
- throw Error("resolve.no_provider");
124
- function run(i, payload, options) {
125
- if (i >= handlers.length) {
126
- throw Error(`resolve.no_providers_for ${JSON.stringify(payload)}`);
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
- const { path, handle } = handlers[i];
129
- if (!unwrap(payload, path)) {
130
- return run(i + 1, payload, options);
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
- let nextCalled = false;
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
- return run(0, firstPayload, firstOptions);
155
+ throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
144
156
  }
145
- class Core {
146
- constructor() {
147
- this.handlers = {};
148
- }
149
- on(type, path, handle) {
150
- this.handlers[type] = this.handlers[type] || [];
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
- call(type, payload, options = {}) {
154
- log("call", type, payload);
155
- return resolve(this.handlers[type], payload, options);
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 function() {
198
+ (async () => {
185
199
  try {
186
- let firstValue = (await subscription.next()).value;
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.10-alpha.2",
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.10-alpha.2",
20
- "@graffy/stream": "0.16.10-alpha.2",
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
@@ -1,3 +1,4 @@
1
+ export { unchanged } from "./shift.js";
1
2
  export default class Graffy {
2
3
  constructor(path?: any[], core?: Core);
3
4
  core: Core;
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;