@edge-base/react-native 0.2.0 → 0.2.2

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.cjs CHANGED
@@ -652,7 +652,7 @@ var DatabaseLiveClient = class {
652
652
  }
653
653
  this.connect(channel).catch(() => {
654
654
  });
655
- return () => {
655
+ return core.createSubscription(() => {
656
656
  const subs = this.subscriptions.get(channel);
657
657
  if (!subs) return;
658
658
  const idx = subs.indexOf(sub);
@@ -663,14 +663,14 @@ var DatabaseLiveClient = class {
663
663
  this.channelOrFilters.delete(channel);
664
664
  this.sendUnsubscribe(channel);
665
665
  }
666
- };
666
+ });
667
667
  }
668
668
  onError(handler) {
669
669
  this.errorHandlers.push(handler);
670
- return () => {
670
+ return core.createSubscription(() => {
671
671
  const idx = this.errorHandlers.indexOf(handler);
672
672
  if (idx >= 0) this.errorHandlers.splice(idx, 1);
673
- };
673
+ });
674
674
  }
675
675
  async connect(channel) {
676
676
  this.connectedChannels.add(channel);
@@ -758,7 +758,7 @@ var DatabaseLiveClient = class {
758
758
  (refreshToken) => refreshAccessToken(this.baseUrl, refreshToken)
759
759
  );
760
760
  if (!token) throw new core.EdgeBaseError(401, "No access token available. Sign in first.");
761
- this.sendRaw({ type: "auth", token, sdkVersion: "0.2.0" });
761
+ this.sendRaw({ type: "auth", token, sdkVersion: "0.2.2" });
762
762
  return new Promise((resolve, reject) => {
763
763
  const timeout = setTimeout(() => reject(new core.EdgeBaseError(401, "Auth timeout")), 1e4);
764
764
  const original = this.ws?.onmessage;
@@ -878,7 +878,7 @@ var DatabaseLiveClient = class {
878
878
  refreshAuth() {
879
879
  const token = this.tokenManager.currentAccessToken;
880
880
  if (!token || !this.ws || !this.connected) return;
881
- this.sendRaw({ type: "auth", token, sdkVersion: "0.2.0" });
881
+ this.sendRaw({ type: "auth", token, sdkVersion: "0.2.2" });
882
882
  }
883
883
  handleAuthStateChange(user) {
884
884
  if (user) {
@@ -1225,14 +1225,12 @@ var RoomCloudflareMediaTransport = class {
1225
1225
  }
1226
1226
  onRemoteTrack(handler) {
1227
1227
  this.remoteTrackHandlers.push(handler);
1228
- return {
1229
- unsubscribe: () => {
1230
- const index = this.remoteTrackHandlers.indexOf(handler);
1231
- if (index >= 0) {
1232
- this.remoteTrackHandlers.splice(index, 1);
1233
- }
1228
+ return core.createSubscription(() => {
1229
+ const index = this.remoteTrackHandlers.indexOf(handler);
1230
+ if (index >= 0) {
1231
+ this.remoteTrackHandlers.splice(index, 1);
1234
1232
  }
1235
- };
1233
+ });
1236
1234
  }
1237
1235
  destroy() {
1238
1236
  this.lifecycleVersion += 1;
@@ -1633,14 +1631,12 @@ var RoomP2PMediaTransport = class {
1633
1631
  }
1634
1632
  onRemoteTrack(handler) {
1635
1633
  this.remoteTrackHandlers.push(handler);
1636
- return {
1637
- unsubscribe: () => {
1638
- const index = this.remoteTrackHandlers.indexOf(handler);
1639
- if (index >= 0) {
1640
- this.remoteTrackHandlers.splice(index, 1);
1641
- }
1634
+ return core.createSubscription(() => {
1635
+ const index = this.remoteTrackHandlers.indexOf(handler);
1636
+ if (index >= 0) {
1637
+ this.remoteTrackHandlers.splice(index, 1);
1642
1638
  }
1643
- };
1639
+ });
1644
1640
  }
1645
1641
  destroy() {
1646
1642
  this.connected = false;
@@ -2507,31 +2503,27 @@ var RoomClient = class _RoomClient {
2507
2503
  * Subscribe to shared state changes.
2508
2504
  * Called on full sync and on each shared_delta.
2509
2505
  *
2510
- * @returns Subscription with unsubscribe()
2506
+ * @returns Subscription (callable & .unsubscribe())
2511
2507
  */
2512
2508
  onSharedState(handler) {
2513
2509
  this.sharedStateHandlers.push(handler);
2514
- return {
2515
- unsubscribe: () => {
2516
- const idx = this.sharedStateHandlers.indexOf(handler);
2517
- if (idx >= 0) this.sharedStateHandlers.splice(idx, 1);
2518
- }
2519
- };
2510
+ return core.createSubscription(() => {
2511
+ const idx = this.sharedStateHandlers.indexOf(handler);
2512
+ if (idx >= 0) this.sharedStateHandlers.splice(idx, 1);
2513
+ });
2520
2514
  }
2521
2515
  /**
2522
2516
  * Subscribe to player state changes.
2523
2517
  * Called on full sync and on each player_delta.
2524
2518
  *
2525
- * @returns Subscription with unsubscribe()
2519
+ * @returns Subscription (callable & .unsubscribe())
2526
2520
  */
2527
2521
  onPlayerState(handler) {
2528
2522
  this.playerStateHandlers.push(handler);
2529
- return {
2530
- unsubscribe: () => {
2531
- const idx = this.playerStateHandlers.indexOf(handler);
2532
- if (idx >= 0) this.playerStateHandlers.splice(idx, 1);
2533
- }
2534
- };
2523
+ return core.createSubscription(() => {
2524
+ const idx = this.playerStateHandlers.indexOf(handler);
2525
+ if (idx >= 0) this.playerStateHandlers.splice(idx, 1);
2526
+ });
2535
2527
  }
2536
2528
  /**
2537
2529
  * Subscribe to messages of a specific type sent by room.sendMessage().
@@ -2539,169 +2531,137 @@ var RoomClient = class _RoomClient {
2539
2531
  * @example
2540
2532
  * room.onMessage('game_over', (data) => { console.log(data.winner); });
2541
2533
  *
2542
- * @returns Subscription with unsubscribe()
2534
+ * @returns Subscription (callable & .unsubscribe())
2543
2535
  */
2544
2536
  onMessage(messageType, handler) {
2545
2537
  if (!this.messageHandlers.has(messageType)) {
2546
2538
  this.messageHandlers.set(messageType, []);
2547
2539
  }
2548
2540
  this.messageHandlers.get(messageType).push(handler);
2549
- return {
2550
- unsubscribe: () => {
2551
- const handlers = this.messageHandlers.get(messageType);
2552
- if (handlers) {
2553
- const idx = handlers.indexOf(handler);
2554
- if (idx >= 0) handlers.splice(idx, 1);
2555
- }
2541
+ return core.createSubscription(() => {
2542
+ const handlers = this.messageHandlers.get(messageType);
2543
+ if (handlers) {
2544
+ const idx = handlers.indexOf(handler);
2545
+ if (idx >= 0) handlers.splice(idx, 1);
2556
2546
  }
2557
- };
2547
+ });
2558
2548
  }
2559
2549
  /**
2560
2550
  * Subscribe to ALL messages regardless of type.
2561
2551
  *
2562
- * @returns Subscription with unsubscribe()
2552
+ * @returns Subscription (callable & .unsubscribe())
2563
2553
  */
2564
2554
  onAnyMessage(handler) {
2565
2555
  this.allMessageHandlers.push(handler);
2566
- return {
2567
- unsubscribe: () => {
2568
- const idx = this.allMessageHandlers.indexOf(handler);
2569
- if (idx >= 0) this.allMessageHandlers.splice(idx, 1);
2570
- }
2571
- };
2556
+ return core.createSubscription(() => {
2557
+ const idx = this.allMessageHandlers.indexOf(handler);
2558
+ if (idx >= 0) this.allMessageHandlers.splice(idx, 1);
2559
+ });
2572
2560
  }
2573
2561
  /** Subscribe to errors */
2574
2562
  onError(handler) {
2575
2563
  this.errorHandlers.push(handler);
2576
- return {
2577
- unsubscribe: () => {
2578
- const idx = this.errorHandlers.indexOf(handler);
2579
- if (idx >= 0) this.errorHandlers.splice(idx, 1);
2580
- }
2581
- };
2564
+ return core.createSubscription(() => {
2565
+ const idx = this.errorHandlers.indexOf(handler);
2566
+ if (idx >= 0) this.errorHandlers.splice(idx, 1);
2567
+ });
2582
2568
  }
2583
2569
  /** Subscribe to kick events */
2584
2570
  onKicked(handler) {
2585
2571
  this.kickedHandlers.push(handler);
2586
- return {
2587
- unsubscribe: () => {
2588
- const idx = this.kickedHandlers.indexOf(handler);
2589
- if (idx >= 0) this.kickedHandlers.splice(idx, 1);
2590
- }
2591
- };
2572
+ return core.createSubscription(() => {
2573
+ const idx = this.kickedHandlers.indexOf(handler);
2574
+ if (idx >= 0) this.kickedHandlers.splice(idx, 1);
2575
+ });
2592
2576
  }
2593
2577
  onSignal(event, handler) {
2594
2578
  if (!this.signalHandlers.has(event)) {
2595
2579
  this.signalHandlers.set(event, []);
2596
2580
  }
2597
2581
  this.signalHandlers.get(event).push(handler);
2598
- return {
2599
- unsubscribe: () => {
2600
- const handlers = this.signalHandlers.get(event);
2601
- if (!handlers) return;
2602
- const index = handlers.indexOf(handler);
2603
- if (index >= 0) handlers.splice(index, 1);
2604
- }
2605
- };
2582
+ return core.createSubscription(() => {
2583
+ const handlers = this.signalHandlers.get(event);
2584
+ if (!handlers) return;
2585
+ const index = handlers.indexOf(handler);
2586
+ if (index >= 0) handlers.splice(index, 1);
2587
+ });
2606
2588
  }
2607
2589
  onAnySignal(handler) {
2608
2590
  this.anySignalHandlers.push(handler);
2609
- return {
2610
- unsubscribe: () => {
2611
- const index = this.anySignalHandlers.indexOf(handler);
2612
- if (index >= 0) this.anySignalHandlers.splice(index, 1);
2613
- }
2614
- };
2591
+ return core.createSubscription(() => {
2592
+ const index = this.anySignalHandlers.indexOf(handler);
2593
+ if (index >= 0) this.anySignalHandlers.splice(index, 1);
2594
+ });
2615
2595
  }
2616
2596
  onMembersSync(handler) {
2617
2597
  this.memberSyncHandlers.push(handler);
2618
- return {
2619
- unsubscribe: () => {
2620
- const index = this.memberSyncHandlers.indexOf(handler);
2621
- if (index >= 0) this.memberSyncHandlers.splice(index, 1);
2622
- }
2623
- };
2598
+ return core.createSubscription(() => {
2599
+ const index = this.memberSyncHandlers.indexOf(handler);
2600
+ if (index >= 0) this.memberSyncHandlers.splice(index, 1);
2601
+ });
2624
2602
  }
2625
2603
  onMemberJoin(handler) {
2626
2604
  this.memberJoinHandlers.push(handler);
2627
- return {
2628
- unsubscribe: () => {
2629
- const index = this.memberJoinHandlers.indexOf(handler);
2630
- if (index >= 0) this.memberJoinHandlers.splice(index, 1);
2631
- }
2632
- };
2605
+ return core.createSubscription(() => {
2606
+ const index = this.memberJoinHandlers.indexOf(handler);
2607
+ if (index >= 0) this.memberJoinHandlers.splice(index, 1);
2608
+ });
2633
2609
  }
2634
2610
  onMemberLeave(handler) {
2635
2611
  this.memberLeaveHandlers.push(handler);
2636
- return {
2637
- unsubscribe: () => {
2638
- const index = this.memberLeaveHandlers.indexOf(handler);
2639
- if (index >= 0) this.memberLeaveHandlers.splice(index, 1);
2640
- }
2641
- };
2612
+ return core.createSubscription(() => {
2613
+ const index = this.memberLeaveHandlers.indexOf(handler);
2614
+ if (index >= 0) this.memberLeaveHandlers.splice(index, 1);
2615
+ });
2642
2616
  }
2643
2617
  onMemberStateChange(handler) {
2644
2618
  this.memberStateHandlers.push(handler);
2645
- return {
2646
- unsubscribe: () => {
2647
- const index = this.memberStateHandlers.indexOf(handler);
2648
- if (index >= 0) this.memberStateHandlers.splice(index, 1);
2649
- }
2650
- };
2619
+ return core.createSubscription(() => {
2620
+ const index = this.memberStateHandlers.indexOf(handler);
2621
+ if (index >= 0) this.memberStateHandlers.splice(index, 1);
2622
+ });
2651
2623
  }
2652
2624
  onReconnect(handler) {
2653
2625
  this.reconnectHandlers.push(handler);
2654
- return {
2655
- unsubscribe: () => {
2656
- const index = this.reconnectHandlers.indexOf(handler);
2657
- if (index >= 0) this.reconnectHandlers.splice(index, 1);
2658
- }
2659
- };
2626
+ return core.createSubscription(() => {
2627
+ const index = this.reconnectHandlers.indexOf(handler);
2628
+ if (index >= 0) this.reconnectHandlers.splice(index, 1);
2629
+ });
2660
2630
  }
2661
2631
  onConnectionStateChange(handler) {
2662
2632
  this.connectionStateHandlers.push(handler);
2663
- return {
2664
- unsubscribe: () => {
2665
- const index = this.connectionStateHandlers.indexOf(handler);
2666
- if (index >= 0) this.connectionStateHandlers.splice(index, 1);
2667
- }
2668
- };
2633
+ return core.createSubscription(() => {
2634
+ const index = this.connectionStateHandlers.indexOf(handler);
2635
+ if (index >= 0) this.connectionStateHandlers.splice(index, 1);
2636
+ });
2669
2637
  }
2670
2638
  onMediaTrack(handler) {
2671
2639
  this.mediaTrackHandlers.push(handler);
2672
- return {
2673
- unsubscribe: () => {
2674
- const index = this.mediaTrackHandlers.indexOf(handler);
2675
- if (index >= 0) this.mediaTrackHandlers.splice(index, 1);
2676
- }
2677
- };
2640
+ return core.createSubscription(() => {
2641
+ const index = this.mediaTrackHandlers.indexOf(handler);
2642
+ if (index >= 0) this.mediaTrackHandlers.splice(index, 1);
2643
+ });
2678
2644
  }
2679
2645
  onMediaTrackRemoved(handler) {
2680
2646
  this.mediaTrackRemovedHandlers.push(handler);
2681
- return {
2682
- unsubscribe: () => {
2683
- const index = this.mediaTrackRemovedHandlers.indexOf(handler);
2684
- if (index >= 0) this.mediaTrackRemovedHandlers.splice(index, 1);
2685
- }
2686
- };
2647
+ return core.createSubscription(() => {
2648
+ const index = this.mediaTrackRemovedHandlers.indexOf(handler);
2649
+ if (index >= 0) this.mediaTrackRemovedHandlers.splice(index, 1);
2650
+ });
2687
2651
  }
2688
2652
  onMediaStateChange(handler) {
2689
2653
  this.mediaStateHandlers.push(handler);
2690
- return {
2691
- unsubscribe: () => {
2692
- const index = this.mediaStateHandlers.indexOf(handler);
2693
- if (index >= 0) this.mediaStateHandlers.splice(index, 1);
2694
- }
2695
- };
2654
+ return core.createSubscription(() => {
2655
+ const index = this.mediaStateHandlers.indexOf(handler);
2656
+ if (index >= 0) this.mediaStateHandlers.splice(index, 1);
2657
+ });
2696
2658
  }
2697
2659
  onMediaDeviceChange(handler) {
2698
2660
  this.mediaDeviceHandlers.push(handler);
2699
- return {
2700
- unsubscribe: () => {
2701
- const index = this.mediaDeviceHandlers.indexOf(handler);
2702
- if (index >= 0) this.mediaDeviceHandlers.splice(index, 1);
2703
- }
2704
- };
2661
+ return core.createSubscription(() => {
2662
+ const index = this.mediaDeviceHandlers.indexOf(handler);
2663
+ if (index >= 0) this.mediaDeviceHandlers.splice(index, 1);
2664
+ });
2705
2665
  }
2706
2666
  async sendSignal(event, payload, options) {
2707
2667
  if (!this.ws || !this.connected || !this.authenticated) {