shelving 1.55.0 → 1.57.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/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.55.0",
14
+ "version": "1.57.1",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -9,26 +9,26 @@ export class ArrayState extends State {
9
9
  }
10
10
  /** Get the length of the current value of this state. */
11
11
  get length() {
12
- return this.value.length;
12
+ return this._value.length;
13
13
  }
14
14
  /** Add an item to this array. */
15
15
  add(item) {
16
- this.next(withItem(this.value, item));
16
+ this.next(withItem(this._value, item));
17
17
  }
18
18
  /** Remove an item from this array. */
19
19
  delete(item) {
20
- this.next(withoutItem(this.value, item));
20
+ this.next(withoutItem(this._value, item));
21
21
  }
22
22
  /** Swap an item in this array with a different item. */
23
23
  swap(oldItem, newItem) {
24
- this.next(swapItem(this.value, oldItem, newItem));
24
+ this.next(swapItem(this._value, oldItem, newItem));
25
25
  }
26
26
  /** Toggle an item in this array. */
27
27
  toggle(item) {
28
- this.next(toggleItem(this.value, item));
28
+ this.next(toggleItem(this._value, item));
29
29
  }
30
30
  /** Iterate over the items. */
31
31
  [Symbol.iterator]() {
32
- return this.value.values();
32
+ return this._value.values();
33
33
  }
34
34
  }
@@ -0,0 +1,7 @@
1
+ import { State } from "./State.js";
2
+ /** State that stores a boolean and has additional methods to help with that. */
3
+ export declare class BooleanState extends State<boolean> {
4
+ _value: boolean;
5
+ /** Toggle the current boolean value. */
6
+ toggle(): void;
7
+ }
@@ -0,0 +1,13 @@
1
+ import { State } from "./State.js";
2
+ /** State that stores a boolean and has additional methods to help with that. */
3
+ export class BooleanState extends State {
4
+ constructor() {
5
+ super(...arguments);
6
+ // Set default value to be false.
7
+ this._value = false;
8
+ }
9
+ /** Toggle the current boolean value. */
10
+ toggle() {
11
+ this.next(this._value ? false : true);
12
+ }
13
+ }
@@ -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.
@@ -0,0 +1,14 @@
1
+ import { Entry, ImmutableObject } from "../util/index.js";
2
+ import { State } from "./State.js";
3
+ /** State that stores a map-like object and has additional methods to help with that. */
4
+ export declare class ObjectState<T> extends State<ImmutableObject<T>> implements Iterable<Entry<T>> {
5
+ _value: {};
6
+ /** Get the length of the current value of this state. */
7
+ get length(): number;
8
+ /** Remove a named entry from this object. */
9
+ delete(key: string): void;
10
+ /** Set a named entry in this object with a different value. */
11
+ set(key: string, value: T): void;
12
+ /** Iterate over the items. */
13
+ [Symbol.iterator](): Iterator<Entry<T>, void>;
14
+ }
@@ -0,0 +1,26 @@
1
+ import { withEntry, withoutEntry } from "../util/index.js";
2
+ import { State } from "./State.js";
3
+ /** State that stores a map-like object and has additional methods to help with that. */
4
+ export class ObjectState extends State {
5
+ constructor() {
6
+ super(...arguments);
7
+ // Set default value to be empty object.
8
+ this._value = {};
9
+ }
10
+ /** Get the length of the current value of this state. */
11
+ get length() {
12
+ return Object.keys(this._value).length;
13
+ }
14
+ /** Remove a named entry from this object. */
15
+ delete(key) {
16
+ this.next(withoutEntry(this._value, key));
17
+ }
18
+ /** Set a named entry in this object with a different value. */
19
+ set(key, value) {
20
+ this.next(withEntry(this._value, key, value));
21
+ }
22
+ /** Iterate over the items. */
23
+ [Symbol.iterator]() {
24
+ return Object.entries(this._value)[Symbol.iterator]();
25
+ }
26
+ }
package/stream/index.d.ts CHANGED
@@ -3,5 +3,7 @@ export * from "./LazyStream.js";
3
3
  export * from "./LastStream.js";
4
4
  export * from "./State.js";
5
5
  export * from "./LazyState.js";
6
+ export * from "./BooleanState.js";
6
7
  export * from "./ArrayState.js";
8
+ export * from "./ObjectState.js";
7
9
  export * from "./DataState.js";
package/stream/index.js CHANGED
@@ -3,5 +3,7 @@ export * from "./LazyStream.js";
3
3
  export * from "./LastStream.js";
4
4
  export * from "./State.js";
5
5
  export * from "./LazyState.js";
6
+ export * from "./BooleanState.js";
6
7
  export * from "./ArrayState.js";
8
+ export * from "./ObjectState.js";
7
9
  export * from "./DataState.js";
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";
@@ -40,7 +40,7 @@ export interface Observer<T> {
40
40
  */
41
41
  readonly from?: (source: Subscribable<T>) => this;
42
42
  /** Receive the next value. */
43
- readonly next: Dispatcher<[T]>;
43
+ readonly next?: Dispatcher<[T]>;
44
44
  /** End the subscription with an error. */
45
45
  readonly error?: Handler;
46
46
  /** End the subscription with success. */
@@ -17,8 +17,9 @@ export function subscribe(source, target) {
17
17
  }
18
18
  /** Dispatch the next value to an observer (and if the next value errors, calls a handler). */
19
19
  export function dispatchNext(observer, value) {
20
+ var _a;
20
21
  try {
21
- observer.next(value);
22
+ (_a = observer.next) === null || _a === void 0 ? void 0 : _a.call(observer, value);
22
23
  }
23
24
  catch (thrown) {
24
25
  logError(thrown);
@@ -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
+ }