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