@routier/core 0.8.0 → 0.8.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.
@@ -4638,7 +4638,6 @@ const getChannelRegistry = (schemaId, scope)=>{
4638
4638
  registry[key] = channel;
4639
4639
  return channel;
4640
4640
  };
4641
- // Must have a sender and receiver. Sender cannot listen for it's own message
4642
4641
  class SchemaChannel {
4643
4642
  sender;
4644
4643
  receiver;
@@ -4647,8 +4646,8 @@ class SchemaChannel {
4647
4646
  // hence a count rather than a boolean.
4648
4647
  subscribers = 0;
4649
4648
  constructor(channelKey){
4650
- this.sender = new SchemaChannelSender(channelKey);
4651
4649
  this.receiver = new SchemaChannelReceiver(channelKey);
4650
+ this.sender = new SchemaChannelSender(channelKey, this.receiver);
4652
4651
  }
4653
4652
  /**
4654
4653
  * How many callbacks in THIS process are waiting on the channel.
@@ -4672,51 +4671,50 @@ class SchemaChannel {
4672
4671
  this.receiver.close();
4673
4672
  }
4674
4673
  }
4675
- /**
4676
- * Stops a channel from holding the process open.
4677
- *
4678
- * A DataStore opens a sender and a receiver per collection, and in Node an open
4679
- * BroadcastChannel is a referenced handle. Without this, any script that builds a store and
4680
- * does not call `destroyAsync()` runs to the end of its code and then hangs forever with two
4681
- * live MessagePorts per collection — including the example in the README.
4682
- *
4683
- * `unref` is Node-only; the browser's BroadcastChannel has no such method and needs none.
4684
- * A channel that is unreferenced still sends and receives normally. It only stops being a
4685
- * reason for the process to stay alive, which is the correct default for a library: a program
4686
- * with no work left should exit.
4687
- */ const unreference = (channel)=>{
4674
+ const openBroadcastChannel = (channelKey)=>{
4675
+ if (typeof BroadcastChannel !== "function") {
4676
+ return null;
4677
+ }
4678
+ const channel = new BroadcastChannel(`__routier-schema-subscription-channel:${channelKey}`);
4688
4679
  const maybeUnref = channel.unref;
4689
- if (typeof maybeUnref === 'function') {
4680
+ if (typeof maybeUnref === "function") {
4690
4681
  maybeUnref.call(channel);
4691
4682
  }
4683
+ return channel;
4692
4684
  };
4693
4685
  class SchemaChannelSender {
4694
4686
  broadcastChannel;
4695
- constructor(channelKey){
4696
- this.broadcastChannel = new BroadcastChannel(`__routier-schema-subscription-channel:${channelKey}`);
4697
- unreference(this.broadcastChannel);
4687
+ localReceiver;
4688
+ constructor(channelKey, localReceiver){
4689
+ this.broadcastChannel = openBroadcastChannel(channelKey);
4690
+ this.localReceiver = localReceiver;
4698
4691
  }
4699
4692
  send(changes) {
4700
- this.broadcastChannel.postMessage(changes);
4693
+ if (this.broadcastChannel != null) {
4694
+ this.broadcastChannel.postMessage(changes);
4695
+ return;
4696
+ }
4697
+ const copy = structuredClone(changes);
4698
+ setTimeout(()=>this.localReceiver.dispatch(copy), 0);
4701
4699
  }
4702
4700
  close() {
4703
- this.broadcastChannel.close();
4701
+ this.broadcastChannel?.close();
4704
4702
  }
4705
4703
  }
4706
4704
  class SchemaChannelReceiver {
4707
4705
  broadcastChannel;
4708
4706
  subscriptions = [];
4709
4707
  constructor(channelKey){
4710
- this.broadcastChannel = new BroadcastChannel(`__routier-schema-subscription-channel:${channelKey}`);
4711
- unreference(this.broadcastChannel);
4712
- this.broadcastChannel.onmessage = (e)=>{
4713
- // We can't send to the same instance it is not possbile
4714
- const stampedChanges = e.data;
4715
- for(let i = 0, length = this.subscriptions.length; i < length; i++){
4716
- const subscription = this.subscriptions[i];
4717
- subscription.action(stampedChanges);
4718
- }
4719
- };
4708
+ this.broadcastChannel = openBroadcastChannel(channelKey);
4709
+ if (this.broadcastChannel != null) {
4710
+ this.broadcastChannel.onmessage = (e)=>this.dispatch(e.data);
4711
+ }
4712
+ }
4713
+ dispatch(changes) {
4714
+ const subscriptions = this.subscriptions;
4715
+ for(let i = 0, length = subscriptions.length; i < length; i++){
4716
+ subscriptions[i].action(changes);
4717
+ }
4720
4718
  }
4721
4719
  get listenerCount() {
4722
4720
  return this.subscriptions.length;
@@ -4729,8 +4727,10 @@ class SchemaChannelReceiver {
4729
4727
  }
4730
4728
  close() {
4731
4729
  this.subscriptions = [];
4732
- this.broadcastChannel.onmessage = null;
4733
- this.broadcastChannel.close();
4730
+ if (this.broadcastChannel != null) {
4731
+ this.broadcastChannel.onmessage = null;
4732
+ this.broadcastChannel.close();
4733
+ }
4734
4734
  }
4735
4735
  }
4736
4736
  class SubscriptionListener {