@graffy/core 0.15.25 → 0.16.0-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.
Files changed (3) hide show
  1. package/index.cjs +15 -14
  2. package/index.mjs +13 -12
  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);
@@ -221,7 +222,7 @@ class Graffy {
221
222
  const rootQuery = common.wrapObject(porcelainQuery, path);
222
223
  const query = common.encodeQuery(rootQuery);
223
224
  const result = await this.core.call("read", query, options || {});
224
- return common.unwrapObject(common.decorate(result, rootQuery), common.decodePath(path));
225
+ return common.unwrapObject(common.decorate(result, rootQuery), path);
225
226
  }
226
227
  watch(...args) {
227
228
  const [path, porcelainQuery, options] = validateCall(...args);
@@ -230,14 +231,14 @@ class Graffy {
230
231
  const stream$1 = this.core.call("watch", query, options || {});
231
232
  return stream.mapStream(
232
233
  stream$1,
233
- (value) => common.unwrapObject(common.decorate(value, rootQuery), common.decodePath(path))
234
+ (value) => common.unwrapObject(common.decorate(value, rootQuery), path)
234
235
  );
235
236
  }
236
237
  async write(...args) {
237
238
  const [path, porcelainChange, options] = validateCall(...args);
238
239
  const change = common.encodeGraph(common.wrapObject(porcelainChange, path));
239
240
  const writtenChange = await this.core.call("write", change, options || {});
240
- return common.unwrapObject(common.decodeGraph(writtenChange), common.decodePath(path));
241
+ return common.unwrapObject(common.decodeGraph(writtenChange), path);
241
242
  }
242
243
  }
243
244
  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);
@@ -218,7 +219,7 @@ class Graffy {
218
219
  const rootQuery = wrapObject(porcelainQuery, path);
219
220
  const query = encodeQuery(rootQuery);
220
221
  const result = await this.core.call("read", query, options || {});
221
- return unwrapObject(decorate(result, rootQuery), decodePath(path));
222
+ return unwrapObject(decorate(result, rootQuery), path);
222
223
  }
223
224
  watch(...args) {
224
225
  const [path, porcelainQuery, options] = validateCall(...args);
@@ -227,14 +228,14 @@ class Graffy {
227
228
  const stream = this.core.call("watch", query, options || {});
228
229
  return mapStream$1(
229
230
  stream,
230
- (value) => unwrapObject(decorate(value, rootQuery), decodePath(path))
231
+ (value) => unwrapObject(decorate(value, rootQuery), path)
231
232
  );
232
233
  }
233
234
  async write(...args) {
234
235
  const [path, porcelainChange, options] = validateCall(...args);
235
236
  const change = encodeGraph(wrapObject(porcelainChange, path));
236
237
  const writtenChange = await this.core.call("write", change, options || {});
237
- return unwrapObject(decodeGraph(writtenChange), decodePath(path));
238
+ return unwrapObject(decodeGraph(writtenChange), path);
238
239
  }
239
240
  }
240
241
  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.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.15.25",
20
- "@graffy/stream": "0.15.25",
19
+ "@graffy/common": "0.16.0-alpha.1",
20
+ "@graffy/stream": "0.16.0-alpha.1",
21
21
  "debug": "^4.3.3"
22
22
  }
23
23
  }