shelving 1.56.0 → 1.57.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.
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.56.0",
14
+ "version": "1.57.0",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -1,12 +1,11 @@
1
- import type { Observer } from "../util/index.js";
1
+ import { Observer } from "../util/index.js";
2
2
  import { State } from "./State.js";
3
3
  /**
4
4
  * State that tidies up after itself by completing itself after all its subscribers unsubscribe.
5
5
  * @param delay How long to wait (in ms) before the source subscription is stopped.
6
6
  */
7
7
  export declare class LazyState<T> extends State<T> {
8
- private _delay;
9
- private _timeout?;
10
- constructor(delay?: number);
8
+ private _timeout;
9
+ constructor(delay?: number | null);
11
10
  _removeObserver(observer: Observer<T>): void;
12
11
  }
@@ -1,24 +1,23 @@
1
+ import { Timeout } from "../util/index.js";
1
2
  import { State } from "./State.js";
2
3
  /**
3
4
  * State that tidies up after itself by completing itself after all its subscribers unsubscribe.
4
5
  * @param delay How long to wait (in ms) before the source subscription is stopped.
5
6
  */
6
7
  export class LazyState extends State {
7
- constructor(delay = 0) {
8
+ constructor(delay = null) {
8
9
  super();
9
- this._delay = delay;
10
+ this._timeout = delay ? new Timeout(delay) : null;
10
11
  }
11
12
  // Override to stop the source subscription when the last subscriber unsubscribes.
12
13
  _removeObserver(observer) {
13
14
  super._removeObserver(observer);
14
- if (this._delay) {
15
+ if (this._timeout) {
15
16
  // Maybe stop in a bit (if there are still no subscribers).
16
- if (this._timeout)
17
- clearTimeout(this._timeout);
18
- this._timeout = setTimeout(() => {
17
+ this._timeout.set(() => {
19
18
  if (!this._observers.size && !this.closed)
20
19
  this.complete();
21
- }, this._delay);
20
+ });
22
21
  }
23
22
  else {
24
23
  // Stop now.
package/util/async.d.ts CHANGED
@@ -59,7 +59,3 @@ export declare class Signal extends AbstractPromise<typeof DONE> {
59
59
  /** Send this signal now. */
60
60
  done(): void;
61
61
  }
62
- /** Resolve to `DONE` after a specified delay. */
63
- export declare class Timeout extends AbstractPromise<typeof DONE> {
64
- constructor(ms: number);
65
- }
package/util/async.js CHANGED
@@ -95,10 +95,3 @@ export class Signal extends AbstractPromise {
95
95
  this._resolve(DONE);
96
96
  }
97
97
  }
98
- /** Resolve to `DONE` after a specified delay. */
99
- export class Timeout extends AbstractPromise {
100
- constructor(ms) {
101
- super();
102
- setTimeout(this._resolve, ms, DONE);
103
- }
104
- }
package/util/index.d.ts CHANGED
@@ -31,6 +31,7 @@ export * from "./serialise.js";
31
31
  export * from "./sort.js";
32
32
  export * from "./string.js";
33
33
  export * from "./template.js";
34
+ export * from "./timeout.js";
34
35
  export * from "./transform.js";
35
36
  export * from "./undefined.js";
36
37
  export * from "./units.js";
package/util/index.js CHANGED
@@ -31,6 +31,7 @@ export * from "./serialise.js";
31
31
  export * from "./sort.js";
32
32
  export * from "./string.js";
33
33
  export * from "./template.js";
34
+ export * from "./timeout.js";
34
35
  export * from "./transform.js";
35
36
  export * from "./undefined.js";
36
37
  export * from "./units.js";
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Create a new Timeout.
3
+ *
4
+ * Wrapper for `setTimeout()` that...
5
+ * - Keeps track of the reference returned from `setTimeout()`
6
+ * - Clears the any existing timeout when a new timeout is set.
7
+ * - Allows a default delay to be set that is applied to all new timeouts that don't have a delay set.
8
+ *
9
+ * @param ms The default delay for any created timeouts (in ms).
10
+ */
11
+ export declare class Timeout {
12
+ private _ms;
13
+ private _timeout;
14
+ constructor(ms?: number);
15
+ /**
16
+ * Cancel any existing timeout and set a new one.
17
+ * @param callback
18
+ * @param ms The delay for this timeout (in ms).
19
+ */
20
+ set(callback: () => void, ms?: number): void;
21
+ /** Cancel any existing timeout.. */
22
+ clear(): void;
23
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Create a new Timeout.
3
+ *
4
+ * Wrapper for `setTimeout()` that...
5
+ * - Keeps track of the reference returned from `setTimeout()`
6
+ * - Clears the any existing timeout when a new timeout is set.
7
+ * - Allows a default delay to be set that is applied to all new timeouts that don't have a delay set.
8
+ *
9
+ * @param ms The default delay for any created timeouts (in ms).
10
+ */
11
+ export class Timeout {
12
+ constructor(ms = 0) {
13
+ this._timeout = undefined;
14
+ this._ms = ms;
15
+ }
16
+ /**
17
+ * Cancel any existing timeout and set a new one.
18
+ * @param callback
19
+ * @param ms The delay for this timeout (in ms).
20
+ */
21
+ set(callback, ms = this._ms) {
22
+ this.clear();
23
+ this._timeout = setTimeout(callback, ms);
24
+ }
25
+ /** Cancel any existing timeout.. */
26
+ clear() {
27
+ if (this._timeout)
28
+ this._timeout = void clearTimeout(this._timeout);
29
+ }
30
+ }