@graffy/core 0.15.25 → 0.16.0-alpha.10

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.
Files changed (3) hide show
  1. package/index.cjs +37 -20
  2. package/index.mjs +35 -18
  3. package/package.json +3 -3
package/index.cjs CHANGED
@@ -4,26 +4,24 @@ const stream = require("@graffy/stream");
4
4
  const debug = require("debug");
5
5
  const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
6
6
  const debug__default = /* @__PURE__ */ _interopDefaultLegacy(debug);
7
- function isPlainObject(obj) {
8
- return obj && typeof obj === "object" && !Array.isArray(obj);
9
- }
7
+ const splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
10
8
  function validateCall(...args) {
11
9
  if (args.length === 1) {
12
10
  return [[], args[0], {}];
13
11
  } else if (args.length === 2) {
14
- if (isPlainObject(args[0])) {
15
- if (!isPlainObject(args[1])) {
12
+ if (common.isPlainObject(args[0])) {
13
+ if (!common.isPlainObject(args[1])) {
16
14
  throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
17
15
  }
18
16
  return [[], args[0], args[1]];
19
17
  } else {
20
- return [common.encodePath(args[0]), args[1], {}];
18
+ return [splitPath(args[0]), args[1], {}];
21
19
  }
22
20
  } else if (args.length === 3) {
23
- if (!isPlainObject(args[2])) {
21
+ if (!common.isPlainObject(args[2])) {
24
22
  throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
25
23
  }
26
- return [common.encodePath(args[0]), args[1], args[2]];
24
+ return [splitPath(args[0]), args[1], args[2]];
27
25
  }
28
26
  throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
29
27
  }
@@ -37,7 +35,7 @@ function validateOn(...args) {
37
35
  if (typeof args[1] !== "function") {
38
36
  throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
39
37
  }
40
- return [common.encodePath(args[0]), args[1]];
38
+ return [splitPath(args[0]), args[1]];
41
39
  }
42
40
  throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
43
41
  }
@@ -47,6 +45,7 @@ async function mapStream(stream2, fn) {
47
45
  }
48
46
  }
49
47
  function shiftFn(fn, path) {
48
+ path = common.encodePath(path);
50
49
  return async function shiftedFn(payload, options, next) {
51
50
  let nextCalled = false;
52
51
  let remainingNextResult;
@@ -73,6 +72,7 @@ function shiftFn(fn, path) {
73
72
  };
74
73
  }
75
74
  function shiftGen(fn, path) {
75
+ path = common.encodePath(path);
76
76
  return async function* shiftedGen(payload, options, next) {
77
77
  let nextCalled = false;
78
78
  let remainingNextStream;
@@ -120,8 +120,9 @@ function resolve(handlers, firstPayload, firstOptions) {
120
120
  throw Error("resolve.no_providers_for " + JSON.stringify(payload));
121
121
  }
122
122
  const { path, handle } = handlers[i];
123
- if (!common.unwrap(payload, path))
123
+ if (!common.unwrap(payload, path)) {
124
124
  return run(i + 1, payload, options);
125
+ }
125
126
  let nextCalled = false;
126
127
  return handle(payload, options, (nextPayload, nextOptions) => {
127
128
  if (nextCalled) {
@@ -141,7 +142,7 @@ class Core {
141
142
  }
142
143
  on(type, path, handle) {
143
144
  this.handlers[type] = this.handlers[type] || [];
144
- this.handlers[type].push({ path, handle });
145
+ this.handlers[type].push({ path: common.encodePath(path), handle });
145
146
  }
146
147
  call(type, payload, options = {}) {
147
148
  log("call", type, payload);
@@ -164,9 +165,14 @@ class Graffy {
164
165
  this.core.on(
165
166
  "read",
166
167
  path,
167
- shiftFn(async function porcelainRead(query, options) {
168
- const decoded = common.decodeQuery(query);
169
- const encoded = common.encodeGraph(await handle(decoded, options));
168
+ shiftFn(async function porcelainRead(query, options, next) {
169
+ const porcelainQuery = common.decodeQuery(query);
170
+ const encoded = common.encodeGraph(
171
+ await handle(porcelainQuery, options, async (nextQuery, nextOpts) => {
172
+ const nextResult = await next(common.encodeQuery(nextQuery), nextOpts);
173
+ return common.decodeGraph(nextResult);
174
+ })
175
+ );
170
176
  const finalized = common.finalize(encoded, query);
171
177
  return finalized;
172
178
  }, path)
@@ -180,7 +186,9 @@ class Graffy {
180
186
  path,
181
187
  shiftGen(function porcelainWatch(query, options) {
182
188
  return stream.makeStream((push, end) => {
183
- const subscription = handle(common.decodeQuery(query), options);
189
+ const subscription = handle(common.decodeQuery(query), options, () => {
190
+ throw Error("porcelain.watch_next_unsupported: " + path);
191
+ });
184
192
  (async function() {
185
193
  try {
186
194
  let firstValue = (await subscription.next()).value;
@@ -203,8 +211,17 @@ class Graffy {
203
211
  this.core.on(
204
212
  "write",
205
213
  path,
206
- shiftFn(async function porcelainWrite(change, options) {
207
- return common.encodeGraph(await handle(common.decodeGraph(change), options));
214
+ shiftFn(async function porcelainWrite(change, options, next) {
215
+ return common.encodeGraph(
216
+ await handle(
217
+ common.decodeGraph(change),
218
+ options,
219
+ async (nextChange, nextOpts) => {
220
+ const nextResult = await next(common.encodeGraph(nextChange), nextOpts);
221
+ return common.decodeGraph(nextResult);
222
+ }
223
+ )
224
+ );
208
225
  }, path)
209
226
  );
210
227
  }
@@ -221,7 +238,7 @@ class Graffy {
221
238
  const rootQuery = common.wrapObject(porcelainQuery, path);
222
239
  const query = common.encodeQuery(rootQuery);
223
240
  const result = await this.core.call("read", query, options || {});
224
- return common.unwrapObject(common.decorate(result, rootQuery), common.decodePath(path));
241
+ return common.unwrapObject(common.decorate(result, rootQuery), path);
225
242
  }
226
243
  watch(...args) {
227
244
  const [path, porcelainQuery, options] = validateCall(...args);
@@ -230,14 +247,14 @@ class Graffy {
230
247
  const stream$1 = this.core.call("watch", query, options || {});
231
248
  return stream.mapStream(
232
249
  stream$1,
233
- (value) => common.unwrapObject(common.decorate(value, rootQuery), common.decodePath(path))
250
+ (value) => common.unwrapObject(common.decorate(value, rootQuery), path)
234
251
  );
235
252
  }
236
253
  async write(...args) {
237
254
  const [path, porcelainChange, options] = validateCall(...args);
238
255
  const change = common.encodeGraph(common.wrapObject(porcelainChange, path));
239
256
  const writtenChange = await this.core.call("write", change, options || {});
240
- return common.unwrapObject(common.decodeGraph(writtenChange), common.decodePath(path));
257
+ return common.unwrapObject(common.decodeGraph(writtenChange), path);
241
258
  }
242
259
  }
243
260
  module.exports = Graffy;
package/index.mjs CHANGED
@@ -1,9 +1,7 @@
1
- import { encodePath, unwrap, remove, wrap, merge, mergeStreams, wrapObject, encodeQuery, unwrapObject, decorate, decodePath, encodeGraph, decodeGraph, decodeQuery, finalize } from "@graffy/common";
1
+ import { isPlainObject, encodePath, unwrap, remove, wrap, merge, mergeStreams, wrapObject, encodeQuery, unwrapObject, decorate, encodeGraph, decodeGraph, decodeQuery, finalize } from "@graffy/common";
2
2
  import { makeStream, mapStream as mapStream$1 } from "@graffy/stream";
3
3
  import debug from "debug";
4
- function isPlainObject(obj) {
5
- return obj && typeof obj === "object" && !Array.isArray(obj);
6
- }
4
+ const splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
7
5
  function validateCall(...args) {
8
6
  if (args.length === 1) {
9
7
  return [[], args[0], {}];
@@ -14,13 +12,13 @@ function validateCall(...args) {
14
12
  }
15
13
  return [[], args[0], args[1]];
16
14
  } else {
17
- return [encodePath(args[0]), args[1], {}];
15
+ return [splitPath(args[0]), args[1], {}];
18
16
  }
19
17
  } else if (args.length === 3) {
20
18
  if (!isPlainObject(args[2])) {
21
19
  throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
22
20
  }
23
- return [encodePath(args[0]), args[1], args[2]];
21
+ return [splitPath(args[0]), args[1], args[2]];
24
22
  }
25
23
  throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
26
24
  }
@@ -34,7 +32,7 @@ function validateOn(...args) {
34
32
  if (typeof args[1] !== "function") {
35
33
  throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
36
34
  }
37
- return [encodePath(args[0]), args[1]];
35
+ return [splitPath(args[0]), args[1]];
38
36
  }
39
37
  throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
40
38
  }
@@ -44,6 +42,7 @@ async function mapStream(stream, fn) {
44
42
  }
45
43
  }
46
44
  function shiftFn(fn, path) {
45
+ path = encodePath(path);
47
46
  return async function shiftedFn(payload, options, next) {
48
47
  let nextCalled = false;
49
48
  let remainingNextResult;
@@ -70,6 +69,7 @@ function shiftFn(fn, path) {
70
69
  };
71
70
  }
72
71
  function shiftGen(fn, path) {
72
+ path = encodePath(path);
73
73
  return async function* shiftedGen(payload, options, next) {
74
74
  let nextCalled = false;
75
75
  let remainingNextStream;
@@ -117,8 +117,9 @@ function resolve(handlers, firstPayload, firstOptions) {
117
117
  throw Error("resolve.no_providers_for " + JSON.stringify(payload));
118
118
  }
119
119
  const { path, handle } = handlers[i];
120
- if (!unwrap(payload, path))
120
+ if (!unwrap(payload, path)) {
121
121
  return run(i + 1, payload, options);
122
+ }
122
123
  let nextCalled = false;
123
124
  return handle(payload, options, (nextPayload, nextOptions) => {
124
125
  if (nextCalled) {
@@ -138,7 +139,7 @@ class Core {
138
139
  }
139
140
  on(type, path, handle) {
140
141
  this.handlers[type] = this.handlers[type] || [];
141
- this.handlers[type].push({ path, handle });
142
+ this.handlers[type].push({ path: encodePath(path), handle });
142
143
  }
143
144
  call(type, payload, options = {}) {
144
145
  log("call", type, payload);
@@ -161,9 +162,14 @@ class Graffy {
161
162
  this.core.on(
162
163
  "read",
163
164
  path,
164
- shiftFn(async function porcelainRead(query, options) {
165
- const decoded = decodeQuery(query);
166
- const encoded = encodeGraph(await handle(decoded, options));
165
+ shiftFn(async function porcelainRead(query, options, next) {
166
+ const porcelainQuery = decodeQuery(query);
167
+ const encoded = encodeGraph(
168
+ await handle(porcelainQuery, options, async (nextQuery, nextOpts) => {
169
+ const nextResult = await next(encodeQuery(nextQuery), nextOpts);
170
+ return decodeGraph(nextResult);
171
+ })
172
+ );
167
173
  const finalized = finalize(encoded, query);
168
174
  return finalized;
169
175
  }, path)
@@ -177,7 +183,9 @@ class Graffy {
177
183
  path,
178
184
  shiftGen(function porcelainWatch(query, options) {
179
185
  return makeStream((push, end) => {
180
- const subscription = handle(decodeQuery(query), options);
186
+ const subscription = handle(decodeQuery(query), options, () => {
187
+ throw Error("porcelain.watch_next_unsupported: " + path);
188
+ });
181
189
  (async function() {
182
190
  try {
183
191
  let firstValue = (await subscription.next()).value;
@@ -200,8 +208,17 @@ class Graffy {
200
208
  this.core.on(
201
209
  "write",
202
210
  path,
203
- shiftFn(async function porcelainWrite(change, options) {
204
- return encodeGraph(await handle(decodeGraph(change), options));
211
+ shiftFn(async function porcelainWrite(change, options, next) {
212
+ return encodeGraph(
213
+ await handle(
214
+ decodeGraph(change),
215
+ options,
216
+ async (nextChange, nextOpts) => {
217
+ const nextResult = await next(encodeGraph(nextChange), nextOpts);
218
+ return decodeGraph(nextResult);
219
+ }
220
+ )
221
+ );
205
222
  }, path)
206
223
  );
207
224
  }
@@ -218,7 +235,7 @@ class Graffy {
218
235
  const rootQuery = wrapObject(porcelainQuery, path);
219
236
  const query = encodeQuery(rootQuery);
220
237
  const result = await this.core.call("read", query, options || {});
221
- return unwrapObject(decorate(result, rootQuery), decodePath(path));
238
+ return unwrapObject(decorate(result, rootQuery), path);
222
239
  }
223
240
  watch(...args) {
224
241
  const [path, porcelainQuery, options] = validateCall(...args);
@@ -227,14 +244,14 @@ class Graffy {
227
244
  const stream = this.core.call("watch", query, options || {});
228
245
  return mapStream$1(
229
246
  stream,
230
- (value) => unwrapObject(decorate(value, rootQuery), decodePath(path))
247
+ (value) => unwrapObject(decorate(value, rootQuery), path)
231
248
  );
232
249
  }
233
250
  async write(...args) {
234
251
  const [path, porcelainChange, options] = validateCall(...args);
235
252
  const change = encodeGraph(wrapObject(porcelainChange, path));
236
253
  const writtenChange = await this.core.call("write", change, options || {});
237
- return unwrapObject(decodeGraph(writtenChange), decodePath(path));
254
+ return unwrapObject(decodeGraph(writtenChange), path);
238
255
  }
239
256
  }
240
257
  export {
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.15.25",
5
+ "version": "0.16.0-alpha.10",
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.15.25",
20
- "@graffy/stream": "0.15.25",
19
+ "@graffy/common": "0.16.0-alpha.10",
20
+ "@graffy/stream": "0.16.0-alpha.10",
21
21
  "debug": "^4.3.3"
22
22
  }
23
23
  }