@graffy/core 0.16.19 → 0.16.20-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
@@ -4,8 +4,7 @@ const stream = require("@graffy/stream");
4
4
  const debug = require("debug");
5
5
  const log = debug("graffy:core");
6
6
  function resolve(handlers, firstPayload, firstOptions) {
7
- if (!(handlers == null ? void 0 : handlers.length))
8
- throw Error("resolve.no_provider");
7
+ if (!(handlers == null ? void 0 : handlers.length)) throw Error("resolve.no_provider");
9
8
  function run(i, payload, options) {
10
9
  if (i >= handlers.length) {
11
10
  throw Error(`resolve.no_providers_for ${JSON.stringify(payload)}`);
@@ -20,8 +19,7 @@ function resolve(handlers, firstPayload, firstOptions) {
20
19
  throw Error(`resolve.duplicate_next_call: ${handlers[i].name}`);
21
20
  }
22
21
  nextCalled = true;
23
- if (typeof nextPayload === "undefined" || !nextPayload.length)
24
- return;
22
+ if (typeof nextPayload === "undefined" || !nextPayload.length) return;
25
23
  return run(i + 1, nextPayload, nextOptions || options);
26
24
  });
27
25
  }
@@ -49,11 +47,9 @@ const unchanged = Symbol("Payload or result unchanged by handler");
49
47
  const decodeCache = /* @__PURE__ */ new WeakMap();
50
48
  function memoizeDecode(origDecode) {
51
49
  return (payload) => {
52
- if (decodeCache.has(payload))
53
- return decodeCache.get(payload);
50
+ if (decodeCache.has(payload)) return decodeCache.get(payload);
54
51
  const decoded = origDecode(payload);
55
- if (payload.type === "object" && payload)
56
- decodeCache.set(payload, decoded);
52
+ if (payload.type === "object" && payload) decodeCache.set(payload, decoded);
57
53
  return decoded;
58
54
  };
59
55
  }
@@ -78,8 +74,7 @@ function wrapProvider(fn, decodedPath, isRead) {
78
74
  nextPayload = encodePayload(
79
75
  common.wrapObject(porcelainNextPayload, decodedPath)
80
76
  );
81
- if (remainingPayload.length)
82
- common.merge(nextPayload, remainingPayload);
77
+ if (remainingPayload.length) common.merge(nextPayload, remainingPayload);
83
78
  }
84
79
  nextResult = await next(nextPayload, nextOptions);
85
80
  remainingNextResult = common.remove(nextResult, path) || [];
@@ -116,8 +111,7 @@ function shiftGen(fn, path) {
116
111
  const shiftedNext = async function* shiftedNextFn(unwrappedNextPayload, nextOptions) {
117
112
  nextCalled = true;
118
113
  const nextPayload = common.wrap(unwrappedNextPayload, path);
119
- if (remainingPayload.length)
120
- common.merge(nextPayload, remainingPayload);
114
+ if (remainingPayload.length) common.merge(nextPayload, remainingPayload);
121
115
  let pushRemaining;
122
116
  remainingNextStream = stream.makeStream((push) => {
123
117
  pushRemaining = push;
@@ -125,10 +119,8 @@ function shiftGen(fn, path) {
125
119
  for await (const value of next(nextPayload, nextOptions)) {
126
120
  const unwrappedValue = common.unwrap(value, path);
127
121
  const remainingValue = common.remove(value, path);
128
- if (remainingValue)
129
- pushRemaining(remainingValue);
130
- if (unwrappedValue)
131
- yield unwrappedValue;
122
+ if (remainingValue) pushRemaining(remainingValue);
123
+ if (unwrappedValue) yield unwrappedValue;
132
124
  }
133
125
  };
134
126
  const unwrappedStream = fn(unwrappedPayload, options, shiftedNext);
package/index.mjs CHANGED
@@ -3,8 +3,7 @@ import { makeStream, mapStream as mapStream$1 } from "@graffy/stream";
3
3
  import debug from "debug";
4
4
  const log = debug("graffy:core");
5
5
  function resolve(handlers, firstPayload, firstOptions) {
6
- if (!(handlers == null ? void 0 : handlers.length))
7
- throw Error("resolve.no_provider");
6
+ if (!(handlers == null ? void 0 : handlers.length)) throw Error("resolve.no_provider");
8
7
  function run(i, payload, options) {
9
8
  if (i >= handlers.length) {
10
9
  throw Error(`resolve.no_providers_for ${JSON.stringify(payload)}`);
@@ -19,8 +18,7 @@ function resolve(handlers, firstPayload, firstOptions) {
19
18
  throw Error(`resolve.duplicate_next_call: ${handlers[i].name}`);
20
19
  }
21
20
  nextCalled = true;
22
- if (typeof nextPayload === "undefined" || !nextPayload.length)
23
- return;
21
+ if (typeof nextPayload === "undefined" || !nextPayload.length) return;
24
22
  return run(i + 1, nextPayload, nextOptions || options);
25
23
  });
26
24
  }
@@ -48,11 +46,9 @@ const unchanged = Symbol("Payload or result unchanged by handler");
48
46
  const decodeCache = /* @__PURE__ */ new WeakMap();
49
47
  function memoizeDecode(origDecode) {
50
48
  return (payload) => {
51
- if (decodeCache.has(payload))
52
- return decodeCache.get(payload);
49
+ if (decodeCache.has(payload)) return decodeCache.get(payload);
53
50
  const decoded = origDecode(payload);
54
- if (payload.type === "object" && payload)
55
- decodeCache.set(payload, decoded);
51
+ if (payload.type === "object" && payload) decodeCache.set(payload, decoded);
56
52
  return decoded;
57
53
  };
58
54
  }
@@ -77,8 +73,7 @@ function wrapProvider(fn, decodedPath, isRead) {
77
73
  nextPayload = encodePayload(
78
74
  wrapObject(porcelainNextPayload, decodedPath)
79
75
  );
80
- if (remainingPayload.length)
81
- merge(nextPayload, remainingPayload);
76
+ if (remainingPayload.length) merge(nextPayload, remainingPayload);
82
77
  }
83
78
  nextResult = await next(nextPayload, nextOptions);
84
79
  remainingNextResult = remove(nextResult, path) || [];
@@ -115,8 +110,7 @@ function shiftGen(fn, path) {
115
110
  const shiftedNext = async function* shiftedNextFn(unwrappedNextPayload, nextOptions) {
116
111
  nextCalled = true;
117
112
  const nextPayload = wrap(unwrappedNextPayload, path);
118
- if (remainingPayload.length)
119
- merge(nextPayload, remainingPayload);
113
+ if (remainingPayload.length) merge(nextPayload, remainingPayload);
120
114
  let pushRemaining;
121
115
  remainingNextStream = makeStream((push) => {
122
116
  pushRemaining = push;
@@ -124,10 +118,8 @@ function shiftGen(fn, path) {
124
118
  for await (const value of next(nextPayload, nextOptions)) {
125
119
  const unwrappedValue = unwrap(value, path);
126
120
  const remainingValue = remove(value, path);
127
- if (remainingValue)
128
- pushRemaining(remainingValue);
129
- if (unwrappedValue)
130
- yield unwrappedValue;
121
+ if (remainingValue) pushRemaining(remainingValue);
122
+ if (unwrappedValue) yield unwrappedValue;
131
123
  }
132
124
  };
133
125
  const unwrappedStream = fn(unwrappedPayload, options, shiftedNext);
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.19",
5
+ "version": "0.16.20-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.19",
20
- "@graffy/stream": "0.16.19",
21
- "debug": "^4.3.3"
19
+ "@graffy/common": "0.16.20-alpha.2",
20
+ "@graffy/stream": "0.16.20-alpha.2",
21
+ "debug": "^4.3.7"
22
22
  }
23
23
  }
package/types/index.d.ts CHANGED
@@ -1,2 +1,75 @@
1
- export default Graffy;
2
- import Graffy from './Graffy.js';
1
+ export type JsonObject = Record<string, unknown>;
2
+
3
+ type Key = unknown;
4
+
5
+ // biome-ignore lint/suspicious/noExplicitAny: "Unknown" prevents assigning a concrete type to this parameter.
6
+ export type GraffyCollection<CollectionSchema extends Record<string, any>> = {
7
+ [name: string]: CollectionSchema;
8
+ } & { __brand: 'GraffyCollection' };
9
+
10
+ // The parameter should be the Schema of the entire store. Use the "GraffyCollection"
11
+ // type to define the schema of a collection.
12
+ export default class Graffy<S> {
13
+ read<P extends PathOf<S>, Q extends Project<Descend<S, P>>>(
14
+ path: P,
15
+ projection: Q,
16
+ options?: GraffyReadOptions,
17
+ ): Promise<ReadResult<Descend<S, P>, Q>>;
18
+
19
+ read<Q extends Project<S>>(
20
+ projection: Q,
21
+ options?: GraffyReadOptions,
22
+ ): Promise<ReadResult<S, Q>>;
23
+
24
+ // biome-ignore lint/suspicious/noExplicitAny: TODO: add typings
25
+ on: (...any) => any;
26
+ // biome-ignore lint/suspicious/noExplicitAny: TODO: add typings
27
+ call: (...any) => any;
28
+ // biome-ignore lint/suspicious/noExplicitAny: TODO: add typings
29
+ use: (...any) => any;
30
+ // biome-ignore lint/suspicious/noExplicitAny: TODO: add typings
31
+ onRead: (...any) => any;
32
+ // biome-ignore lint/suspicious/noExplicitAny: TODO: add typings
33
+ onWrite: (...any) => any;
34
+ // biome-ignore lint/suspicious/noExplicitAny: TODO: add typings
35
+ onWatch: (...any) => any;
36
+ // biome-ignore lint/suspicious/noExplicitAny: TODO: add typings
37
+ write: (...any) => any;
38
+ // biome-ignore lint/suspicious/noExplicitAny: TODO: add typings
39
+ watch: (...any) => any;
40
+ }
41
+
42
+ export type PathOf<S> = S extends GraffyCollection<BuildObject>
43
+ ? string | Key
44
+ : S extends JsonObject
45
+ ? keyof S | [keyof S, ...PathOf<S[keyof S]>[]]
46
+ : never;
47
+
48
+ export type Descend<S, P> = S extends GraffyCollection<BuildObject>
49
+ ? S[string]
50
+ : P extends keyof S
51
+ ? S[P]
52
+ : P extends [keyof S]
53
+ ? S[P[0]]
54
+ : P extends [keyof S, ...infer R]
55
+ ? Descend<S[P[0]], R>
56
+ : S;
57
+
58
+ export type Project<S> = S extends GraffyCollection<BuildObject>
59
+ ?
60
+ | (Project<S[string]> & { $key: Key })
61
+ | (Project<S[string]> & { $key: Key })[]
62
+ : S extends JsonObject
63
+ ? Partial<{ [K in keyof S]: Project<S[K]> }>
64
+ : boolean;
65
+
66
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
67
+ type ResultArray<R> = R[] & { $page: any; $next: any; $prev: any };
68
+
69
+ type ReadResult<S, Q> = S extends GraffyCollection<BuildObject>
70
+ ? (S[string] & { $key: Key }) | ResultArray<S[string] & { $key: Key }>
71
+ : S extends JsonObject
72
+ ? { [K in keyof S & keyof Q]: ReadResult<S[K], Q[K]> }
73
+ : S;
74
+
75
+ type GraffyReadOptions = JsonObject;
package/types/Core.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export default class Core {
2
- handlers: {};
3
- on(type: any, path: any, handle: any): void;
4
- call(type: any, payload: any, options?: {}): any;
5
- }
package/types/Graffy.d.ts DELETED
@@ -1,33 +0,0 @@
1
- export { unchanged } from "./shift.js";
2
- declare class Graffy {
3
- constructor(path?: any[], core?: Core);
4
- core: Core;
5
- path: any[];
6
- on(type: any, ...args: any[]): void;
7
- onRead(...args: any[]): void;
8
- onWatch(...args: any[]): void;
9
- onWrite(...args: any[]): void;
10
- use(...args: any[]): void;
11
- call(type: any, payload: any, options?: {}): any;
12
- read(...args: any[]): Promise<any>;
13
- watch(...args: any[]): {
14
- debugId: any;
15
- next: () => any;
16
- return(value: any): Promise<{
17
- value: any;
18
- done: boolean;
19
- }>;
20
- throw(error: any): Promise<{
21
- value: any;
22
- done: boolean;
23
- }>;
24
- [Symbol.asyncIterator](): any;
25
- };
26
- write(...args: any[]): Promise<any>;
27
- }
28
- declare namespace Graffy {
29
- export { unchanged };
30
- }
31
- export default Graffy;
32
- import Core from './Core.js';
33
- import { unchanged } from './shift.js';
package/types/shift.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export function wrapProvider(fn: any, decodedPath: any, isRead: any): (payload: any, options: any, next: any) => Promise<any>;
2
- export function shiftGen(fn: any, path: any): (payload: any, options: any, next: any) => AsyncGenerator<any, void, any>;
3
- export const unchanged: unique symbol;
@@ -1,2 +0,0 @@
1
- export function validateCall(...args: any[]): any[];
2
- export function validateOn(...args: any[]): any[];