Skip to content

feat: ephemeral peer channels #6

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

Merged
merged 11 commits into from
May 31, 2024
Merged
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
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-22

### Added

- Realtime data 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
43 changes: 35 additions & 8 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,26 @@ type SendOptions =
text: string;
};

interface Webxdc<T> {
/**
* A listener for realtime data.
*/
export class RealtimeListener {
private listener: (data: Uint8Array) => void
private trashed: boolean

/* Whether the realtime channel was left */
private is_trashed(): boolean
/* Receive data from the realtime channel */
private receive(data: Uint8Array): void
/* Set a listener for the realtime channel */
public setListener(listener: (data: Uint8Array) => void): void
/* Send data over the realtime channel */
public send(data: Uint8Array): void
/* Leave the realtime channel */
public leave(): void
}

interface Webxdc<StatusPayload> {
/** 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 +93,27 @@ 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>;

/**
* Join a realtime channel.
* @throws Calling this function a second time
* without leaving the previous channel will throw an error.
*/
joinRealtimeChannel(): RealtimeListener;

/**
* @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 a message with file, text or both to a chat.
* Asks user to what Chat to send the message to.
Expand Down