@parcae/sdk 0.6.1 → 0.7.0

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.
@@ -1,4 +1,4 @@
1
- import { createClient, log } from '../chunk-YKK5SBY4.js';
1
+ import { __export, createClient, log } from '../chunk-USSSG27K.js';
2
2
  import { createContext, useContext, useMemo, useRef, useEffect, useSyncExternalStore, useState, useCallback } from 'react';
3
3
  import { jsx, Fragment } from 'react/jsx-runtime';
4
4
 
@@ -85,6 +85,642 @@ var ParcaeProvider = ({
85
85
  }, [client]);
86
86
  return /* @__PURE__ */ jsx(ParcaeContext.Provider, { value: client, children });
87
87
  };
88
+
89
+ // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/module/core.mjs
90
+ var core_exports = {};
91
+ __export(core_exports, {
92
+ JsonPatchError: () => JsonPatchError,
93
+ _areEquals: () => _areEquals,
94
+ applyOperation: () => applyOperation,
95
+ applyPatch: () => applyPatch,
96
+ applyReducer: () => applyReducer,
97
+ deepClone: () => deepClone,
98
+ getValueByPointer: () => getValueByPointer,
99
+ validate: () => validate,
100
+ validator: () => validator
101
+ });
102
+
103
+ // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/module/helpers.mjs
104
+ var __extends = /* @__PURE__ */ (function() {
105
+ var extendStatics = function(d, b) {
106
+ extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
107
+ d2.__proto__ = b2;
108
+ } || function(d2, b2) {
109
+ for (var p in b2) if (b2.hasOwnProperty(p)) d2[p] = b2[p];
110
+ };
111
+ return extendStatics(d, b);
112
+ };
113
+ return function(d, b) {
114
+ extendStatics(d, b);
115
+ function __() {
116
+ this.constructor = d;
117
+ }
118
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
119
+ };
120
+ })();
121
+ var _hasOwnProperty = Object.prototype.hasOwnProperty;
122
+ function hasOwnProperty(obj, key) {
123
+ return _hasOwnProperty.call(obj, key);
124
+ }
125
+ function _objectKeys(obj) {
126
+ if (Array.isArray(obj)) {
127
+ var keys_1 = new Array(obj.length);
128
+ for (var k = 0; k < keys_1.length; k++) {
129
+ keys_1[k] = "" + k;
130
+ }
131
+ return keys_1;
132
+ }
133
+ if (Object.keys) {
134
+ return Object.keys(obj);
135
+ }
136
+ var keys = [];
137
+ for (var i in obj) {
138
+ if (hasOwnProperty(obj, i)) {
139
+ keys.push(i);
140
+ }
141
+ }
142
+ return keys;
143
+ }
144
+ function _deepClone(obj) {
145
+ switch (typeof obj) {
146
+ case "object":
147
+ return JSON.parse(JSON.stringify(obj));
148
+ //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
149
+ case "undefined":
150
+ return null;
151
+ //this is how JSON.stringify behaves for array items
152
+ default:
153
+ return obj;
154
+ }
155
+ }
156
+ function isInteger(str) {
157
+ var i = 0;
158
+ var len = str.length;
159
+ var charCode;
160
+ while (i < len) {
161
+ charCode = str.charCodeAt(i);
162
+ if (charCode >= 48 && charCode <= 57) {
163
+ i++;
164
+ continue;
165
+ }
166
+ return false;
167
+ }
168
+ return true;
169
+ }
170
+ function escapePathComponent(path) {
171
+ if (path.indexOf("/") === -1 && path.indexOf("~") === -1)
172
+ return path;
173
+ return path.replace(/~/g, "~0").replace(/\//g, "~1");
174
+ }
175
+ function unescapePathComponent(path) {
176
+ return path.replace(/~1/g, "/").replace(/~0/g, "~");
177
+ }
178
+ function hasUndefined(obj) {
179
+ if (obj === void 0) {
180
+ return true;
181
+ }
182
+ if (obj) {
183
+ if (Array.isArray(obj)) {
184
+ for (var i_1 = 0, len = obj.length; i_1 < len; i_1++) {
185
+ if (hasUndefined(obj[i_1])) {
186
+ return true;
187
+ }
188
+ }
189
+ } else if (typeof obj === "object") {
190
+ var objKeys = _objectKeys(obj);
191
+ var objKeysLength = objKeys.length;
192
+ for (var i = 0; i < objKeysLength; i++) {
193
+ if (hasUndefined(obj[objKeys[i]])) {
194
+ return true;
195
+ }
196
+ }
197
+ }
198
+ }
199
+ return false;
200
+ }
201
+ function patchErrorMessageFormatter(message, args) {
202
+ var messageParts = [message];
203
+ for (var key in args) {
204
+ var value = typeof args[key] === "object" ? JSON.stringify(args[key], null, 2) : args[key];
205
+ if (typeof value !== "undefined") {
206
+ messageParts.push(key + ": " + value);
207
+ }
208
+ }
209
+ return messageParts.join("\n");
210
+ }
211
+ var PatchError = (
212
+ /** @class */
213
+ (function(_super) {
214
+ __extends(PatchError2, _super);
215
+ function PatchError2(message, name, index, operation, tree) {
216
+ var _newTarget = this.constructor;
217
+ var _this = _super.call(this, patchErrorMessageFormatter(message, { name, index, operation, tree })) || this;
218
+ _this.name = name;
219
+ _this.index = index;
220
+ _this.operation = operation;
221
+ _this.tree = tree;
222
+ Object.setPrototypeOf(_this, _newTarget.prototype);
223
+ _this.message = patchErrorMessageFormatter(message, { name, index, operation, tree });
224
+ return _this;
225
+ }
226
+ return PatchError2;
227
+ })(Error)
228
+ );
229
+
230
+ // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/module/core.mjs
231
+ var JsonPatchError = PatchError;
232
+ var deepClone = _deepClone;
233
+ var objOps = {
234
+ add: function(obj, key, document) {
235
+ obj[key] = this.value;
236
+ return { newDocument: document };
237
+ },
238
+ remove: function(obj, key, document) {
239
+ var removed = obj[key];
240
+ delete obj[key];
241
+ return { newDocument: document, removed };
242
+ },
243
+ replace: function(obj, key, document) {
244
+ var removed = obj[key];
245
+ obj[key] = this.value;
246
+ return { newDocument: document, removed };
247
+ },
248
+ move: function(obj, key, document) {
249
+ var removed = getValueByPointer(document, this.path);
250
+ if (removed) {
251
+ removed = _deepClone(removed);
252
+ }
253
+ var originalValue = applyOperation(document, { op: "remove", path: this.from }).removed;
254
+ applyOperation(document, { op: "add", path: this.path, value: originalValue });
255
+ return { newDocument: document, removed };
256
+ },
257
+ copy: function(obj, key, document) {
258
+ var valueToCopy = getValueByPointer(document, this.from);
259
+ applyOperation(document, { op: "add", path: this.path, value: _deepClone(valueToCopy) });
260
+ return { newDocument: document };
261
+ },
262
+ test: function(obj, key, document) {
263
+ return { newDocument: document, test: _areEquals(obj[key], this.value) };
264
+ },
265
+ _get: function(obj, key, document) {
266
+ this.value = obj[key];
267
+ return { newDocument: document };
268
+ }
269
+ };
270
+ var arrOps = {
271
+ add: function(arr, i, document) {
272
+ if (isInteger(i)) {
273
+ arr.splice(i, 0, this.value);
274
+ } else {
275
+ arr[i] = this.value;
276
+ }
277
+ return { newDocument: document, index: i };
278
+ },
279
+ remove: function(arr, i, document) {
280
+ var removedList = arr.splice(i, 1);
281
+ return { newDocument: document, removed: removedList[0] };
282
+ },
283
+ replace: function(arr, i, document) {
284
+ var removed = arr[i];
285
+ arr[i] = this.value;
286
+ return { newDocument: document, removed };
287
+ },
288
+ move: objOps.move,
289
+ copy: objOps.copy,
290
+ test: objOps.test,
291
+ _get: objOps._get
292
+ };
293
+ function getValueByPointer(document, pointer) {
294
+ if (pointer == "") {
295
+ return document;
296
+ }
297
+ var getOriginalDestination = { op: "_get", path: pointer };
298
+ applyOperation(document, getOriginalDestination);
299
+ return getOriginalDestination.value;
300
+ }
301
+ function applyOperation(document, operation, validateOperation, mutateDocument, banPrototypeModifications, index) {
302
+ if (validateOperation === void 0) {
303
+ validateOperation = false;
304
+ }
305
+ if (mutateDocument === void 0) {
306
+ mutateDocument = true;
307
+ }
308
+ if (banPrototypeModifications === void 0) {
309
+ banPrototypeModifications = true;
310
+ }
311
+ if (index === void 0) {
312
+ index = 0;
313
+ }
314
+ if (validateOperation) {
315
+ if (typeof validateOperation == "function") {
316
+ validateOperation(operation, 0, document, operation.path);
317
+ } else {
318
+ validator(operation, 0);
319
+ }
320
+ }
321
+ if (operation.path === "") {
322
+ var returnValue = { newDocument: document };
323
+ if (operation.op === "add") {
324
+ returnValue.newDocument = operation.value;
325
+ return returnValue;
326
+ } else if (operation.op === "replace") {
327
+ returnValue.newDocument = operation.value;
328
+ returnValue.removed = document;
329
+ return returnValue;
330
+ } else if (operation.op === "move" || operation.op === "copy") {
331
+ returnValue.newDocument = getValueByPointer(document, operation.from);
332
+ if (operation.op === "move") {
333
+ returnValue.removed = document;
334
+ }
335
+ return returnValue;
336
+ } else if (operation.op === "test") {
337
+ returnValue.test = _areEquals(document, operation.value);
338
+ if (returnValue.test === false) {
339
+ throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
340
+ }
341
+ returnValue.newDocument = document;
342
+ return returnValue;
343
+ } else if (operation.op === "remove") {
344
+ returnValue.removed = document;
345
+ returnValue.newDocument = null;
346
+ return returnValue;
347
+ } else if (operation.op === "_get") {
348
+ operation.value = document;
349
+ return returnValue;
350
+ } else {
351
+ if (validateOperation) {
352
+ throw new JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902", "OPERATION_OP_INVALID", index, operation, document);
353
+ } else {
354
+ return returnValue;
355
+ }
356
+ }
357
+ } else {
358
+ if (!mutateDocument) {
359
+ document = _deepClone(document);
360
+ }
361
+ var path = operation.path || "";
362
+ var keys = path.split("/");
363
+ var obj = document;
364
+ var t = 1;
365
+ var len = keys.length;
366
+ var existingPathFragment = void 0;
367
+ var key = void 0;
368
+ var validateFunction = void 0;
369
+ if (typeof validateOperation == "function") {
370
+ validateFunction = validateOperation;
371
+ } else {
372
+ validateFunction = validator;
373
+ }
374
+ while (true) {
375
+ key = keys[t];
376
+ if (key && key.indexOf("~") != -1) {
377
+ key = unescapePathComponent(key);
378
+ }
379
+ if (banPrototypeModifications && (key == "__proto__" || key == "prototype" && t > 0 && keys[t - 1] == "constructor")) {
380
+ throw new TypeError("JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README");
381
+ }
382
+ if (validateOperation) {
383
+ if (existingPathFragment === void 0) {
384
+ if (obj[key] === void 0) {
385
+ existingPathFragment = keys.slice(0, t).join("/");
386
+ } else if (t == len - 1) {
387
+ existingPathFragment = operation.path;
388
+ }
389
+ if (existingPathFragment !== void 0) {
390
+ validateFunction(operation, 0, document, existingPathFragment);
391
+ }
392
+ }
393
+ }
394
+ t++;
395
+ if (Array.isArray(obj)) {
396
+ if (key === "-") {
397
+ key = obj.length;
398
+ } else {
399
+ if (validateOperation && !isInteger(key)) {
400
+ throw new JsonPatchError("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index", "OPERATION_PATH_ILLEGAL_ARRAY_INDEX", index, operation, document);
401
+ } else if (isInteger(key)) {
402
+ key = ~~key;
403
+ }
404
+ }
405
+ if (t >= len) {
406
+ if (validateOperation && operation.op === "add" && key > obj.length) {
407
+ throw new JsonPatchError("The specified index MUST NOT be greater than the number of elements in the array", "OPERATION_VALUE_OUT_OF_BOUNDS", index, operation, document);
408
+ }
409
+ var returnValue = arrOps[operation.op].call(operation, obj, key, document);
410
+ if (returnValue.test === false) {
411
+ throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
412
+ }
413
+ return returnValue;
414
+ }
415
+ } else {
416
+ if (t >= len) {
417
+ var returnValue = objOps[operation.op].call(operation, obj, key, document);
418
+ if (returnValue.test === false) {
419
+ throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
420
+ }
421
+ return returnValue;
422
+ }
423
+ }
424
+ obj = obj[key];
425
+ if (validateOperation && t < len && (!obj || typeof obj !== "object")) {
426
+ throw new JsonPatchError("Cannot perform operation at the desired path", "OPERATION_PATH_UNRESOLVABLE", index, operation, document);
427
+ }
428
+ }
429
+ }
430
+ }
431
+ function applyPatch(document, patch, validateOperation, mutateDocument, banPrototypeModifications) {
432
+ if (mutateDocument === void 0) {
433
+ mutateDocument = true;
434
+ }
435
+ if (banPrototypeModifications === void 0) {
436
+ banPrototypeModifications = true;
437
+ }
438
+ if (validateOperation) {
439
+ if (!Array.isArray(patch)) {
440
+ throw new JsonPatchError("Patch sequence must be an array", "SEQUENCE_NOT_AN_ARRAY");
441
+ }
442
+ }
443
+ if (!mutateDocument) {
444
+ document = _deepClone(document);
445
+ }
446
+ var results = new Array(patch.length);
447
+ for (var i = 0, length_1 = patch.length; i < length_1; i++) {
448
+ results[i] = applyOperation(document, patch[i], validateOperation, true, banPrototypeModifications, i);
449
+ document = results[i].newDocument;
450
+ }
451
+ results.newDocument = document;
452
+ return results;
453
+ }
454
+ function applyReducer(document, operation, index) {
455
+ var operationResult = applyOperation(document, operation);
456
+ if (operationResult.test === false) {
457
+ throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
458
+ }
459
+ return operationResult.newDocument;
460
+ }
461
+ function validator(operation, index, document, existingPathFragment) {
462
+ if (typeof operation !== "object" || operation === null || Array.isArray(operation)) {
463
+ throw new JsonPatchError("Operation is not an object", "OPERATION_NOT_AN_OBJECT", index, operation, document);
464
+ } else if (!objOps[operation.op]) {
465
+ throw new JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902", "OPERATION_OP_INVALID", index, operation, document);
466
+ } else if (typeof operation.path !== "string") {
467
+ throw new JsonPatchError("Operation `path` property is not a string", "OPERATION_PATH_INVALID", index, operation, document);
468
+ } else if (operation.path.indexOf("/") !== 0 && operation.path.length > 0) {
469
+ throw new JsonPatchError('Operation `path` property must start with "/"', "OPERATION_PATH_INVALID", index, operation, document);
470
+ } else if ((operation.op === "move" || operation.op === "copy") && typeof operation.from !== "string") {
471
+ throw new JsonPatchError("Operation `from` property is not present (applicable in `move` and `copy` operations)", "OPERATION_FROM_REQUIRED", index, operation, document);
472
+ } else if ((operation.op === "add" || operation.op === "replace" || operation.op === "test") && operation.value === void 0) {
473
+ throw new JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)", "OPERATION_VALUE_REQUIRED", index, operation, document);
474
+ } else if ((operation.op === "add" || operation.op === "replace" || operation.op === "test") && hasUndefined(operation.value)) {
475
+ throw new JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)", "OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED", index, operation, document);
476
+ } else if (document) {
477
+ if (operation.op == "add") {
478
+ var pathLen = operation.path.split("/").length;
479
+ var existingPathLen = existingPathFragment.split("/").length;
480
+ if (pathLen !== existingPathLen + 1 && pathLen !== existingPathLen) {
481
+ throw new JsonPatchError("Cannot perform an `add` operation at the desired path", "OPERATION_PATH_CANNOT_ADD", index, operation, document);
482
+ }
483
+ } else if (operation.op === "replace" || operation.op === "remove" || operation.op === "_get") {
484
+ if (operation.path !== existingPathFragment) {
485
+ throw new JsonPatchError("Cannot perform the operation at a path that does not exist", "OPERATION_PATH_UNRESOLVABLE", index, operation, document);
486
+ }
487
+ } else if (operation.op === "move" || operation.op === "copy") {
488
+ var existingValue = { op: "_get", path: operation.from, value: void 0 };
489
+ var error = validate([existingValue], document);
490
+ if (error && error.name === "OPERATION_PATH_UNRESOLVABLE") {
491
+ throw new JsonPatchError("Cannot perform the operation from a path that does not exist", "OPERATION_FROM_UNRESOLVABLE", index, operation, document);
492
+ }
493
+ }
494
+ }
495
+ }
496
+ function validate(sequence, document, externalValidator) {
497
+ try {
498
+ if (!Array.isArray(sequence)) {
499
+ throw new JsonPatchError("Patch sequence must be an array", "SEQUENCE_NOT_AN_ARRAY");
500
+ }
501
+ if (document) {
502
+ applyPatch(_deepClone(document), _deepClone(sequence), externalValidator || true);
503
+ } else {
504
+ externalValidator = externalValidator || validator;
505
+ for (var i = 0; i < sequence.length; i++) {
506
+ externalValidator(sequence[i], i, document, void 0);
507
+ }
508
+ }
509
+ } catch (e) {
510
+ if (e instanceof JsonPatchError) {
511
+ return e;
512
+ } else {
513
+ throw e;
514
+ }
515
+ }
516
+ }
517
+ function _areEquals(a, b) {
518
+ if (a === b)
519
+ return true;
520
+ if (a && b && typeof a == "object" && typeof b == "object") {
521
+ var arrA = Array.isArray(a), arrB = Array.isArray(b), i, length, key;
522
+ if (arrA && arrB) {
523
+ length = a.length;
524
+ if (length != b.length)
525
+ return false;
526
+ for (i = length; i-- !== 0; )
527
+ if (!_areEquals(a[i], b[i]))
528
+ return false;
529
+ return true;
530
+ }
531
+ if (arrA != arrB)
532
+ return false;
533
+ var keys = Object.keys(a);
534
+ length = keys.length;
535
+ if (length !== Object.keys(b).length)
536
+ return false;
537
+ for (i = length; i-- !== 0; )
538
+ if (!b.hasOwnProperty(keys[i]))
539
+ return false;
540
+ for (i = length; i-- !== 0; ) {
541
+ key = keys[i];
542
+ if (!_areEquals(a[key], b[key]))
543
+ return false;
544
+ }
545
+ return true;
546
+ }
547
+ return a !== a && b !== b;
548
+ }
549
+
550
+ // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/module/duplex.mjs
551
+ var duplex_exports = {};
552
+ __export(duplex_exports, {
553
+ compare: () => compare,
554
+ generate: () => generate,
555
+ observe: () => observe,
556
+ unobserve: () => unobserve
557
+ });
558
+ var beforeDict = /* @__PURE__ */ new WeakMap();
559
+ var Mirror = (
560
+ /** @class */
561
+ /* @__PURE__ */ (function() {
562
+ function Mirror2(obj) {
563
+ this.observers = /* @__PURE__ */ new Map();
564
+ this.obj = obj;
565
+ }
566
+ return Mirror2;
567
+ })()
568
+ );
569
+ var ObserverInfo = (
570
+ /** @class */
571
+ /* @__PURE__ */ (function() {
572
+ function ObserverInfo2(callback, observer) {
573
+ this.callback = callback;
574
+ this.observer = observer;
575
+ }
576
+ return ObserverInfo2;
577
+ })()
578
+ );
579
+ function getMirror(obj) {
580
+ return beforeDict.get(obj);
581
+ }
582
+ function getObserverFromMirror(mirror, callback) {
583
+ return mirror.observers.get(callback);
584
+ }
585
+ function removeObserverFromMirror(mirror, observer) {
586
+ mirror.observers.delete(observer.callback);
587
+ }
588
+ function unobserve(root, observer) {
589
+ observer.unobserve();
590
+ }
591
+ function observe(obj, callback) {
592
+ var patches = [];
593
+ var observer;
594
+ var mirror = getMirror(obj);
595
+ if (!mirror) {
596
+ mirror = new Mirror(obj);
597
+ beforeDict.set(obj, mirror);
598
+ } else {
599
+ var observerInfo = getObserverFromMirror(mirror, callback);
600
+ observer = observerInfo && observerInfo.observer;
601
+ }
602
+ if (observer) {
603
+ return observer;
604
+ }
605
+ observer = {};
606
+ mirror.value = _deepClone(obj);
607
+ if (callback) {
608
+ observer.callback = callback;
609
+ observer.next = null;
610
+ var dirtyCheck = function() {
611
+ generate(observer);
612
+ };
613
+ var fastCheck = function() {
614
+ clearTimeout(observer.next);
615
+ observer.next = setTimeout(dirtyCheck);
616
+ };
617
+ if (typeof window !== "undefined") {
618
+ window.addEventListener("mouseup", fastCheck);
619
+ window.addEventListener("keyup", fastCheck);
620
+ window.addEventListener("mousedown", fastCheck);
621
+ window.addEventListener("keydown", fastCheck);
622
+ window.addEventListener("change", fastCheck);
623
+ }
624
+ }
625
+ observer.patches = patches;
626
+ observer.object = obj;
627
+ observer.unobserve = function() {
628
+ generate(observer);
629
+ clearTimeout(observer.next);
630
+ removeObserverFromMirror(mirror, observer);
631
+ if (typeof window !== "undefined") {
632
+ window.removeEventListener("mouseup", fastCheck);
633
+ window.removeEventListener("keyup", fastCheck);
634
+ window.removeEventListener("mousedown", fastCheck);
635
+ window.removeEventListener("keydown", fastCheck);
636
+ window.removeEventListener("change", fastCheck);
637
+ }
638
+ };
639
+ mirror.observers.set(callback, new ObserverInfo(callback, observer));
640
+ return observer;
641
+ }
642
+ function generate(observer, invertible) {
643
+ if (invertible === void 0) {
644
+ invertible = false;
645
+ }
646
+ var mirror = beforeDict.get(observer.object);
647
+ _generate(mirror.value, observer.object, observer.patches, "", invertible);
648
+ if (observer.patches.length) {
649
+ applyPatch(mirror.value, observer.patches);
650
+ }
651
+ var temp = observer.patches;
652
+ if (temp.length > 0) {
653
+ observer.patches = [];
654
+ if (observer.callback) {
655
+ observer.callback(temp);
656
+ }
657
+ }
658
+ return temp;
659
+ }
660
+ function _generate(mirror, obj, patches, path, invertible) {
661
+ if (obj === mirror) {
662
+ return;
663
+ }
664
+ if (typeof obj.toJSON === "function") {
665
+ obj = obj.toJSON();
666
+ }
667
+ var newKeys = _objectKeys(obj);
668
+ var oldKeys = _objectKeys(mirror);
669
+ var deleted = false;
670
+ for (var t = oldKeys.length - 1; t >= 0; t--) {
671
+ var key = oldKeys[t];
672
+ var oldVal = mirror[key];
673
+ if (hasOwnProperty(obj, key) && !(obj[key] === void 0 && oldVal !== void 0 && Array.isArray(obj) === false)) {
674
+ var newVal = obj[key];
675
+ if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null && Array.isArray(oldVal) === Array.isArray(newVal)) {
676
+ _generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key), invertible);
677
+ } else {
678
+ if (oldVal !== newVal) {
679
+ if (invertible) {
680
+ patches.push({ op: "test", path: path + "/" + escapePathComponent(key), value: _deepClone(oldVal) });
681
+ }
682
+ patches.push({ op: "replace", path: path + "/" + escapePathComponent(key), value: _deepClone(newVal) });
683
+ }
684
+ }
685
+ } else if (Array.isArray(mirror) === Array.isArray(obj)) {
686
+ if (invertible) {
687
+ patches.push({ op: "test", path: path + "/" + escapePathComponent(key), value: _deepClone(oldVal) });
688
+ }
689
+ patches.push({ op: "remove", path: path + "/" + escapePathComponent(key) });
690
+ deleted = true;
691
+ } else {
692
+ if (invertible) {
693
+ patches.push({ op: "test", path, value: mirror });
694
+ }
695
+ patches.push({ op: "replace", path, value: obj });
696
+ }
697
+ }
698
+ if (!deleted && newKeys.length == oldKeys.length) {
699
+ return;
700
+ }
701
+ for (var t = 0; t < newKeys.length; t++) {
702
+ var key = newKeys[t];
703
+ if (!hasOwnProperty(mirror, key) && obj[key] !== void 0) {
704
+ patches.push({ op: "add", path: path + "/" + escapePathComponent(key), value: _deepClone(obj[key]) });
705
+ }
706
+ }
707
+ }
708
+ function compare(tree1, tree2, invertible) {
709
+ if (invertible === void 0) {
710
+ invertible = false;
711
+ }
712
+ var patches = [];
713
+ _generate(tree1, tree2, patches, "", invertible);
714
+ return patches;
715
+ }
716
+
717
+ // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/index.mjs
718
+ Object.assign({}, core_exports, duplex_exports, {
719
+ JsonPatchError: PatchError,
720
+ deepClone: _deepClone,
721
+ escapePathComponent,
722
+ unescapePathComponent
723
+ });
88
724
  function useAuthStatus() {
89
725
  const client = useParcae();
90
726
  const transport = client.transport;
@@ -112,17 +748,20 @@ function useAuthStatus() {
112
748
  var cache = /* @__PURE__ */ new Map();
113
749
  var GC_DELAY = 6e4;
114
750
  var EMPTY = [];
115
- function getOrCreate(key) {
751
+ function getOrCreate(key, modelClass) {
116
752
  let e = cache.get(key);
117
753
  if (!e) {
118
754
  e = {
119
755
  items: EMPTY,
756
+ itemMap: /* @__PURE__ */ new Map(),
120
757
  loading: true,
121
758
  error: null,
122
759
  refs: 0,
123
760
  listeners: /* @__PURE__ */ new Set(),
124
761
  dispose: null,
125
- gcTimer: null
762
+ gcTimer: null,
763
+ hash: null,
764
+ modelClass: modelClass ?? null
126
765
  };
127
766
  cache.set(key, e);
128
767
  }
@@ -131,16 +770,37 @@ function getOrCreate(key) {
131
770
  function notify(e) {
132
771
  for (const fn of e.listeners) fn();
133
772
  }
134
- function doFetch(key, entry, chain) {
773
+ function doFetchAndSubscribe(key, entry, chain, client) {
135
774
  log.debug("useQuery: fetching", chain.__modelType);
136
775
  entry.loading = true;
137
776
  entry.error = null;
138
777
  notify(entry);
139
- chain.find().then((result) => {
140
- log.debug("useQuery: got", result.length, "items for", chain.__modelType);
141
- entry.items = result;
778
+ chain.findAndSubscribe().then(({ items, hash }) => {
779
+ log.debug("useQuery: got", items.length, "items, hash:", hash);
780
+ entry.items = items;
781
+ entry.itemMap = /* @__PURE__ */ new Map();
782
+ for (const item of items) {
783
+ const id = item.id;
784
+ if (id) entry.itemMap.set(id, item);
785
+ }
142
786
  entry.loading = false;
787
+ entry.hash = hash;
143
788
  notify(entry);
789
+ if (hash) {
790
+ const diffHandler = (ops) => {
791
+ if (!Array.isArray(ops)) return;
792
+ applyOps(entry, ops);
793
+ };
794
+ const unsubDiff = client.subscribe(`query:${hash}`, diffHandler);
795
+ const prevDispose = entry.dispose;
796
+ entry.dispose = () => {
797
+ unsubDiff();
798
+ client.send?.("unsubscribe:query", hash);
799
+ entry.hash = null;
800
+ entry.dispose = null;
801
+ prevDispose?.();
802
+ };
803
+ }
144
804
  }).catch((err) => {
145
805
  log.error("useQuery: error", err.message);
146
806
  entry.error = err;
@@ -148,6 +808,50 @@ function doFetch(key, entry, chain) {
148
808
  notify(entry);
149
809
  });
150
810
  }
811
+ function applyOps(entry, ops) {
812
+ let changed = false;
813
+ const adapter = entry.items[0]?.[/* @__PURE__ */ Symbol.for?.("parcae:adapter")] ?? null;
814
+ for (const op of ops) {
815
+ switch (op.op) {
816
+ case "add": {
817
+ if (!entry.itemMap.has(op.id) && entry.modelClass && adapter) {
818
+ const instance = new entry.modelClass(adapter, op.data);
819
+ entry.itemMap.set(op.id, instance);
820
+ changed = true;
821
+ }
822
+ break;
823
+ }
824
+ case "remove": {
825
+ if (entry.itemMap.has(op.id)) {
826
+ entry.itemMap.delete(op.id);
827
+ changed = true;
828
+ }
829
+ break;
830
+ }
831
+ case "patch": {
832
+ const existing = entry.itemMap.get(op.id);
833
+ if (existing && op.patch?.length > 0) {
834
+ try {
835
+ const data = existing.__data;
836
+ applyPatch(data, op.patch, false, true);
837
+ for (const patchOp of op.patch) {
838
+ const key = patchOp.path?.split("/")?.[1];
839
+ if (key && key in data) existing[key] = data[key];
840
+ }
841
+ changed = true;
842
+ } catch (err) {
843
+ log.error("useQuery: patch apply failed", err);
844
+ }
845
+ }
846
+ break;
847
+ }
848
+ }
849
+ }
850
+ if (changed) {
851
+ entry.items = [...entry.itemMap.values()];
852
+ notify(entry);
853
+ }
854
+ }
151
855
  function useQuery(chain, options = {}) {
152
856
  const client = useParcae();
153
857
  const waitForAuth = options.waitForAuth ?? true;
@@ -160,7 +864,7 @@ function useQuery(chain, options = {}) {
160
864
  const k = keyRef.current;
161
865
  if (!k) return () => {
162
866
  };
163
- const e = getOrCreate(k);
867
+ const e = getOrCreate(k, chain?.__modelClass);
164
868
  e.refs++;
165
869
  e.listeners.add(onChange);
166
870
  if (e.gcTimer) {
@@ -190,23 +894,32 @@ function useQuery(chain, options = {}) {
190
894
  );
191
895
  useEffect(() => {
192
896
  if (!key || !chain) return;
193
- const entry2 = getOrCreate(key);
194
- if (entry2.items === EMPTY) {
195
- doFetch(key, entry2, chain);
196
- }
197
- if (chain.__modelType) {
198
- const unsub = client.subscribe(
199
- `model:${chain.__modelType}:changed`,
200
- () => doFetch(key, getOrCreate(key), chain)
201
- );
202
- return unsub;
897
+ const entry2 = getOrCreate(key, chain.__modelClass);
898
+ if (entry2.items === EMPTY && !entry2.dispose) {
899
+ doFetchAndSubscribe(key, entry2, chain, client);
203
900
  }
901
+ const onReconnect = () => {
902
+ if (!cache.has(key)) return;
903
+ const e = cache.get(key);
904
+ e.dispose?.();
905
+ e.loading = true;
906
+ notify(e);
907
+ doFetchAndSubscribe(key, e, chain, client);
908
+ };
909
+ client.on?.("connected", onReconnect);
910
+ return () => {
911
+ client.off?.("connected", onReconnect);
912
+ };
204
913
  }, [key]);
205
914
  const refetch = () => {
206
915
  if (!key || !chain) return;
207
- doFetch(key, getOrCreate(key), chain);
916
+ const entry2 = getOrCreate(key, chain.__modelClass);
917
+ entry2.dispose?.();
918
+ entry2.loading = true;
919
+ notify(entry2);
920
+ doFetchAndSubscribe(key, entry2, chain, client);
208
921
  };
209
- if (!key)
922
+ if (!key) {
210
923
  return {
211
924
  items: EMPTY,
212
925
  loading: !authReady,
@@ -214,6 +927,7 @@ function useQuery(chain, options = {}) {
214
927
  refetch: () => {
215
928
  }
216
929
  };
930
+ }
217
931
  const entry = cache.get(key);
218
932
  return {
219
933
  items,
@@ -222,6 +936,13 @@ function useQuery(chain, options = {}) {
222
936
  refetch
223
937
  };
224
938
  }
939
+ function clearQueryCache() {
940
+ for (const [, entry] of cache) {
941
+ entry.dispose?.();
942
+ if (entry.gcTimer) clearTimeout(entry.gcTimer);
943
+ }
944
+ cache.clear();
945
+ }
225
946
  function useApi() {
226
947
  const client = useParcae();
227
948
  return useMemo(
@@ -291,7 +1012,23 @@ function AuthLoading({ children, fallback = null }) {
291
1012
  const { status } = useAuthStatus();
292
1013
  return status === "pending" ? /* @__PURE__ */ jsx(Fragment, { children }) : /* @__PURE__ */ jsx(Fragment, { children: fallback });
293
1014
  }
1015
+ /*! Bundled license information:
1016
+
1017
+ fast-json-patch/module/helpers.mjs:
1018
+ (*!
1019
+ * https://github.com/Starcounter-Jack/JSON-Patch
1020
+ * (c) 2017-2022 Joachim Wester
1021
+ * MIT licensed
1022
+ *)
1023
+
1024
+ fast-json-patch/module/duplex.mjs:
1025
+ (*!
1026
+ * https://github.com/Starcounter-Jack/JSON-Patch
1027
+ * (c) 2017-2021 Joachim Wester
1028
+ * MIT license
1029
+ *)
1030
+ */
294
1031
 
295
- export { AuthLoading, Authenticated, ParcaeContext, ParcaeProvider, Unauthenticated, useApi, useAuthState, useConnectionStatus, useParcae, useQuery, useSDK, useSetting };
1032
+ export { AuthLoading, Authenticated, ParcaeContext, ParcaeProvider, Unauthenticated, clearQueryCache, useApi, useAuthState, useConnectionStatus, useParcae, useQuery, useSDK, useSetting };
296
1033
  //# sourceMappingURL=index.js.map
297
1034
  //# sourceMappingURL=index.js.map