Skip to content

Commit

Permalink
fix(sdks/actor/client): make disconnect async & await for socket close (
Browse files Browse the repository at this point in the history
#1733)

<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
NathanFlurry committed Dec 30, 2024
1 parent 58a1c11 commit d702a98
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
23 changes: 14 additions & 9 deletions sdks/actor/client/src/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,23 +333,28 @@ export class ActorHandleRaw {
}

// TODO:Add destructor
disconnect() {
if (!this.#websocket) return;
this.#disconnected = true;
disconnect(): Promise<void> {
return new Promise(resolve => {
if (!this.#websocket) return;
this.#disconnected = true;

logger().debug("disconnecting");
logger().debug("disconnecting");

// TODO: What do we do with the queue?
// TODO: What do we do with the queue?

this.#websocket?.close();
this.#websocket = undefined;
if (this.#websocket) {
this.#websocket.addEventListener("close", () => resolve());
this.#websocket.close();
this.#websocket = undefined;
}
});
}

dispose() {
async dispose() {
logger().debug("disposing");

// TODO: this will error if not usable
this.disconnect();
await this.disconnect();
}

#sendSubscription(eventName: string, subscribe: boolean) {
Expand Down
10 changes: 8 additions & 2 deletions site/src/content/docs/connections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,21 @@ class ChatRoom extends Actor {
Connections can be disconnected with:

```typescript {{ "title": "actor.ts" }}
connection.disconnect();
await connection.disconnect();
```

A reason can optionally be provided like:

```typescript {{ "title": "actor.ts" }}
connection.disconnect('Too many requests');
await connection.disconnect('Too many requests');
```

<Tip>
Waiting the `disconnect` promise is not required, but recommended in order
to ensure the underlying network connections close cleanly before exiting
the program.
</Tip>

## Offline & reconnection behavior

Clients automatically attempt to reconnect (with [exponential backoff](https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/retry-backoff.html)) when disconnected. Remote procedure calls made while disconnected are queued.
Expand Down

0 comments on commit d702a98

Please sign in to comment.