shelving 1.111.0 → 1.112.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.111.0",
14
+ "version": "1.112.0",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -1,8 +1,4 @@
1
1
  /// <reference types="react" />
2
+ import type { Arguments } from "../util/function.js";
2
3
  import { type Start } from "../util/start.js";
3
- /**
4
- * Call a `StartCallback` when an element is mounted, and the returned `StopCallback` when it's unmounted again.
5
- * - Takes care of state so `start()` won't be called twice for the same element.
6
- * - Returns a `RefCallback` which should be set as `ref={ref}` on the target element.
7
- */
8
- export declare function useMount<T extends Element>(start: Start<[T]>): React.RefCallback<T>;
4
+ export declare function useMount<T extends Element, A extends Arguments = []>(start: Start<[T, ...A]>, ...args: A): React.RefCallback<T>;
package/react/useMount.js CHANGED
@@ -1,23 +1,25 @@
1
1
  import { useRef } from "react";
2
+ import { isArrayEqual } from "../util/equal.js";
2
3
  import { Starter } from "../util/start.js";
3
- /**
4
- * Call a `StartCallback` when an element is mounted, and the returned `StopCallback` when it's unmounted again.
5
- * - Takes care of state so `start()` won't be called twice for the same element.
6
- * - Returns a `RefCallback` which should be set as `ref={ref}` on the target element.
7
- */
8
- export function useMount(start) {
9
- const ref = useRef();
10
- if (!ref.current) {
11
- let current;
12
- const starter = new Starter(start);
13
- ref.current = (next) => {
14
- if (current !== next) {
15
- starter.stop();
16
- current = next;
17
- if (current)
18
- starter.start(current);
4
+ export function useMount(start, ...args) {
5
+ const internals = (useRef().current ||= {
6
+ args,
7
+ current: null,
8
+ starter: new Starter(start),
9
+ ref(next) {
10
+ if (internals.current !== next) {
11
+ internals.starter.stop();
12
+ internals.current = next;
13
+ if (internals.current)
14
+ internals.starter.start(internals.current, ...internals.args);
19
15
  }
20
- };
16
+ },
17
+ });
18
+ if (!isArrayEqual(args, internals.args)) {
19
+ internals.args = args;
20
+ internals.starter.stop();
21
+ if (internals.current)
22
+ internals.starter.start(internals.current, ...internals.args);
21
23
  }
22
- return ref.current;
24
+ return internals.ref;
23
25
  }
package/util/start.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Disposable } from "./dispose.js";
2
2
  import type { Arguments } from "./function.js";
3
3
  /** Callback function that starts something with multiple values and returns an optional stop callback. */
4
- export type Start<T extends Arguments = []> = (...values: T) => Stop | undefined;
4
+ export type Start<T extends Arguments = []> = (...values: T) => Stop | void;
5
5
  /** Callback function that stops something. */
6
6
  export type Stop = () => void;
7
7
  /**