Skip to content

Commit

Permalink
Merge pull request #102 from xmtp/np/stop-streams
Browse files Browse the repository at this point in the history
feat: Ability to cancel streams
  • Loading branch information
nplasterer authored Sep 8, 2023
2 parents fe09f9d + 15b4313 commit 6c3fb33
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,16 @@ class XMTPModule : Module() {
)
}

Function("unsubscribeFromConversations") {
logV("unsubscribeFromConversations")
subscriptions["conversations"]?.cancel()
}

Function("unsubscribeFromAllMessages") {
logV("unsubscribeFromAllMessages")
subscriptions["messages"]?.cancel()
}

AsyncFunction("unsubscribeFromMessages") { clientAddress: String, topic: String ->
logV("unsubscribeFromMessages")
unsubscribeFromMessages(
Expand Down
28 changes: 7 additions & 21 deletions example/dev/local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,17 @@ services:
depends_on:
- db
healthcheck:
test: ["CMD", "lsof", "-i", ":5556"]
test: [ "CMD", "lsof", "-i", ":5556" ]
interval: 3s
timeout: 10s
retries: 5
db:
image: postgres:13
environment:
POSTGRES_PASSWORD: xmtp

upload-service:
build: ./upload-service
ports:
- 4567:4567

caddy:
image: caddy:latest
ports:
- "443:443"
volumes:
- ./upload-service/Caddyfile:/etc/caddy/Caddyfile
- ./upload-service/data/data:/data
- ./upload-service/data/config:/config
# js:
# restart: always
# depends_on:
# wakunode:
# condition: service_healthy
# build: ./test
js:
restart: always
depends_on:
wakunode:
condition: service_healthy
build: ./test
3 changes: 3 additions & 0 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ test("can stream messages", async () => {
throw Error("Unexpected convo message topic " + convoMessages[i].topic);
}
}
alice.conversations.cancelStream();
alice.conversations.cancelStreamAllMessages();

return true;
});

Expand Down
8 changes: 8 additions & 0 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ public class XMTPModule: Module {
try await subscribeToMessages(clientAddress: clientAddress, topic: topic)
}

Function("unsubscribeFromConversations") {
subscriptions["conversations"]?.cancel()
}

Function("unsubscribeFromAllMessages") {
subscriptions["messages"]?.cancel()
}

AsyncFunction("unsubscribeFromMessages") { (clientAddress: String, topic: String) in
try await unsubscribeFromMessages(clientAddress: clientAddress, topic: topic)
}
Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ export async function subscribeToMessages(
return await XMTPModule.subscribeToMessages(clientAddress, topic);
}

export function unsubscribeFromConversations() {
return XMTPModule.unsubscribeFromConversations();
}

export function unsubscribeFromAllMessages() {
return XMTPModule.unsubscribeFromAllMessages();
}

export async function unsubscribeFromMessages(
clientAddress: string,
topic: string,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ export default class Conversations {
},
);
}

cancelStream() {
XMTPModule.unsubscribeFromConversations();
}

cancelStreamAllMessages() {
XMTPModule.unsubscribeFromAllMessages();
}
}

0 comments on commit 6c3fb33

Please sign in to comment.