@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.
package/dist/index.cjs CHANGED
@@ -13264,7 +13264,6 @@ const getChannelRegistry = (schemaId, scope)=>{
13264
13264
  registry[key] = channel;
13265
13265
  return channel;
13266
13266
  };
13267
- // Must have a sender and receiver. Sender cannot listen for it's own message
13268
13267
  class SchemaChannel {
13269
13268
  sender;
13270
13269
  receiver;
@@ -13273,8 +13272,8 @@ class SchemaChannel {
13273
13272
  // hence a count rather than a boolean.
13274
13273
  subscribers = 0;
13275
13274
  constructor(channelKey){
13276
- this.sender = new SchemaChannelSender(channelKey);
13277
13275
  this.receiver = new SchemaChannelReceiver(channelKey);
13276
+ this.sender = new SchemaChannelSender(channelKey, this.receiver);
13278
13277
  }
13279
13278
  /**
13280
13279
  * How many callbacks in THIS process are waiting on the channel.
@@ -13298,51 +13297,50 @@ class SchemaChannel {
13298
13297
  this.receiver.close();
13299
13298
  }
13300
13299
  }
13301
- /**
13302
- * Stops a channel from holding the process open.
13303
- *
13304
- * A DataStore opens a sender and a receiver per collection, and in Node an open
13305
- * BroadcastChannel is a referenced handle. Without this, any script that builds a store and
13306
- * does not call `destroyAsync()` runs to the end of its code and then hangs forever with two
13307
- * live MessagePorts per collection — including the example in the README.
13308
- *
13309
- * `unref` is Node-only; the browser's BroadcastChannel has no such method and needs none.
13310
- * A channel that is unreferenced still sends and receives normally. It only stops being a
13311
- * reason for the process to stay alive, which is the correct default for a library: a program
13312
- * with no work left should exit.
13313
- */ const unreference = (channel)=>{
13300
+ const openBroadcastChannel = (channelKey)=>{
13301
+ if (typeof BroadcastChannel !== "function") {
13302
+ return null;
13303
+ }
13304
+ const channel = new BroadcastChannel(`__routier-schema-subscription-channel:${channelKey}`);
13314
13305
  const maybeUnref = channel.unref;
13315
- if (typeof maybeUnref === 'function') {
13306
+ if (typeof maybeUnref === "function") {
13316
13307
  maybeUnref.call(channel);
13317
13308
  }
13309
+ return channel;
13318
13310
  };
13319
13311
  class SchemaChannelSender {
13320
13312
  broadcastChannel;
13321
- constructor(channelKey){
13322
- this.broadcastChannel = new BroadcastChannel(`__routier-schema-subscription-channel:${channelKey}`);
13323
- unreference(this.broadcastChannel);
13313
+ localReceiver;
13314
+ constructor(channelKey, localReceiver){
13315
+ this.broadcastChannel = openBroadcastChannel(channelKey);
13316
+ this.localReceiver = localReceiver;
13324
13317
  }
13325
13318
  send(changes) {
13326
- this.broadcastChannel.postMessage(changes);
13319
+ if (this.broadcastChannel != null) {
13320
+ this.broadcastChannel.postMessage(changes);
13321
+ return;
13322
+ }
13323
+ const copy = structuredClone(changes);
13324
+ setTimeout(()=>this.localReceiver.dispatch(copy), 0);
13327
13325
  }
13328
13326
  close() {
13329
- this.broadcastChannel.close();
13327
+ this.broadcastChannel?.close();
13330
13328
  }
13331
13329
  }
13332
13330
  class SchemaChannelReceiver {
13333
13331
  broadcastChannel;
13334
13332
  subscriptions = [];
13335
13333
  constructor(channelKey){
13336
- this.broadcastChannel = new BroadcastChannel(`__routier-schema-subscription-channel:${channelKey}`);
13337
- unreference(this.broadcastChannel);
13338
- this.broadcastChannel.onmessage = (e)=>{
13339
- // We can't send to the same instance it is not possbile
13340
- const stampedChanges = e.data;
13341
- for(let i = 0, length = this.subscriptions.length; i < length; i++){
13342
- const subscription = this.subscriptions[i];
13343
- subscription.action(stampedChanges);
13344
- }
13345
- };
13334
+ this.broadcastChannel = openBroadcastChannel(channelKey);
13335
+ if (this.broadcastChannel != null) {
13336
+ this.broadcastChannel.onmessage = (e)=>this.dispatch(e.data);
13337
+ }
13338
+ }
13339
+ dispatch(changes) {
13340
+ const subscriptions = this.subscriptions;
13341
+ for(let i = 0, length = subscriptions.length; i < length; i++){
13342
+ subscriptions[i].action(changes);
13343
+ }
13346
13344
  }
13347
13345
  get listenerCount() {
13348
13346
  return this.subscriptions.length;
@@ -13355,8 +13353,10 @@ class SchemaChannelReceiver {
13355
13353
  }
13356
13354
  close() {
13357
13355
  this.subscriptions = [];
13358
- this.broadcastChannel.onmessage = null;
13359
- this.broadcastChannel.close();
13356
+ if (this.broadcastChannel != null) {
13357
+ this.broadcastChannel.onmessage = null;
13358
+ this.broadcastChannel.close();
13359
+ }
13360
13360
  }
13361
13361
  }
13362
13362
  class SubscriptionListener {