@graffy/core 0.16.3 → 0.16.4-alpha.2

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,24 +42,33 @@ async function mapStream(stream2, fn) {
42
42
  fn(value);
43
43
  }
44
44
  }
45
- function shiftFn(fn, path) {
46
- path = common.encodePath(path);
47
- return async function shiftedFn(payload, options, next) {
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) {
48
50
  let nextCalled = false;
49
51
  let remainingNextResult;
50
- const unwrappedPayload = common.unwrap(payload, path);
52
+ const porcelainPayload = common.unwrapObject(decodePayload(payload), decodedPath);
51
53
  const remainingPayload = common.remove(payload, path) || [];
52
- async function shiftedNext(unwrappedNextPayload) {
54
+ async function shiftedNext(porcelainNextPayload) {
53
55
  nextCalled = true;
54
- const nextPayload = common.wrap(unwrappedNextPayload, path);
56
+ const nextPayload = encodePayload(
57
+ common.wrapObject(porcelainNextPayload, decodedPath)
58
+ );
55
59
  if (remainingPayload.length)
56
60
  common.merge(nextPayload, remainingPayload);
57
61
  const nextResult = await next(nextPayload);
58
62
  remainingNextResult = common.remove(nextResult, path) || [];
59
- return common.unwrap(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);
60
71
  }
61
- const rawResult = await fn(unwrappedPayload, options, shiftedNext);
62
- const result = common.wrap(rawResult, path);
63
72
  if (!nextCalled && remainingPayload.length) {
64
73
  remainingNextResult = await next(remainingPayload);
65
74
  }
@@ -160,32 +169,7 @@ class Graffy {
160
169
  onRead(...args) {
161
170
  const [pathArg, handle] = validateOn(...args);
162
171
  const path = this.path.concat(pathArg);
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
- );
172
+ this.core.on("read", path, wrapProvider(handle, path, true));
189
173
  }
190
174
  onWatch(...args) {
191
175
  const [pathArg, handle] = validateOn(...args);
@@ -217,22 +201,7 @@ class Graffy {
217
201
  onWrite(...args) {
218
202
  const [pathArg, handle] = validateOn(...args);
219
203
  const path = this.path.concat(pathArg);
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
- );
204
+ this.core.on("write", path, wrapProvider(handle, path, false));
236
205
  }
237
206
  use(...args) {
238
207
  const [pathArg, provider] = validateOn(...args);
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isPlainObject, encodePath, unwrap, remove, wrap, merge, mergeStreams, wrapObject, encodeQuery, unwrapObject, decorate, encodeGraph, decodeGraph, decodeQuery, finalize } from "@graffy/common";
1
+ import { isPlainObject, encodePath, unwrapObject, remove, encodeGraph, wrapObject, wrap, unwrap, finalize, merge, mergeStreams, decodeQuery, decodeGraph, encodeQuery, decorate } 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,24 +41,33 @@ async function mapStream(stream, fn) {
41
41
  fn(value);
42
42
  }
43
43
  }
44
- function shiftFn(fn, path) {
45
- path = encodePath(path);
46
- return async function shiftedFn(payload, options, next) {
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) {
47
49
  let nextCalled = false;
48
50
  let remainingNextResult;
49
- const unwrappedPayload = unwrap(payload, path);
51
+ const porcelainPayload = unwrapObject(decodePayload(payload), decodedPath);
50
52
  const remainingPayload = remove(payload, path) || [];
51
- async function shiftedNext(unwrappedNextPayload) {
53
+ async function shiftedNext(porcelainNextPayload) {
52
54
  nextCalled = true;
53
- const nextPayload = wrap(unwrappedNextPayload, path);
55
+ const nextPayload = encodePayload(
56
+ wrapObject(porcelainNextPayload, decodedPath)
57
+ );
54
58
  if (remainingPayload.length)
55
59
  merge(nextPayload, remainingPayload);
56
60
  const nextResult = await next(nextPayload);
57
61
  remainingNextResult = remove(nextResult, path) || [];
58
- return unwrap(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);
59
70
  }
60
- const rawResult = await fn(unwrappedPayload, options, shiftedNext);
61
- const result = wrap(rawResult, path);
62
71
  if (!nextCalled && remainingPayload.length) {
63
72
  remainingNextResult = await next(remainingPayload);
64
73
  }
@@ -159,32 +168,7 @@ class Graffy {
159
168
  onRead(...args) {
160
169
  const [pathArg, handle] = validateOn(...args);
161
170
  const path = this.path.concat(pathArg);
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
- );
171
+ this.core.on("read", path, wrapProvider(handle, path, true));
188
172
  }
189
173
  onWatch(...args) {
190
174
  const [pathArg, handle] = validateOn(...args);
@@ -216,22 +200,7 @@ class Graffy {
216
200
  onWrite(...args) {
217
201
  const [pathArg, handle] = validateOn(...args);
218
202
  const path = this.path.concat(pathArg);
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
- );
203
+ this.core.on("write", path, wrapProvider(handle, path, false));
235
204
  }
236
205
  use(...args) {
237
206
  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",
5
+ "version": "0.16.4-alpha.2",
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",
20
- "@graffy/stream": "0.16.3",
19
+ "@graffy/common": "0.16.4-alpha.2",
20
+ "@graffy/stream": "0.16.4-alpha.2",
21
21
  "debug": "^4.3.3"
22
22
  }
23
23
  }
package/types/shift.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export function shiftFn(fn: any, path: any): (payload: any, options: any, next: any) => Promise<any>;
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>;