Skip to content

add examples for sendUpdate, setUpdateListener #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src-docs/spec/sendUpdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ Send an update to all peers.
- `descr`: short, human-readable description what this update is about.
this is shown e.g. as a fallback text in an e-mail program.

Example:

```js
window.webxdc.sendUpdate(
{ payload: "Hello from Alice" },
"A 'hello' message"
);

// Peers can receive messages as such:
window.webxdc.setUpdateListener((update) => {
console.log(update.payload);
});
// 'Hello from Alice' is printed in the console
```

All peers, including the sending one,
will receive the update by the callback given to [`setUpdateListener()`](./setUpdateListener.html).

Expand Down
34 changes: 34 additions & 0 deletions src-docs/spec/setUpdateListener.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,40 @@ Each `update` which is passed to the callback comes with the following propertie

- `update.summary`: optional, short text, shown beside icon (see [`sendUpdate()`])

Example:

```js
let myDocumentState = "";
const initialPendingUpdatesHandledPromise = window.webxdc.setUpdateListener(
(update) => {
// Remember that the listener is invoked for
// your own `window.webxdc.sendUpdate()` calls as well!

// Dummy document update logic.
// Yours might be more complex,
// such as applying a chess move to the board.
Comment on lines +42 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Yours might be more complex,
// such as applying a chess move to the board.

myDocumentState = myDocumentUpdate;

const areAllUpdatesProcessed = update.serial === update.max_serial;
if (areAllUpdatesProcessed) {
renderDocument();
}
}
);

initialPendingUpdatesHandledPromise.then(() => {
renderDocument();
});

// Let's only call this when there are no pending updates.
function renderDocument() {
document.body.innerText = myDocumentState;
}

// Peers can send messages like this
window.webxdc.sendUpdate({ payload: "Knight d3" }, "Bob made a move!");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

above you talk about a hyptothetic chess thing, but suddenly you send a move.
I'd rather stay within the "document state" terminology.

Suggested change
window.webxdc.sendUpdate({ payload: "Knight d3" }, "Bob made a move!");
window.webxdc.sendUpdate({ payload: "new document set from ${window.webxdc.selfName}" });

```

Calling `setUpdateListener()` multiple times is undefined behavior: in current implementations only the last invocation works.

[`sendUpdate()`]: ./sendUpdate.html