@scarlett-player/core 0.5.0 → 0.5.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.
package/dist/index.js CHANGED
@@ -9,8 +9,19 @@ function setCurrentEffect(effect2) {
9
9
  function getCurrentEffect() {
10
10
  return effectContext.current;
11
11
  }
12
+ const effectCleanups = /* @__PURE__ */ new WeakMap();
13
+ function trackEffectSubscription(effectFn, unsubscribe) {
14
+ let cleanups = effectCleanups.get(effectFn);
15
+ if (!cleanups) {
16
+ cleanups = /* @__PURE__ */ new Set();
17
+ effectCleanups.set(effectFn, cleanups);
18
+ }
19
+ cleanups.add(unsubscribe);
20
+ }
12
21
  function effect(fn) {
22
+ let disposed = false;
13
23
  const execute = () => {
24
+ if (disposed) return;
14
25
  setCurrentEffect(execute);
15
26
  try {
16
27
  fn();
@@ -23,6 +34,13 @@ function effect(fn) {
23
34
  };
24
35
  execute();
25
36
  return () => {
37
+ disposed = true;
38
+ const cleanups = effectCleanups.get(execute);
39
+ if (cleanups) {
40
+ cleanups.forEach((unsub) => unsub());
41
+ cleanups.clear();
42
+ effectCleanups.delete(execute);
43
+ }
26
44
  };
27
45
  }
28
46
  class Signal {
@@ -37,7 +55,9 @@ class Signal {
37
55
  */
38
56
  get() {
39
57
  if (currentEffect) {
40
- this.subscribers.add(currentEffect);
58
+ const effect2 = currentEffect;
59
+ this.subscribers.add(effect2);
60
+ trackEffectSubscription(effect2, () => this.subscribers.delete(effect2));
41
61
  }
42
62
  return this.value;
43
63
  }
@@ -135,7 +155,9 @@ class Computed {
135
155
  }
136
156
  }
137
157
  if (currentEffect) {
138
- this.subscribers.add(currentEffect);
158
+ const effect2 = currentEffect;
159
+ this.subscribers.add(effect2);
160
+ trackEffectSubscription(effect2, () => this.subscribers.delete(effect2));
139
161
  }
140
162
  return this.value;
141
163
  }
@@ -1604,6 +1626,7 @@ class ScarlettPlayer {
1604
1626
  this.destroyed = false;
1605
1627
  this.seekingWhilePlaying = false;
1606
1628
  this.seekResumeTimeout = null;
1629
+ this.loadGeneration = 0;
1607
1630
  if (typeof options.container === "string") {
1608
1631
  const el = document.querySelector(options.container);
1609
1632
  if (!el || !(el instanceof HTMLElement)) {
@@ -1677,6 +1700,7 @@ class ScarlettPlayer {
1677
1700
  */
1678
1701
  async load(source) {
1679
1702
  this.checkDestroyed();
1703
+ const generation = ++this.loadGeneration;
1680
1704
  try {
1681
1705
  this.logger.info("Loading source", { source });
1682
1706
  this.stateManager.update({
@@ -1695,6 +1719,10 @@ class ScarlettPlayer {
1695
1719
  await this.pluginManager.destroyPlugin(previousProviderId);
1696
1720
  this._currentProvider = null;
1697
1721
  }
1722
+ if (generation !== this.loadGeneration) {
1723
+ this.logger.info("Load superseded by newer load call", { source });
1724
+ return;
1725
+ }
1698
1726
  const provider = this.pluginManager.selectProvider(source);
1699
1727
  if (!provider) {
1700
1728
  this.errorHandler.throw(
@@ -1710,18 +1738,28 @@ class ScarlettPlayer {
1710
1738
  this._currentProvider = provider;
1711
1739
  this.logger.info("Provider selected", { provider: provider.id });
1712
1740
  await this.pluginManager.initPlugin(provider.id);
1741
+ if (generation !== this.loadGeneration) {
1742
+ this.logger.info("Load superseded by newer load call", { source });
1743
+ return;
1744
+ }
1713
1745
  this.stateManager.set("source", { src: source, type: this.detectMimeType(source) });
1714
1746
  if (typeof provider.loadSource === "function") {
1715
1747
  await provider.loadSource(source);
1716
1748
  }
1749
+ if (generation !== this.loadGeneration) {
1750
+ this.logger.info("Load superseded by newer load call", { source });
1751
+ return;
1752
+ }
1717
1753
  if (this.stateManager.getValue("autoplay")) {
1718
1754
  await this.play();
1719
1755
  }
1720
1756
  } catch (error) {
1721
- this.errorHandler.handle(error, {
1722
- operation: "load",
1723
- source
1724
- });
1757
+ if (generation === this.loadGeneration) {
1758
+ this.errorHandler.handle(error, {
1759
+ operation: "load",
1760
+ source
1761
+ });
1762
+ }
1725
1763
  }
1726
1764
  }
1727
1765
  /**