|
42 | 42 | * For a simple example of distributed mode without editing, see |
43 | 43 | * `index.html`. |
44 | 44 | */ |
45 | | - window.addEventListener("DOMContentLoaded", async function () { |
46 | | - const viewer = document.getElementById("viewer"); |
| 45 | + const viewer = document.getElementById("viewer"); |
47 | 46 |
|
48 | | - // Connect to the websocket and create a new webworker |
49 | | - const websocket = perspective.websocket("ws://localhost:8080/websocket"); |
50 | | - const worker = perspective.worker(); |
| 47 | + // Connect to the websocket and create a new webworker |
| 48 | + const websocket = perspective.websocket("ws://localhost:8080/websocket"); |
| 49 | + const worker = perspective.worker(); |
51 | 50 |
|
52 | | - // Get handles to the `Table` on the server, and create a |
53 | | - // `view()` on the server. |
54 | | - const server_table = await websocket.open_table("data_source_one"); |
55 | | - const server_view = await server_table.view(); |
| 51 | + // Get handles to the `Table` on the server, and create a |
| 52 | + // `view()` on the server. |
| 53 | + const server_table = await websocket.open_table("data_source_one"); |
| 54 | + const server_view = await server_table.view(); |
56 | 55 |
|
57 | | - // Serialize the current state of the view to an arrow, and create |
58 | | - // a Table on the client that has the same index as the Table |
59 | | - // on the server using `get_index()`. |
60 | | - // |
61 | | - // Client/server editing does not work on an unindexed Table. |
62 | | - const arrow = await server_view.to_arrow(); |
63 | | - const client_table = await worker.table(arrow, { index: "Row ID" }); |
64 | | - const client_view = await client_table.view(); // client -> server |
| 56 | + // Serialize the current state of the view to an arrow, and create |
| 57 | + // a Table on the client that has the same index as the Table |
| 58 | + // on the server using `get_index()`. |
| 59 | + // |
| 60 | + // Client/server editing does not work on an unindexed Table. |
| 61 | + const arrow = await server_view.to_arrow(); |
| 62 | + const client_table = await worker.table(arrow, { index: "Row ID" }); |
| 63 | + const client_view = await client_table.view(); // client -> server |
65 | 64 |
|
66 | | - await viewer.load(Promise.resolve(client_table)); |
| 65 | + await viewer.load(Promise.resolve(client_table)); |
67 | 66 |
|
68 | | - // Create ports on both the client and the server - this allows |
69 | | - // the client and server to track which updates are coming from |
70 | | - // the server, and which edits are coming from the client |
71 | | - // in the browser. |
72 | | - const client_edit_port = await viewer.getEditPort(); |
73 | | - const server_edit_port = await server_table.make_port(); |
| 67 | + // Create ports on both the client and the server - this allows |
| 68 | + // the client and server to track which updates are coming from |
| 69 | + // the server, and which edits are coming from the client |
| 70 | + // in the browser. |
| 71 | + const client_edit_port = await viewer.getEditPort(); |
| 72 | + const server_edit_port = await server_table.make_port(); |
74 | 73 |
|
75 | | - // When the client updates, if the update comes through the edit |
76 | | - // port then forward it to the server. |
77 | | - client_view.on_update( |
78 | | - (updated) => { |
79 | | - if (updated.port_id === client_edit_port) { |
80 | | - // `update` must be on the `server_edit_port` |
81 | | - server_table.update(updated.delta, { |
82 | | - port_id: server_edit_port, |
83 | | - }); |
84 | | - } |
85 | | - }, |
86 | | - { mode: "row" } |
87 | | - ); |
| 74 | + // When the client updates, if the update comes through the edit |
| 75 | + // port then forward it to the server. |
| 76 | + client_view.on_update( |
| 77 | + (updated) => { |
| 78 | + if (updated.port_id === client_edit_port) { |
| 79 | + // `update` must be on the `server_edit_port` |
| 80 | + server_table.update(updated.delta, { |
| 81 | + port_id: server_edit_port, |
| 82 | + }); |
| 83 | + } |
| 84 | + }, |
| 85 | + { mode: "row" } |
| 86 | + ); |
88 | 87 |
|
89 | | - // If the server updates, and the edit is not coming from the |
90 | | - // server edit port, then synchronize state with the client. |
91 | | - server_view.on_update( |
92 | | - (updated) => { |
93 | | - if (updated.port_id !== server_edit_port) { |
94 | | - // any port, we don't care |
95 | | - client_table.update(updated.delta); |
96 | | - } |
97 | | - }, |
98 | | - { mode: "row" } |
99 | | - ); |
100 | | - }); |
| 88 | + // If the server updates, and the edit is not coming from the |
| 89 | + // server edit port, then synchronize state with the client. |
| 90 | + server_view.on_update( |
| 91 | + (updated) => { |
| 92 | + if (updated.port_id !== server_edit_port) { |
| 93 | + // any port, we don't care |
| 94 | + client_table.update(updated.delta); |
| 95 | + } |
| 96 | + }, |
| 97 | + { mode: "row" } |
| 98 | + ); |
101 | 99 | </script> |
102 | 100 | </body> |
103 | 101 | </html> |
0 commit comments