@graffy/core 0.15.25-alpha.3 → 0.15.25-alpha.5

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 (2) hide show
  1. package/package.json +4 -9
  2. package/index.cjs +0 -243
package/package.json CHANGED
@@ -2,13 +2,8 @@
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-alpha.3",
6
- "main": "./index.cjs",
7
- "exports": {
8
- "import": "./index.mjs",
9
- "require": "./index.cjs"
10
- },
11
- "module": "./index.mjs",
5
+ "version": "0.15.25-alpha.5",
6
+ "main": "./index.mjs",
12
7
  "types": "./types/index.d.ts",
13
8
  "repository": {
14
9
  "type": "git",
@@ -16,8 +11,8 @@
16
11
  },
17
12
  "license": "Apache-2.0",
18
13
  "dependencies": {
19
- "@graffy/common": "0.15.25-alpha.3",
20
- "@graffy/stream": "0.15.25-alpha.3",
14
+ "@graffy/common": "0.15.25-alpha.5",
15
+ "@graffy/stream": "0.15.25-alpha.5",
21
16
  "debug": "^4.3.3"
22
17
  }
23
18
  }
package/index.cjs DELETED
@@ -1,243 +0,0 @@
1
- "use strict";
2
- const common = require("@graffy/common");
3
- const stream = require("@graffy/stream");
4
- const debug = require("debug");
5
- const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
6
- const debug__default = /* @__PURE__ */ _interopDefaultLegacy(debug);
7
- function isPlainObject(obj) {
8
- return obj && typeof obj === "object" && !Array.isArray(obj);
9
- }
10
- function validateCall(...args) {
11
- if (args.length === 1) {
12
- return [[], args[0], {}];
13
- } else if (args.length === 2) {
14
- if (isPlainObject(args[0])) {
15
- if (!isPlainObject(args[1])) {
16
- throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
17
- }
18
- return [[], args[0], args[1]];
19
- } else {
20
- return [common.encodePath(args[0]), args[1], {}];
21
- }
22
- } else if (args.length === 3) {
23
- if (!isPlainObject(args[2])) {
24
- throw Error(`validateCall.invalid_options: ${JSON.stringify(args[1])}`);
25
- }
26
- return [common.encodePath(args[0]), args[1], args[2]];
27
- }
28
- throw Error(`validateCall.invalid_args: ${JSON.stringify(args)}`);
29
- }
30
- function validateOn(...args) {
31
- if (args.length === 1) {
32
- if (typeof args[0] !== "function") {
33
- throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[0])}`);
34
- }
35
- return [[], args[0]];
36
- } else if (args.length === 2) {
37
- if (typeof args[1] !== "function") {
38
- throw Error(`validateOn.invalid_handler: ${JSON.stringify(args[1])}`);
39
- }
40
- return [common.encodePath(args[0]), args[1]];
41
- }
42
- throw Error(`validateOn.invalid_args: ${JSON.stringify(args)}`);
43
- }
44
- async function mapStream(stream2, fn) {
45
- for await (const value of stream2) {
46
- fn(value);
47
- }
48
- }
49
- function shiftFn(fn, path) {
50
- return async function shiftedFn(payload, options, next) {
51
- let nextCalled = false;
52
- let remainingNextResult;
53
- const unwrappedPayload = common.unwrap(payload, path);
54
- const remainingPayload = common.remove(payload, path) || [];
55
- async function shiftedNext(unwrappedNextPayload) {
56
- nextCalled = true;
57
- const nextPayload = common.wrap(unwrappedNextPayload, path);
58
- if (remainingPayload.length)
59
- common.merge(nextPayload, remainingPayload);
60
- const nextResult = await next(nextPayload);
61
- remainingNextResult = common.remove(nextResult, path) || [];
62
- return common.unwrap(nextResult, path);
63
- }
64
- const rawResult = await fn(unwrappedPayload, options, shiftedNext);
65
- const result = common.wrap(rawResult, path);
66
- if (!nextCalled && remainingPayload.length) {
67
- remainingNextResult = await next(remainingPayload);
68
- }
69
- if (remainingNextResult && remainingNextResult.length) {
70
- common.merge(result, remainingNextResult);
71
- }
72
- return result;
73
- };
74
- }
75
- function shiftGen(fn, path) {
76
- return async function* shiftedGen(payload, options, next) {
77
- let nextCalled = false;
78
- let remainingNextStream;
79
- const unwrappedPayload = common.unwrap(payload, path);
80
- const remainingPayload = common.remove(payload, path) || [];
81
- const shiftedNext = async function* shiftedNextFn(unwrappedNextPayload) {
82
- nextCalled = true;
83
- const nextPayload = common.wrap(unwrappedNextPayload, path);
84
- if (remainingPayload.length)
85
- common.merge(nextPayload, remainingPayload);
86
- let pushRemaining;
87
- remainingNextStream = stream.makeStream((push) => {
88
- pushRemaining = push;
89
- });
90
- for await (const value of next(nextPayload)) {
91
- const unwrappedValue = common.unwrap(value, path);
92
- const remainingValue = common.remove(value, path);
93
- if (remainingValue)
94
- pushRemaining(remainingValue);
95
- if (unwrappedValue)
96
- yield unwrappedValue;
97
- }
98
- };
99
- const unwrappedStream = fn(unwrappedPayload, options, shiftedNext);
100
- const firstValue = await (await unwrappedStream.next()).value;
101
- const resultStream = stream.makeStream((push) => {
102
- push(common.wrap(firstValue, path));
103
- mapStream(unwrappedStream, (value) => {
104
- push(common.wrap(value, path));
105
- });
106
- return () => unwrappedStream.return();
107
- });
108
- if (!nextCalled && remainingPayload.length) {
109
- remainingNextStream = next(remainingPayload);
110
- }
111
- yield* remainingNextStream ? common.mergeStreams(resultStream, remainingNextStream) : resultStream;
112
- };
113
- }
114
- const log = debug__default.default("graffy:core");
115
- function resolve(handlers, firstPayload, firstOptions) {
116
- if (!handlers || !handlers.length)
117
- throw Error("resolve.no_provider");
118
- function run(i, payload, options) {
119
- if (i >= handlers.length) {
120
- throw Error("resolve.no_providers_for " + JSON.stringify(payload));
121
- }
122
- const { path, handle } = handlers[i];
123
- if (!common.unwrap(payload, path))
124
- return run(i + 1, payload, options);
125
- let nextCalled = false;
126
- return handle(payload, options, (nextPayload, nextOptions) => {
127
- if (nextCalled) {
128
- throw Error("resolve.duplicate_next_call: " + handlers[i].name);
129
- }
130
- nextCalled = true;
131
- if (typeof nextPayload === "undefined" || !nextPayload.length)
132
- return;
133
- return run(i + 1, nextPayload, nextOptions || options);
134
- });
135
- }
136
- return run(0, firstPayload, firstOptions);
137
- }
138
- class Core {
139
- constructor() {
140
- this.handlers = {};
141
- }
142
- on(type, path, handle) {
143
- this.handlers[type] = this.handlers[type] || [];
144
- this.handlers[type].push({ path, handle });
145
- }
146
- call(type, payload, options = {}) {
147
- log("call", type, payload);
148
- return resolve(this.handlers[type], payload, options);
149
- }
150
- }
151
- class Graffy {
152
- constructor(path = [], core = new Core()) {
153
- this.core = core;
154
- this.path = path;
155
- }
156
- on(type, ...args) {
157
- const [pathArg, handler] = validateOn(...args);
158
- const path = this.path.concat(pathArg);
159
- this.core.on(type, path, handler);
160
- }
161
- onRead(...args) {
162
- const [pathArg, handle] = validateOn(...args);
163
- const path = this.path.concat(pathArg);
164
- this.core.on(
165
- "read",
166
- path,
167
- shiftFn(async function porcelainRead(query, options) {
168
- const decoded = common.decodeQuery(query);
169
- const encoded = common.encodeGraph(await handle(decoded, options));
170
- const finalized = common.finalize(encoded, query);
171
- return finalized;
172
- }, path)
173
- );
174
- }
175
- onWatch(...args) {
176
- const [pathArg, handle] = validateOn(...args);
177
- const path = this.path.concat(pathArg);
178
- this.core.on(
179
- "watch",
180
- path,
181
- shiftGen(function porcelainWatch(query, options) {
182
- return stream.makeStream((push, end) => {
183
- const subscription = handle(common.decodeQuery(query), options);
184
- (async function() {
185
- try {
186
- let firstValue = (await subscription.next()).value;
187
- push(firstValue && common.finalize(common.encodeGraph(firstValue), query));
188
- for await (const value of subscription) {
189
- push(value && common.encodeGraph(value));
190
- }
191
- } catch (e) {
192
- end(e);
193
- }
194
- })();
195
- return () => subscription.return();
196
- });
197
- }, path)
198
- );
199
- }
200
- onWrite(...args) {
201
- const [pathArg, handle] = validateOn(...args);
202
- const path = this.path.concat(pathArg);
203
- this.core.on(
204
- "write",
205
- path,
206
- shiftFn(async function porcelainWrite(change, options) {
207
- return common.encodeGraph(await handle(common.decodeGraph(change), options));
208
- }, path)
209
- );
210
- }
211
- use(...args) {
212
- const [pathArg, provider] = validateOn(...args);
213
- const path = this.path.concat(pathArg);
214
- provider(new Graffy(path, this.core));
215
- }
216
- call(type, payload, options = {}) {
217
- return this.core.call(type, payload, options);
218
- }
219
- async read(...args) {
220
- const [path, porcelainQuery, options] = validateCall(...args);
221
- const rootQuery = common.wrapObject(porcelainQuery, path);
222
- const query = common.encodeQuery(rootQuery);
223
- const result = await this.core.call("read", query, options || {});
224
- return common.unwrapObject(common.decorate(result, rootQuery), common.decodePath(path));
225
- }
226
- watch(...args) {
227
- const [path, porcelainQuery, options] = validateCall(...args);
228
- const rootQuery = common.wrapObject(porcelainQuery, path);
229
- const query = common.encodeQuery(rootQuery);
230
- const stream$1 = this.core.call("watch", query, options || {});
231
- return stream.mapStream(
232
- stream$1,
233
- (value) => common.unwrapObject(common.decorate(value, rootQuery), common.decodePath(path))
234
- );
235
- }
236
- async write(...args) {
237
- const [path, porcelainChange, options] = validateCall(...args);
238
- const change = common.encodeGraph(common.wrapObject(porcelainChange, path));
239
- const writtenChange = await this.core.call("write", change, options || {});
240
- return common.unwrapObject(common.decodeGraph(writtenChange), common.decodePath(path));
241
- }
242
- }
243
- module.exports = Graffy;