@telestack/db-sdk 1.0.8 → 1.1.1

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/dist/index.d.ts CHANGED
@@ -128,6 +128,10 @@ export declare class Query<T = any> {
128
128
  user: string;
129
129
  clientId: string;
130
130
  }) => void): () => void;
131
+ /** Broadcast a transient signal to this collection (bypasses database) */
132
+ signal(data: any): Promise<void>;
133
+ /** Listen for transient signals on this collection */
134
+ onSignal(callback: (data: any) => void): () => void;
131
135
  }
132
136
  /**
133
137
  * CollectionReference extends Query with add() and doc() methods
package/dist/index.js CHANGED
@@ -516,6 +516,57 @@ export class Query {
516
516
  sub.removeAllListeners();
517
517
  };
518
518
  }
519
+ /** Broadcast a transient signal to this collection (bypasses database) */
520
+ async signal(data) {
521
+ const centrifuge = this.client.getCentrifuge();
522
+ if (!centrifuge)
523
+ return;
524
+ const channel = `collection:${this.path.replace(/\//g, '_')}`;
525
+ let sub = centrifuge.getSubscription(channel);
526
+ if (!sub) {
527
+ sub = centrifuge.newSubscription(channel);
528
+ }
529
+ if (sub.state !== 'subscribed') {
530
+ sub.subscribe();
531
+ // Wait for subscription to be ready
532
+ await new Promise((resolve) => {
533
+ const onSubscribed = () => {
534
+ sub?.off('subscribed', onSubscribed);
535
+ resolve();
536
+ };
537
+ sub?.on('subscribed', onSubscribed);
538
+ // timeout after 2s to prevent hanging
539
+ setTimeout(resolve, 2000);
540
+ });
541
+ }
542
+ try {
543
+ await sub.publish({ type: 'SIGNAL', data, userId: this.client.config.userId });
544
+ }
545
+ catch (e) {
546
+ console.warn("Telestack: Signal failed", e);
547
+ }
548
+ }
549
+ /** Listen for transient signals on this collection */
550
+ onSignal(callback) {
551
+ const centrifuge = this.client.getCentrifuge();
552
+ if (!centrifuge)
553
+ return () => { };
554
+ const channel = `collection:${this.path.replace(/\//g, '_')}`;
555
+ let sub = centrifuge.getSubscription(channel);
556
+ if (!sub) {
557
+ sub = centrifuge.newSubscription(channel);
558
+ }
559
+ const handler = (ctx) => {
560
+ if (ctx.data.type === 'SIGNAL') {
561
+ callback({ ...ctx.data.data, _userId: ctx.data.userId });
562
+ }
563
+ };
564
+ sub.on('publication', handler);
565
+ sub.subscribe();
566
+ return () => {
567
+ sub.off('publication', handler);
568
+ };
569
+ }
519
570
  }
520
571
  /**
521
572
  * CollectionReference extends Query with add() and doc() methods
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telestack/db-sdk",
3
- "version": "1.0.8",
3
+ "version": "1.1.1",
4
4
  "description": "Fluent, real-time SDK for Telestack DB",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",