@graffy/core 0.16.3-alpha.4 → 0.16.3-alpha.6

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
@@ -42,33 +42,24 @@ async function mapStream(stream2, fn) {
42
42
  fn(value);
43
43
  }
44
44
  }
45
- function wrapProvider(fn, decodedPath, isRead) {
46
- const decodePayload = isRead ? common.decodeQuery : common.decodeGraph;
47
- const encodePayload = isRead ? common.encodeQuery : common.encodeGraph;
48
- const path = common.encodePath(decodedPath);
49
- return async function wrappedProvider(payload, options, next) {
45
+ function shiftFn(fn, path) {
46
+ path = common.encodePath(path);
47
+ return async function shiftedFn(payload, options, next) {
50
48
  let nextCalled = false;
51
49
  let remainingNextResult;
52
- const porcelainPayload = common.unwrapObject(decodePayload(payload), decodedPath);
50
+ const unwrappedPayload = common.unwrap(payload, path);
53
51
  const remainingPayload = common.remove(payload, path) || [];
54
- async function shiftedNext(porcelainNextPayload) {
52
+ async function shiftedNext(unwrappedNextPayload) {
55
53
  nextCalled = true;
56
- const nextPayload = encodePayload(
57
- common.wrapObject(porcelainNextPayload, decodedPath)
58
- );
54
+ const nextPayload = common.wrap(unwrappedNextPayload, path);
59
55
  if (remainingPayload.length)
60
56
  common.merge(nextPayload, remainingPayload);
61
57
  const nextResult = await next(nextPayload);
62
58
  remainingNextResult = common.remove(nextResult, path) || [];
63
- return common.unwrapObject(common.decodeGraph(nextResult), decodedPath);
64
- }
65
- 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);
59
+ return common.unwrap(nextResult, path);
71
60
  }
61
+ const rawResult = await fn(unwrappedPayload, options, shiftedNext);
62
+ const result = common.wrap(rawResult, path);
72
63
  if (!nextCalled && remainingPayload.length) {
73
64
  remainingNextResult = await next(remainingPayload);
74
65
  }
@@ -169,7 +160,32 @@ class Graffy {
169
160
  onRead(...args) {
170
161
  const [pathArg, handle] = validateOn(...args);
171
162
  const path = this.path.concat(pathArg);
172
- this.core.on("read", path, wrapProvider(handle, path, true));
163
+ this.core.on(
164
+ "read",
165
+ path,
166
+ shiftFn(async function porcelainRead(query, options, next) {
167
+ const porcelainQuery = common.decodeQuery(query);
168
+ let nextCalled = false;
169
+ const encoded = common.encodeGraph(
170
+ await handle(porcelainQuery, options, async (nextQuery, nextOpts) => {
171
+ nextCalled = true;
172
+ const nextResult = await next(common.encodeQuery(nextQuery), nextOpts);
173
+ return common.decodeGraph(nextResult);
174
+ })
175
+ );
176
+ let final;
177
+ if (!nextCalled) {
178
+ const encodedPath = common.encodePath(path);
179
+ final = common.unwrap(
180
+ common.finalize(common.wrap(encoded, encodedPath), common.wrap(query, encodedPath)),
181
+ encodedPath
182
+ );
183
+ } else {
184
+ final = encoded;
185
+ }
186
+ return final;
187
+ }, path)
188
+ );
173
189
  }
174
190
  onWatch(...args) {
175
191
  const [pathArg, handle] = validateOn(...args);
@@ -201,7 +217,22 @@ class Graffy {
201
217
  onWrite(...args) {
202
218
  const [pathArg, handle] = validateOn(...args);
203
219
  const path = this.path.concat(pathArg);
204
- this.core.on("write", path, wrapProvider(handle, path, false));
220
+ this.core.on(
221
+ "write",
222
+ path,
223
+ shiftFn(async function porcelainWrite(change, options, next) {
224
+ return common.encodeGraph(
225
+ await handle(
226
+ common.decodeGraph(change),
227
+ options,
228
+ async (nextChange, nextOpts) => {
229
+ const nextResult = await next(common.encodeGraph(nextChange), nextOpts);
230
+ return common.decodeGraph(nextResult);
231
+ }
232
+ )
233
+ );
234
+ }, path)
235
+ );
205
236
  }
206
237
  use(...args) {
207
238
  const [pathArg, provider] = validateOn(...args);
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isPlainObject, encodePath, unwrapObject, remove, encodeGraph, wrapObject, wrap, unwrap, finalize, merge, mergeStreams, decodeQuery, decodeGraph, encodeQuery, decorate } 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
4
  const splitPath = (path) => Array.isArray(path) ? path : path === "" ? [] : String(path).split(".");
@@ -41,33 +41,24 @@ async function mapStream(stream, fn) {
41
41
  fn(value);
42
42
  }
43
43
  }
44
- function wrapProvider(fn, decodedPath, isRead) {
45
- const decodePayload = isRead ? decodeQuery : decodeGraph;
46
- const encodePayload = isRead ? encodeQuery : encodeGraph;
47
- const path = encodePath(decodedPath);
48
- return async function wrappedProvider(payload, options, next) {
44
+ function shiftFn(fn, path) {
45
+ path = encodePath(path);
46
+ return async function shiftedFn(payload, options, next) {
49
47
  let nextCalled = false;
50
48
  let remainingNextResult;
51
- const porcelainPayload = unwrapObject(decodePayload(payload), decodedPath);
49
+ const unwrappedPayload = unwrap(payload, path);
52
50
  const remainingPayload = remove(payload, path) || [];
53
- async function shiftedNext(porcelainNextPayload) {
51
+ async function shiftedNext(unwrappedNextPayload) {
54
52
  nextCalled = true;
55
- const nextPayload = encodePayload(
56
- wrapObject(porcelainNextPayload, decodedPath)
57
- );
53
+ const nextPayload = wrap(unwrappedNextPayload, path);
58
54
  if (remainingPayload.length)
59
55
  merge(nextPayload, remainingPayload);
60
56
  const nextResult = await next(nextPayload);
61
57
  remainingNextResult = remove(nextResult, path) || [];
62
- return unwrapObject(decodeGraph(nextResult), decodedPath);
63
- }
64
- 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);
58
+ return unwrap(nextResult, path);
70
59
  }
60
+ const rawResult = await fn(unwrappedPayload, options, shiftedNext);
61
+ const result = wrap(rawResult, path);
71
62
  if (!nextCalled && remainingPayload.length) {
72
63
  remainingNextResult = await next(remainingPayload);
73
64
  }
@@ -168,7 +159,32 @@ class Graffy {
168
159
  onRead(...args) {
169
160
  const [pathArg, handle] = validateOn(...args);
170
161
  const path = this.path.concat(pathArg);
171
- this.core.on("read", path, wrapProvider(handle, path, true));
162
+ this.core.on(
163
+ "read",
164
+ path,
165
+ shiftFn(async function porcelainRead(query, options, next) {
166
+ const porcelainQuery = decodeQuery(query);
167
+ let nextCalled = false;
168
+ const encoded = encodeGraph(
169
+ await handle(porcelainQuery, options, async (nextQuery, nextOpts) => {
170
+ nextCalled = true;
171
+ const nextResult = await next(encodeQuery(nextQuery), nextOpts);
172
+ return decodeGraph(nextResult);
173
+ })
174
+ );
175
+ let final;
176
+ if (!nextCalled) {
177
+ const encodedPath = encodePath(path);
178
+ final = unwrap(
179
+ finalize(wrap(encoded, encodedPath), wrap(query, encodedPath)),
180
+ encodedPath
181
+ );
182
+ } else {
183
+ final = encoded;
184
+ }
185
+ return final;
186
+ }, path)
187
+ );
172
188
  }
173
189
  onWatch(...args) {
174
190
  const [pathArg, handle] = validateOn(...args);
@@ -200,7 +216,22 @@ class Graffy {
200
216
  onWrite(...args) {
201
217
  const [pathArg, handle] = validateOn(...args);
202
218
  const path = this.path.concat(pathArg);
203
- this.core.on("write", path, wrapProvider(handle, path, false));
219
+ this.core.on(
220
+ "write",
221
+ path,
222
+ shiftFn(async function porcelainWrite(change, options, next) {
223
+ return encodeGraph(
224
+ await handle(
225
+ decodeGraph(change),
226
+ options,
227
+ async (nextChange, nextOpts) => {
228
+ const nextResult = await next(encodeGraph(nextChange), nextOpts);
229
+ return decodeGraph(nextResult);
230
+ }
231
+ )
232
+ );
233
+ }, path)
234
+ );
204
235
  }
205
236
  use(...args) {
206
237
  const [pathArg, provider] = validateOn(...args);
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.3-alpha.4",
5
+ "version": "0.16.3-alpha.6",
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.3-alpha.4",
20
- "@graffy/stream": "0.16.3-alpha.4",
19
+ "@graffy/common": "0.16.3-alpha.6",
20
+ "@graffy/stream": "0.16.3-alpha.6",
21
21
  "debug": "^4.3.3"
22
22
  }
23
23
  }
package/types/shift.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export function wrapProvider(fn: any, decodedPath: any, isRead: any): (payload: any, options: any, next: any) => Promise<any>;
1
+ export function shiftFn(fn: any, path: 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>;