@oscarpalmer/mora 0.33.0 → 0.34.0

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.
Files changed (2) hide show
  1. package/dist/mora.full.mjs +68 -56
  2. package/package.json +2 -2
@@ -464,13 +464,54 @@ const ABORT_EVENT = "abort";
464
464
  const ABORT_OPTIONS = { once: true };
465
465
  //#endregion
466
466
  //#region node_modules/@oscarpalmer/atoms/dist/internal/subscription.mjs
467
- function addSubscription(subscriptions, subscription, state) {
468
- const { items, values } = subscriptions;
467
+ const SUBSCRIPTION_NAME = "subscription";
468
+ const SUBSCRIPTION_PROPERTY = "$subscription";
469
+ const SUBSCRIPTION_STORE = "subscriptions";
470
+ function Subscription(state, property, parameters) {
471
+ Object.defineProperty(this, SUBSCRIPTION_SYMBOL, { value: state });
472
+ state.aborter = createAborter(parameters.signal, () => this.unsubscribe(subscriptions, this, state));
473
+ Object.defineProperties(this, {
474
+ [property.key]: { value: property.value },
475
+ active: {
476
+ enumerable: true,
477
+ get: () => (state.parameters?.isActive?.() ?? true) && state.active
478
+ }
479
+ });
480
+ addSubscription(this);
481
+ }
482
+ Object.defineProperties(Subscription.prototype, {
483
+ [SUBSCRIPTION_PROPERTY]: { value: SUBSCRIPTION_NAME },
484
+ unsubscribe: { value: removeSubscription }
485
+ });
486
+ function Subscriptions(parameters) {
487
+ const { keys, property } = createSubscriptionsParameters(parameters);
488
+ Object.defineProperty(this, SUBSCRIPTION_SYMBOL, { value: createSubscriptionsState(property, keys) });
489
+ }
490
+ Object.defineProperties(Subscriptions.prototype, {
491
+ [SUBSCRIPTION_PROPERTY]: { value: SUBSCRIPTION_STORE },
492
+ clear: { value: clearSubscriptions },
493
+ create: { value: createSubscription },
494
+ items: {
495
+ enumerable: true,
496
+ get() {
497
+ return this[SUBSCRIPTION_SYMBOL].items;
498
+ }
499
+ },
500
+ values: {
501
+ enumerable: true,
502
+ get() {
503
+ return this[SUBSCRIPTION_SYMBOL].values;
504
+ }
505
+ }
506
+ });
507
+ function addSubscription(instance) {
508
+ const state = instance[SUBSCRIPTION_SYMBOL];
469
509
  const { key, value } = state.parameters;
510
+ const { items, values } = state.subscriptions;
470
511
  if (!isKey(key)) {
471
- items.any.add(subscription);
472
- values.from.any.set(value, subscription);
473
- values.to.any.set(subscription, value);
512
+ items.any.add(instance);
513
+ values.from.any.set(value, instance);
514
+ values.to.any.set(instance, value);
474
515
  return;
475
516
  }
476
517
  /* istanbul ignore if */
@@ -482,29 +523,32 @@ function addSubscription(subscriptions, subscription, state) {
482
523
  keyedItems = /* @__PURE__ */ new Set();
483
524
  items.keyed.set(key, keyedItems);
484
525
  }
485
- keyedItems.add(subscription);
526
+ keyedItems.add(instance);
486
527
  let keyedFrom = values.from.keyed.get(key);
487
528
  if (keyedFrom == null) {
488
529
  keyedFrom = /* @__PURE__ */ new Map();
489
530
  values.from.keyed.set(key, keyedFrom);
490
531
  }
491
- keyedFrom.set(value, subscription);
532
+ keyedFrom.set(value, instance);
492
533
  let keyedTo = values.to.keyed.get(key);
493
534
  if (keyedTo == null) {
494
535
  keyedTo = /* @__PURE__ */ new Map();
495
536
  values.to.keyed.set(key, keyedTo);
496
537
  }
497
- keyedTo.set(subscription, value);
538
+ keyedTo.set(instance, value);
498
539
  }
499
- function clearSubscriptions(store) {
500
- for (const susbcription of store.values.to.any.keys()) susbcription.unsubscribe();
501
- if (store.values.to.keyed != null) for (const values of store.values.to.keyed.values()) for (const susbcription of values.keys()) susbcription.unsubscribe();
540
+ function clearSubscriptions() {
541
+ const state = this[SUBSCRIPTION_SYMBOL];
542
+ for (const susbcription of state.values.to.any.keys()) susbcription.unsubscribe();
543
+ if (state.values.to.keyed != null && state.values.to.keyed.size > 0) for (const values of state.values.to.keyed.values()) for (const susbcription of values.keys()) susbcription.unsubscribe();
502
544
  }
503
- function createSubscription(subscriptions, property, params) {
545
+ function createSubscription(params) {
504
546
  const parameters = createSubscriptionParameters(params);
505
547
  if (parameters.signal?.aborted ?? false) throw new Error(parameters.signal?.reason);
548
+ const subscriptions = this[SUBSCRIPTION_SYMBOL];
506
549
  const state = {
507
550
  parameters,
551
+ subscriptions,
508
552
  active: true
509
553
  };
510
554
  const existing = getExistingSubscription(subscriptions, state);
@@ -513,18 +557,7 @@ function createSubscription(subscriptions, property, params) {
513
557
  if (subscriptions.keys === false) throw new Error(SUBSCRIPTION_DISALLOWED_KEY);
514
558
  else if (subscriptions.keys !== true && !subscriptions.keys.has(parameters.key)) throw new Error(SUBSCRIPTION_INVALID_KEY);
515
559
  }
516
- state.aborter = createAborter(parameters.signal, () => unsubscribe(subscriptions, instance, state));
517
- const instance = { unsubscribe: () => unsubscribe(subscriptions, instance, state) };
518
- Object.defineProperties(instance, {
519
- [SUBSCRIPTION_PROPERTY]: { value: SUBSCRIPTION_NAME },
520
- [property.key]: { value: property.value },
521
- active: {
522
- enumerable: true,
523
- get: () => (parameters.isActive?.() ?? true) && state.active
524
- }
525
- });
526
- addSubscription(subscriptions, instance, state);
527
- return [Object.freeze(instance), false];
560
+ return [new Subscription(state, subscriptions.property, parameters), false];
528
561
  }
529
562
  function getExistingSubscription(subscriptions, state) {
530
563
  const { values } = subscriptions;
@@ -558,9 +591,10 @@ function createSubscriptionsProperty(input) {
558
591
  value: values.value == null ? SUBSCRIPTION_NAME : values.value
559
592
  };
560
593
  }
561
- function createSubscriptionsState(keys) {
594
+ function createSubscriptionsState(property, keys) {
562
595
  return {
563
596
  keys,
597
+ property,
564
598
  items: {
565
599
  any: /* @__PURE__ */ new Set(),
566
600
  keyed: keys === false ? void 0 : /* @__PURE__ */ new Map()
@@ -587,16 +621,17 @@ function removeFromStore(items, values, subscription, value, key) {
587
621
  values.to.keyed?.get(key)?.delete(subscription);
588
622
  }
589
623
  }
590
- function removeSubscription(subscriptionsState, subscription, state) {
624
+ function removeSubscription() {
625
+ const state = this[SUBSCRIPTION_SYMBOL];
591
626
  if (!state.active) return;
592
627
  state.aborter = void 0;
593
628
  state.active = false;
594
- state.parameters.isActive = void 0;
595
- state.parameters.signal = void 0;
596
- const { items, values } = subscriptionsState;
629
+ const { items, values } = state.subscriptions;
597
630
  const { key, value } = state.parameters;
631
+ state.parameters = void 0;
632
+ state.subscriptions = void 0;
598
633
  if (!isKey(key)) {
599
- removeFromStore(items.any, values, subscription, value);
634
+ removeFromStore(items.any, values, this, value);
600
635
  return;
601
636
  }
602
637
  const keyed = items.keyed?.get(key);
@@ -604,7 +639,7 @@ function removeSubscription(subscriptionsState, subscription, state) {
604
639
  if (items.keyed == null || keyed == null)
605
640
  // istanbul ignore next
606
641
  return;
607
- removeFromStore(keyed, values, subscription, value, key);
642
+ removeFromStore(keyed, values, this, value, key);
608
643
  if (keyed.size === 0) items.keyed.delete(key);
609
644
  }
610
645
  /**
@@ -614,35 +649,12 @@ function removeSubscription(subscriptionsState, subscription, state) {
614
649
  * @returns Subscription store
615
650
  */
616
651
  function subscriptions(parameters) {
617
- const { keys, property } = createSubscriptionsParameters(parameters);
618
- const state = createSubscriptionsState(keys);
619
- const instance = {
620
- clear: () => clearSubscriptions(state),
621
- create: (parameters) => createSubscription(state, property, parameters)
622
- };
623
- Object.defineProperties(instance, {
624
- [SUBSCRIPTION_PROPERTY]: { value: SUBSCRIPTION_STORE },
625
- items: {
626
- enumerable: true,
627
- value: state.items
628
- },
629
- values: {
630
- enumerable: true,
631
- value: state.values
632
- }
633
- });
634
- return Object.freeze(instance);
635
- }
636
- function unsubscribe(subscriptionsState, subscription, state) {
637
- state.aborter?.cancel();
638
- removeSubscription(subscriptionsState, subscription, state);
652
+ return new Subscriptions(parameters);
639
653
  }
640
654
  const SUBSCRIPTION_DISALLOWED_KEY = "Disallowed key for subscription";
641
655
  const SUBSCRIPTION_INVALID_KEY = "Invalid key for subscription";
642
656
  const SUBSCRIPTION_INVALID_VALUE = "A subscription requires a non-null value to identify it";
643
- const SUBSCRIPTION_NAME = "subscription";
644
- const SUBSCRIPTION_PROPERTY = "$subscriptions";
645
- const SUBSCRIPTION_STORE = "subscriptions";
657
+ const SUBSCRIPTION_SYMBOL = Symbol(SUBSCRIPTION_PROPERTY);
646
658
  //#endregion
647
659
  //#region src/helpers/subscription.ts
648
660
  function subscribeToProxy(first, second, third) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/mora",
3
- "version": "0.33.0",
3
+ "version": "0.34.0",
4
4
  "description": "Signals and stuff…",
5
5
  "keywords": [
6
6
  "reactive",
@@ -39,7 +39,7 @@
39
39
  "test:leak": "vpx vp test run --detect-async-leaks --coverage"
40
40
  },
41
41
  "dependencies": {
42
- "@oscarpalmer/atoms": "^0.202"
42
+ "@oscarpalmer/atoms": "^0.203"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/node": "^26.5",