@dxos/react-ui-graph 0.9.1-staging.ee54ba693a → 0.11.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.
@@ -0,0 +1,61 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ //#region src/hooks/useStateRef.ts
3
+ /**
4
+ * Extension of useState to return an up-to-date reference.
5
+ * E.g., to use in callbacks where the state value is stale.
6
+ * @param initialValue
7
+ *
8
+ * @deprecated This function has been deprecated.
9
+ */
10
+ var useStateRef = (initialValue) => {
11
+ const [value, setValue] = useState(initialValue);
12
+ const ref = useRef(null);
13
+ useEffect(() => {
14
+ ref.current = value;
15
+ }, [initialValue, value]);
16
+ return [
17
+ value,
18
+ setValue,
19
+ ref
20
+ ];
21
+ };
22
+ /**
23
+ * Extension of useRef that contains computed values based on dependencies.
24
+ * E.g., to use in callbacks where the state value is stale.
25
+ * @param initialValue
26
+ * @param deps
27
+ */
28
+ var useDynamicRef = (initialValue, deps) => {
29
+ const [, setValue] = useState(initialValue);
30
+ const ref = useRef(initialValue());
31
+ useEffect(() => {
32
+ ref.current = initialValue();
33
+ setValue(ref.current);
34
+ }, deps);
35
+ return ref;
36
+ };
37
+ /**
38
+ * State setters to force repaint.
39
+ * @param deps
40
+ */
41
+ var useTimestamp = (deps) => {
42
+ const [{ timestamp, previous }, setTimestamp] = useState({ timestamp: Date.now() });
43
+ useEffect(() => {
44
+ setTimestamp({
45
+ timestamp: Date.now(),
46
+ previous: timestamp
47
+ });
48
+ }, deps ?? []);
49
+ return [
50
+ timestamp,
51
+ () => setTimestamp({
52
+ timestamp: Date.now(),
53
+ previous: timestamp
54
+ }),
55
+ previous
56
+ ];
57
+ };
58
+ //#endregion
59
+ export { useStateRef as n, useTimestamp as r, useDynamicRef as t };
60
+
61
+ //# sourceMappingURL=chunk-useStateRef.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunk-useStateRef.mjs","names":[],"sources":["../../src/hooks/useStateRef.ts"],"sourcesContent":["//\n// Copyright 2021 DXOS.org\n//\n\nimport { type Dispatch, type RefObject, type SetStateAction, useEffect, useRef, useState } from 'react';\n\n/**\n * Extension of useState to return an up-to-date reference.\n * E.g., to use in callbacks where the state value is stale.\n * @param initialValue\n *\n * @deprecated This function has been deprecated.\n */\n// TODO(burdon): Remove.\nexport const useStateRef = <V>(\n initialValue?: V | (() => V),\n): [V | undefined, Dispatch<SetStateAction<V | undefined>>, RefObject<V | undefined>] => {\n const [value, setValue] = useState<V | undefined>(initialValue);\n const ref = useRef<V>(null);\n useEffect(() => {\n ref.current = value;\n }, [initialValue, value]);\n\n return [value, setValue, ref];\n};\n\n/**\n * Extension of useRef that contains computed values based on dependencies.\n * E.g., to use in callbacks where the state value is stale.\n * @param initialValue\n * @param deps\n */\n// TODO(burdon): Remove.\nexport const useDynamicRef = <V>(initialValue: () => V, deps: any[]): RefObject<V> => {\n const [, setValue] = useState<V>(initialValue);\n const ref = useRef<V>(initialValue());\n useEffect(() => {\n ref.current = initialValue();\n // Must update state to trigger render cycle.\n setValue(ref.current);\n }, deps);\n\n return ref;\n};\n\n/**\n * State setters to force repaint.\n * @param deps\n */\nexport const useTimestamp = (deps?: any[]): [number, () => void, number?] => {\n const [{ timestamp, previous }, setTimestamp] = useState<{\n timestamp: number;\n previous?: number;\n }>({ timestamp: Date.now() });\n useEffect(() => {\n setTimestamp({ timestamp: Date.now(), previous: timestamp });\n }, deps ?? []);\n\n return [timestamp, () => setTimestamp({ timestamp: Date.now(), previous: timestamp }), previous];\n};\n"],"mappings":";;;;;;;;;AAcA,IAAa,eACX,iBACuF;CACvF,MAAM,CAAC,OAAO,YAAY,SAAwB,YAAY;CAC9D,MAAM,MAAM,OAAU,IAAI;CAC1B,gBAAgB;EACd,IAAI,UAAU;CAChB,GAAG,CAAC,cAAc,KAAK,CAAC;CAExB,OAAO;EAAC;EAAO;EAAU;CAAG;AAC9B;;;;;;;AASA,IAAa,iBAAoB,cAAuB,SAA8B;CACpF,MAAM,GAAG,YAAY,SAAY,YAAY;CAC7C,MAAM,MAAM,OAAU,aAAa,CAAC;CACpC,gBAAgB;EACd,IAAI,UAAU,aAAa;EAE3B,SAAS,IAAI,OAAO;CACtB,GAAG,IAAI;CAEP,OAAO;AACT;;;;;AAMA,IAAa,gBAAgB,SAAgD;CAC3E,MAAM,CAAC,EAAE,WAAW,YAAY,gBAAgB,SAG7C,EAAE,WAAW,KAAK,IAAI,EAAE,CAAC;CAC5B,gBAAgB;EACd,aAAa;GAAE,WAAW,KAAK,IAAI;GAAG,UAAU;EAAU,CAAC;CAC7D,GAAG,QAAQ,CAAC,CAAC;CAEb,OAAO;EAAC;QAAiB,aAAa;GAAE,WAAW,KAAK,IAAI;GAAG,UAAU;EAAU,CAAC;EAAG;CAAQ;AACjG"}