@graffy/core 0.16.19 → 0.16.20-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.
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.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.16.19",
20
- "@graffy/stream": "0.16.19",
21
- "debug": "^4.3.3"
19
+ "@graffy/common": "0.16.20-alpha.10",
20
+ "@graffy/stream": "0.16.20-alpha.10",
21
+ "debug": "^4.3.7"
22
22
  }
23
23
  }
package/types/index.d.ts CHANGED
@@ -1,2 +1,134 @@
1
- export default Graffy;
2
- import Graffy from './Graffy.js';
1
+ export type AnyLeaf = string | number | boolean | null;
2
+
3
+ // biome-ignore lint/suspicious/noExplicitAny: This is used to match concrete types in "extends" expressions.
4
+ export type AnyObject = Record<string, any>;
5
+
6
+ // biome-ignore lint/suspicious/noExplicitAny: Function for which types are not yet defined.
7
+ type AnyFunction = (...args: any[]) => any;
8
+
9
+ // biome-ignore lint/suspicious/noExplicitAny: Keys can be anything.
10
+ export type Key = any;
11
+ export type RangeKey =
12
+ | { $all: boolean }
13
+ | { $first: number }
14
+ | { $last: number };
15
+
16
+ // biome-ignore lint/suspicious/noExplicitAny: Any value is for results.
17
+ type AnyValue = any;
18
+
19
+ type AnyProjection =
20
+ | boolean
21
+ | { $key: Key }
22
+ | { [key: string]: AnyProjection }
23
+ | AnyProjection[];
24
+
25
+ export type GraffyCollection<CollectionSchema extends AnyObject> = {
26
+ [name: string]: CollectionSchema;
27
+ } & { __brand: 'GraffyCollection' };
28
+
29
+ // The parameter should be the Schema of the entire store. Use the "GraffyCollection"
30
+ // type to define the schema of a collection.
31
+ export default class Graffy<S> {
32
+ read<P extends PathOf<S>, Q extends Project<Descend<S, P>>>(
33
+ path: P,
34
+ projection: Q,
35
+ options?: GraffyReadOptions,
36
+ ): Promise<ReadResult<Descend<S, P>, Q>>;
37
+
38
+ read<Q extends Project<S>>(
39
+ projection: Q,
40
+ options?: GraffyReadOptions,
41
+ ): Promise<ReadResult<S, Q>>;
42
+
43
+ // Generic mode, when the path is not known at compile time, but projection is.
44
+ read<Q extends AnyProjection>(
45
+ path: string | Key[],
46
+ projection: Q,
47
+ options?: GraffyReadOptions,
48
+ ): Promise<BlindReadResult<Q>>;
49
+
50
+ // Consider also:
51
+ // 1. Read when path is known at compile time but projection is not?
52
+ // 2. Read when neither path nor projection is known at compile time?
53
+
54
+ on: AnyFunction;
55
+ call: AnyFunction;
56
+ use: AnyFunction;
57
+ onRead: AnyFunction;
58
+ onWrite: AnyFunction;
59
+ onWatch: AnyFunction;
60
+ write: AnyFunction;
61
+ watch: AnyFunction;
62
+ }
63
+
64
+ // TODO: To avoid the "too deep" error, these need to be tail-recursive.
65
+ // This may be possible with an "accummulator" type parameter.
66
+ // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#tail-recursion-elimination-on-conditional-types
67
+
68
+ export type PathOf<S> = S extends GraffyCollection<AnyObject>
69
+ ? Key
70
+ : S extends AnyObject
71
+ ? keyof S | [keyof S, ...PathOf<S[keyof S]>[]]
72
+ : never;
73
+
74
+ type Get<S, K> = S extends GraffyCollection<AnyObject>
75
+ ? S[string]
76
+ : K extends keyof S
77
+ ? S[K]
78
+ : never;
79
+
80
+ export type Descend<S, P> = P extends [Key]
81
+ ? Get<S, P[0]>
82
+ : P extends [Key, ...infer R]
83
+ ? Descend<Get<S, P[0]>, R>
84
+ : Get<S, P>;
85
+
86
+ export type Project<S> = S extends AnyLeaf
87
+ ? boolean
88
+ : S extends GraffyCollection<AnyObject>
89
+ ?
90
+ | { [key: string]: Project<S[string]> } // Object form
91
+ | (Project<S[string]> & { $key: Key }) // Single $key
92
+ | (Project<S[string]> & { $key: Key })[] // Array $key
93
+ : S extends AnyObject
94
+ ? 'string' extends keyof S // No named properties?
95
+ ? AnyProjection
96
+ : Partial<{ [K in keyof S]: Project<S[K]> }> | boolean
97
+ : never;
98
+
99
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
100
+ type ResultArray<R> = R[] & { $page: any; $next: any; $prev: any };
101
+
102
+ type ReadResult<S, Q> = S extends GraffyCollection<AnyObject>
103
+ ? Q extends Array<infer QItem>
104
+ ? ResultArray<PlainReadResult<S[string], QItem> & { $key: Key }> // Array $key
105
+ : Q extends { $key: Key }
106
+ ? Q extends { $key: string }
107
+ ? { [key in Q['$key']]: PlainReadResult<S, Q> }
108
+ : ResultArray<PlainReadResult<S[string], Q> & { $key: Key }> // Single $key
109
+ : { [K in keyof Q]: PlainReadResult<S[string], Q[K]> } // Object form
110
+ : PlainReadResult<S, Q>;
111
+
112
+ // Ignore $key in Q
113
+ type PlainReadResult<S, Q> = Q extends AnyObject
114
+ ? { [K in keyof S & keyof Q]: ReadResult<S[K], Q[K]> }
115
+ : S;
116
+
117
+ // What can we tell about ReadResult when schema isn’t known?
118
+ type BlindReadResult<Q> = Q extends Array<infer QItem>
119
+ ? ResultArray<BlindPlainReadResult<QItem>>
120
+ : Q extends { $key: Key }
121
+ ? Q extends { $key: string }
122
+ ? { [key in Q['$key']]: BlindPlainReadResult<Q> }
123
+ : ResultArray<BlindPlainReadResult<Q>>
124
+ : BlindPlainReadResult<Q>;
125
+
126
+ // Ignore $key in Q
127
+ type BlindPlainReadResult<Q> = Q extends AnyObject
128
+ ? { [K in keyof Q]: BlindReadResult<Q[K]> }
129
+ : AnyValue;
130
+
131
+ type GraffyReadOptions = AnyObject;
132
+
133
+ // --- Re-exported from shift.js
134
+ export type unchanged = symbol;
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[];