@rerun-io/web-viewer-react 0.12.0-rc.3 → 0.12.0-rc.4

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.
Files changed (4) hide show
  1. package/README.md +1 -1
  2. package/index.d.ts +23 -15
  3. package/index.js +73 -44
  4. package/package.json +2 -2
package/README.md CHANGED
@@ -35,7 +35,7 @@ export default function App() {
35
35
  ```
36
36
 
37
37
  The `rrd` in the snippet above should be a URL pointing to either:
38
- - A hosted `.rrd` file, such as <https://app.rerun.io/version/0.12.0-rc.3/examples/dna.rrd>
38
+ - A hosted `.rrd` file, such as <https://app.rerun.io/version/0.12.0-rc.4/examples/dna.rrd>
39
39
  - A WebSocket connection to the SDK opened via the [`serve`](https://www.rerun.io/docs/reference/sdk-operating-modes#serve) API
40
40
 
41
41
  If `rrd` is not set, the viewer will display the same welcome screen as <https://app.rerun.io>.
package/index.d.ts CHANGED
@@ -4,25 +4,33 @@ declare module '@rerun-io/web-viewer-react' {
4
4
  *
5
5
  * This component creates and manages the web viewer's `canvas` element.
6
6
  *
7
- * The web viewer is restarted each time `rrd` changes.
8
- * Starting the web viewer is an expensive operation, so be careful with changing it too often!
9
- *
10
- * */
11
- export default function WebViewer(props: Props): React.DetailedReactHTMLElement<{
12
- className: string;
13
- style: {
14
- width: string;
15
- height: string;
16
- position: "relative";
17
- };
18
- ref: React.RefObject<HTMLDivElement>;
19
- }, HTMLDivElement>;
7
+ *
8
+ */
9
+ export default class WebViewer extends React.Component<Props, any, any> {
10
+
11
+ constructor(props: Props);
12
+ componentDidMount(): void;
13
+ componentDidUpdate(prevProps: Props): void;
14
+ componentWillUnmount(): void;
15
+ render(): React.DetailedReactHTMLElement<{
16
+ className: string;
17
+ style: {
18
+ width: string;
19
+ height: string;
20
+ position: "relative";
21
+ };
22
+ ref: React.RefObject<HTMLDivElement>;
23
+ }, HTMLDivElement>;
24
+ #private;
25
+ }
20
26
  export type React = typeof React;
21
27
  export type Props = {
22
28
  /**
23
- * URL of the `.rrd` file to load
29
+ * URL(s) of the `.rrd` file(s) to load.
30
+ * Changing this prop will open any new unique URLs as recordings,
31
+ * and close any URLs which are not present.
24
32
  */
25
- rrd: string;
33
+ rrd: string | string[];
26
34
  /**
27
35
  * CSS width of the viewer's parent div
28
36
  */
package/index.js CHANGED
@@ -1,11 +1,13 @@
1
- import React, { useEffect, useRef } from "react";
1
+ import React, { createRef } from "react";
2
2
  import * as rerun from "@rerun-io/web-viewer";
3
3
 
4
4
  /** @typedef {import("react")} React */
5
5
 
6
6
  /**
7
7
  * @typedef Props
8
- * @property {string} rrd URL of the `.rrd` file to load
8
+ * @property {string | string[]} rrd URL(s) of the `.rrd` file(s) to load.
9
+ * Changing this prop will open any new unique URLs as recordings,
10
+ * and close any URLs which are not present.
9
11
  * @property {string} [width] CSS width of the viewer's parent div
10
12
  * @property {string} [height] CSS height of the viewer's parent div
11
13
  */
@@ -15,48 +17,75 @@ import * as rerun from "@rerun-io/web-viewer";
15
17
  *
16
18
  * This component creates and manages the web viewer's `canvas` element.
17
19
  *
18
- * The web viewer is restarted each time `rrd` changes.
19
- * Starting the web viewer is an expensive operation, so be careful with changing it too often!
20
+ * @extends {React.Component<Props>}
21
+ */
22
+ export default class WebViewer extends React.Component {
23
+ /** @type {React.RefObject<HTMLDivElement>} */
24
+ #parent = createRef();
25
+
26
+ /** @type {rerun.WebViewer} */
27
+ #handle;
28
+
29
+ /** @type {string[]} */
30
+ #recordings = [];
31
+
32
+ /** @param {Props} props */
33
+ constructor(props) {
34
+ super(props);
35
+
36
+ this.#handle = new rerun.WebViewer();
37
+ this.#recordings = toArray(props.rrd);
38
+ }
39
+
40
+ componentDidMount() {
41
+ const current = /** @type {HTMLDivElement} */ (this.#parent.current);
42
+ this.#handle.start(this.#recordings, current);
43
+ }
44
+
45
+ componentDidUpdate(/** @type {Props} */ prevProps) {
46
+ const prev = toArray(prevProps.rrd);
47
+ const current = toArray(this.props.rrd);
48
+ // Diff recordings when `rrd` prop changes.
49
+ const { added, removed } = diff(prev, current);
50
+ this.#handle.open(added);
51
+ this.#handle.close(removed);
52
+ }
53
+
54
+ componentWillUnmount() {
55
+ this.#handle.stop();
56
+ }
57
+
58
+ render() {
59
+ const { width = "100%", height = "640px" } = this.props;
60
+ return React.createElement("div", {
61
+ className: "rerun-web-viewer",
62
+ style: { width, height, position: "relative" },
63
+ ref: this.#parent,
64
+ });
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Return the difference between the two arrays.
20
70
  *
21
- * @param {Props} props
71
+ * @param {string[]} prev
72
+ * @param {string[]} current
73
+ * @returns {{ added: string[], removed: string[] }}
74
+ */
75
+ function diff(prev, current) {
76
+ const prevSet = new Set(prev);
77
+ const currentSet = new Set(current);
78
+ return {
79
+ added: current.filter((v) => !prevSet.has(v)),
80
+ removed: prev.filter((v) => !currentSet.has(v)),
81
+ };
82
+ }
83
+
84
+ /**
85
+ * @template T
86
+ * @param {T | T[]} a
87
+ * @returns {T[]}
22
88
  */
23
- export default function WebViewer(props) {
24
- const { width = "100%", height = "640px", rrd } = props;
25
-
26
- /**
27
- * Parent DOM node
28
- * @type {React.RefObject<HTMLDivElement>}
29
- */
30
- const parent = useRef(null);
31
- /**
32
- * Web viewer instance
33
- * @type {React.MutableRefObject<rerun.WebViewer | undefined>}
34
- */
35
- const viewer = useRef();
36
-
37
- useEffect(
38
- () => {
39
- if (parent.current) {
40
- // Start the web viewer when the parent div is mounted to the DOM.
41
- const w = new rerun.WebViewer();
42
- w.start(rrd, parent.current);
43
- viewer.current = w;
44
- return () => {
45
- // Stop the web viewer when the component is unmounted.
46
- w.stop();
47
- viewer.current = undefined;
48
- };
49
- }
50
- },
51
- // The web viewer will be restarted when:
52
- // - `parent` is added/moved/removed in the DOM
53
- // - `rrd` changes
54
- [parent.current, rrd],
55
- );
56
-
57
- return React.createElement("div", {
58
- className: "rerun-web-viewer",
59
- style: { width, height, position: "relative" },
60
- ref: parent,
61
- });
89
+ function toArray(a) {
90
+ return Array.isArray(a) ? a : [a];
62
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rerun-io/web-viewer-react",
3
- "version": "0.12.0-rc.3",
3
+ "version": "0.12.0-rc.4",
4
4
  "description": "Embed the Rerun web viewer in your React app",
5
5
  "private": false,
6
6
  "scripts": {
@@ -32,7 +32,7 @@
32
32
  "tsconfig.json"
33
33
  ],
34
34
  "dependencies": {
35
- "@rerun-io/web-viewer": "0.12.0-rc.3",
35
+ "@rerun-io/web-viewer": "0.12.0-rc.4",
36
36
  "@types/react": "^18.2.33",
37
37
  "react": "^18.2.0"
38
38
  },