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