Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
28 changes: 19 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ release date when you use `npm version` (see `README.md`).

## [Unreleased]

## [1.0.1][] - 2023-10-19
## [1.1.0] - 2023-04-13

### Added

- Ephemeral Message api

### Changed

- clarify what `T` is

## [1.0.1] - 2023-10-19

### Fixed

- add dummy js files, because vite complained that it did not found the code, see #5

## [1.0.0][] - 2023-10-11
## [1.0.0] - 2023-10-11

### Added

Expand All @@ -24,20 +34,20 @@ release date when you use `npm version` (see `README.md`).

- updated to the newest definitions from webxdc docs, as this repo should now become the source of truth for the typescript bindings.

## [0.1.0][] - 2022-07-18
## [0.1.0] - 2022-07-18

### Changed

- Fix import in README

## [0.0.3][] - 2022-07-18
## [0.0.3] - 2022-07-18

Initial public release.

[unreleased]: https://github.com/webxdc/webxdc-types/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/webxdc/webxdc-types/tree/v0.1.0


[Unreleased]: https://github.com/webxdc/webxdc-types/compare/v1.0.1...HEAD
[Unreleased]: https://github.com/webxdc/webxdc-types/compare/v1.1.0...HEAD
[1.1.0]: https://github.com/webxdc/webxdc-types/compare/v1.0.1...v1.1.0
[1.0.1]: https://github.com/webxdc/webxdc-types/compare/v1.0.0...v1.0.1
[1.0.0]: https://github.com/webxdc/webxdc-types/tree/v1.0.0
[1.0.0]: https://github.com/webxdc/webxdc-types/tree/v1.0.0
[0.1.0]: https://github.com/webxdc/webxdc-types/tree/v0.1.0
[0.0.3]: https://github.com/webxdc/webxdc-types/tree/v0.0.3
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ type Payload = {
label: string;
value: number;
};
// optional, only needed when you are planning to use the Ephemeral message p2p api.
type EphemeralPayload = {
my_action: string;
my_argument: string;
};
```

Once you have a `Payload` type, you can declare the type of `window.webxdc` in
Expand All @@ -44,7 +49,7 @@ import { WebXdc } from "webxdc-types";

declare global {
interface Window {
webxdc: WebXdc<Payload>;
webxdc: WebXdc<Payload, EphemeralPayload>;
}
}
```
Expand All @@ -58,7 +63,7 @@ Now `window.webxdc` should be fully typed.
<details>
<summary> Usage in Typescript without payload typing </summary>

Use this if you just want completions for the api, but not for the status update payloads, they will get the `any` type with this method.
Use this if you just want completions only for the global API, but not for the status update and ephemeral message payloads. These will be filled with the generic `any` type.

```typescript
import "webxdc-types/global";
Expand Down Expand Up @@ -90,11 +95,12 @@ You can use this to import the webxdc types when you need them to type your func

```js
/**
* @typedef {any} MyPayload
* @typedef {any} MyUpdatePayload
* @typedef {any} MyEphemeralPayload
* @typedef {import('webxdc-types').XDCFile} XDCFile
* @typedef {import('webxdc-types').ReceivedStatusUpdate<MyPayload>} ReceivedStatusUpdate
* @typedef {import('webxdc-types').SendingStatusUpdate<MyPayload>} SendingStatusUpdate
* @typedef {import('webxdc-types').Webxdc<MyPayload>} Webxdc
* @typedef {import('webxdc-types').ReceivedStatusUpdate<MyUpdatePayload, MyEphemeralPayload>} ReceivedStatusUpdate
* @typedef {import('webxdc-types').SendingStatusUpdate<MyUpdatePayload, MyEphemeralPayload>} SendingStatusUpdate
* @typedef {import('webxdc-types').Webxdc<MyUpdatePayload, MyEphemeralPayload>} Webxdc
*/
// note that this does not set `window.webxdc` for you follow the steps below for that.
```
Expand All @@ -109,7 +115,7 @@ If you just want the api and not want to type your payloads you can import the t

### With typed payloads

For this you need to create a `mytypes.d.ts` file declaring your payload type:
For this you need to create a `mytypes.d.ts` file declaring your payload types:

```typescript
import { WebXdc } from "webxdc-types";
Expand All @@ -120,9 +126,16 @@ type Payload = {
value: number;
};

// optional, only needed when you are planning to use the Ephemeral message p2p api.
// if you do not use it set it to `any`
type EphemeralPayload = {
my_action: string;
my_argument: string;
};

declare global {
interface Window {
webxdc: WebXdc<Payload>;
webxdc: WebXdc<Payload, EphemeralPayload>;
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Webxdc } from "./webxdc";
declare global {
interface Window {
webxdc: Webxdc<any>;
webxdc: Webxdc<any, any>;
}
}
36 changes: 27 additions & 9 deletions webxdc.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type SendingStatusUpdate<T> = {
type SendingStatusUpdate<PayloadType> = {
/** the payload, deserialized json:
* any javascript primitive, array or object. */
payload: T;
payload: PayloadType;
/** optional, short, informational message that will be added to the chat,
* eg. "Alice voted" or "Bob scored 123 in MyGame";
* usually only one line of text is shown,
Expand All @@ -16,9 +16,9 @@ type SendingStatusUpdate<T> = {
summary?: string;
};

type ReceivedStatusUpdate<T> = {
type ReceivedStatusUpdate<PayloadType> = {
/** the payload, deserialized json */
payload: T;
payload: PayloadType;
/** the serial number of this update. Serials are larger than 0 and newer serials have higher numbers */
serial: number;
/** the maximum serial currently known */
Expand Down Expand Up @@ -60,7 +60,7 @@ type SendOptions =
text: string;
};

interface Webxdc<T> {
interface Webxdc<StatusPayload, EphemeralPayload = any> {
/** Returns the peer's own address.
* This is esp. useful if you want to differ between different peers - just send the address along with the payload,
* and, if needed, compare the payload addresses against selfAddr() later on. */
Expand All @@ -74,19 +74,37 @@ interface Webxdc<T> {
* @returns promise that resolves when the listener has processed all the update messages known at the time when `setUpdateListener` was called.
* */
setUpdateListener(
cb: (statusUpdate: ReceivedStatusUpdate<T>) => void,
cb: (statusUpdate: ReceivedStatusUpdate<StatusPayload>) => void,
serial?: number
): Promise<void>;

/**
* Set a listener for _ephemeral_ status updates.
* Own status updates are not received.
Copy link

Choose a reason for hiding this comment

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

Does this trigger a regular message to announce the peer-address?
Does this wait on anything?
IOW: the docs are not clarifying what a caller can expect in terms of how connectivity happens (same question for setEphemeralUpdateListener.

Copy link
Contributor Author

@Septias Septias Apr 9, 2024

Choose a reason for hiding this comment

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

Desktop explicitly calls send_gossip_advertisement to send out an advertisement with the current IP address. The async call resolves when at least one peer is connected and the swarm is operatable.

*
* @returns Promise that is resolved when the there is atleast one peer connected
*/
setEphemeralUpdateListener(cb: (payload: EphemeralPayload) => void): Promise<void>;

/**
* @deprecated See {@link setUpdateListener|`setUpdateListener()`}.
*/
getAllUpdates(): Promise<ReceivedStatusUpdate<T>[]>;
getAllUpdates(): Promise<ReceivedStatusUpdate<StatusPayload>[]>;
/**
* Webxdc are usually shared in a chat and run independently on each peer. To get a shared status, the peers use sendUpdate() to send updates to each other.
* @param update status update to send
* @param description short, human-readable description what this update is about. this is shown eg. as a fallback text in an email program.
*/
sendUpdate(update: SendingStatusUpdate<T>, description: string): void;
sendUpdate(update: SendingStatusUpdate<StatusPayload>, description: string): void;

/**
* Send an ephemeral update to another peer.
* @param payload Data that can be serialized with `JSON.stringify`.
*
* @returns A promise that resolves when a peer connection is established. Ephemeral messages can then be sent with `sendEphemeral`
Copy link
Contributor

Choose a reason for hiding this comment

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

are you paying attention? this is sendEphemeralUpdate, your edit seems like it was meant for setEphemeralUpdateListener

*/
sendEphemeralUpdate(payload: EphemeralPayload): Promise<void>;

/**
* Send a message with file, text or both to a chat.
* Asks user to what Chat to send the message to.
Expand Down Expand Up @@ -116,4 +134,4 @@ interface Webxdc<T> {
}): Promise<File[]>;
}

export { SendingStatusUpdate, ReceivedStatusUpdate, Webxdc, XDCFile };
export { SendingStatusUpdate, ReceivedStatusUpdate, Webxdc, XDCFile };