@sylphx/lens-solid 2.1.2 → 2.1.4

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/dist/index.js +80 -27
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -397,7 +397,7 @@ class ClientImpl {
397
397
  connectPromise = null;
398
398
  subscriptions = new Map;
399
399
  queryResultCache = new Map;
400
- callbackWrappers = new WeakMap;
400
+ observerEntries = new WeakMap;
401
401
  constructor(config) {
402
402
  this.transport = config.transport;
403
403
  this.plugins = config.plugins ?? [];
@@ -499,8 +499,21 @@ class ClientImpl {
499
499
  generateId(type, path) {
500
500
  return `${type}-${path}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
501
501
  }
502
+ inputHashCache = new WeakMap;
502
503
  makeQueryKey(path, input) {
503
- return `${path}:${JSON.stringify(input ?? null)}`;
504
+ if (input === undefined || input === null) {
505
+ return `${path}:null`;
506
+ }
507
+ if (typeof input !== "object") {
508
+ return `${path}:${String(input)}`;
509
+ }
510
+ const obj = input;
511
+ let hash = this.inputHashCache.get(obj);
512
+ if (!hash) {
513
+ hash = JSON.stringify(input);
514
+ this.inputHashCache.set(obj, hash);
515
+ }
516
+ return `${path}:${hash}`;
504
517
  }
505
518
  executeQuery(path, input, select) {
506
519
  const key = this.makeQueryKey(path, input);
@@ -511,7 +524,9 @@ class ClientImpl {
511
524
  if (!this.subscriptions.has(key)) {
512
525
  this.subscriptions.set(key, {
513
526
  data: null,
514
- callbacks: new Set
527
+ error: null,
528
+ completed: false,
529
+ observers: new Set
515
530
  });
516
531
  }
517
532
  const sub = this.subscriptions.get(key);
@@ -519,33 +534,48 @@ class ClientImpl {
519
534
  get value() {
520
535
  return sub.data;
521
536
  },
522
- subscribe: (callback) => {
523
- if (callback) {
524
- const typedCallback = callback;
525
- let wrapped = this.callbackWrappers.get(typedCallback);
526
- if (!wrapped) {
527
- wrapped = (data) => callback(data);
528
- this.callbackWrappers.set(typedCallback, wrapped);
529
- }
530
- sub.callbacks.add(wrapped);
531
- if (sub.data !== null) {
532
- callback(sub.data);
533
- }
537
+ subscribe: (observerOrCallback) => {
538
+ let entry;
539
+ if (typeof observerOrCallback === "function") {
540
+ const callback = observerOrCallback;
541
+ entry = { next: (data) => callback(data) };
542
+ } else if (observerOrCallback && typeof observerOrCallback === "object") {
543
+ const observer = observerOrCallback;
544
+ entry = {
545
+ next: observer.next ? (data) => observer.next(data) : undefined,
546
+ error: observer.error,
547
+ complete: observer.complete
548
+ };
549
+ } else {
550
+ entry = {};
551
+ }
552
+ if (observerOrCallback) {
553
+ this.observerEntries.set(observerOrCallback, entry);
554
+ }
555
+ sub.observers.add(entry);
556
+ if (sub.error && entry.error) {
557
+ entry.error(sub.error);
558
+ } else if (sub.data !== null && entry.next) {
559
+ entry.next(sub.data);
560
+ }
561
+ if (sub.completed && entry.complete) {
562
+ entry.complete();
534
563
  }
535
564
  if (!sub.unsubscribe) {
536
565
  this.startSubscription(path, input, key);
537
566
  }
538
567
  return () => {
539
- if (callback) {
540
- const typedCallback = callback;
541
- const wrapped = this.callbackWrappers.get(typedCallback);
542
- if (wrapped) {
543
- sub.callbacks.delete(wrapped);
568
+ if (observerOrCallback) {
569
+ const storedEntry = this.observerEntries.get(observerOrCallback);
570
+ if (storedEntry) {
571
+ sub.observers.delete(storedEntry);
544
572
  }
545
573
  }
546
- if (sub.callbacks.size === 0 && sub.unsubscribe) {
574
+ if (sub.observers.size === 0 && sub.unsubscribe) {
547
575
  sub.unsubscribe();
548
576
  sub.unsubscribe = undefined;
577
+ this.subscriptions.delete(key);
578
+ this.queryResultCache.delete(key);
549
579
  }
550
580
  };
551
581
  },
@@ -566,11 +596,16 @@ class ClientImpl {
566
596
  throw response.error;
567
597
  }
568
598
  sub.data = response.data;
569
- for (const cb of sub.callbacks) {
570
- cb(response.data);
599
+ for (const observer of sub.observers) {
600
+ observer.next?.(response.data);
571
601
  }
572
602
  return onfulfilled ? onfulfilled(response.data) : response.data;
573
603
  } catch (error) {
604
+ const err = error instanceof Error ? error : new Error(String(error));
605
+ sub.error = err;
606
+ for (const observer of sub.observers) {
607
+ observer.error?.(err);
608
+ }
574
609
  if (onrejected) {
575
610
  return onrejected(error);
576
611
  }
@@ -603,13 +638,31 @@ class ClientImpl {
603
638
  next: (result) => {
604
639
  if (result.data !== undefined) {
605
640
  sub.data = result.data;
606
- for (const cb of sub.callbacks) {
607
- cb(result.data);
641
+ sub.error = null;
642
+ for (const observer of sub.observers) {
643
+ observer.next?.(result.data);
644
+ }
645
+ }
646
+ if (result.error) {
647
+ const err = result.error instanceof Error ? result.error : new Error(String(result.error));
648
+ sub.error = err;
649
+ for (const observer of sub.observers) {
650
+ observer.error?.(err);
608
651
  }
609
652
  }
610
653
  },
611
- error: () => {},
612
- complete: () => {}
654
+ error: (err) => {
655
+ sub.error = err;
656
+ for (const observer of sub.observers) {
657
+ observer.error?.(err);
658
+ }
659
+ },
660
+ complete: () => {
661
+ sub.completed = true;
662
+ for (const observer of sub.observers) {
663
+ observer.complete?.();
664
+ }
665
+ }
613
666
  });
614
667
  sub.unsubscribe = () => subscription.unsubscribe();
615
668
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sylphx/lens-solid",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "SolidJS bindings for Lens API framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -31,7 +31,7 @@
31
31
  "author": "SylphxAI",
32
32
  "license": "MIT",
33
33
  "dependencies": {
34
- "@sylphx/lens-client": "^2.2.1",
34
+ "@sylphx/lens-client": "^2.3.1",
35
35
  "@sylphx/lens-core": "^2.2.0"
36
36
  },
37
37
  "peerDependencies": {