mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-03 22:33:07 +00:00
implement background send, requestID
This commit is contained in:
parent
4fe8bfdd88
commit
37ee490114
@ -80,10 +80,7 @@ export class MessageStore {
|
||||
}
|
||||
}
|
||||
|
||||
public async queue(
|
||||
encoder: IEncoder,
|
||||
message: IMessage
|
||||
): Promise<RequestId | undefined> {
|
||||
public async queue(encoder: IEncoder, message: IMessage): Promise<RequestId> {
|
||||
const requestId = crypto.randomUUID();
|
||||
|
||||
this.pendingRequests.set(requestId, {
|
||||
|
||||
@ -11,16 +11,46 @@ export class Sender {
|
||||
private readonly messageStore: MessageStore;
|
||||
private readonly lightPush: ILightPush;
|
||||
|
||||
private sendInterval: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
public constructor(params: SenderConstructorParams) {
|
||||
this.messageStore = params.messageStore;
|
||||
this.lightPush = params.lightPush;
|
||||
}
|
||||
|
||||
public async send(encoder: IEncoder, message: IMessage): Promise<void> {
|
||||
public start(): void {
|
||||
this.sendInterval = setInterval(() => void this.backgroundSend(), 1000);
|
||||
}
|
||||
|
||||
public stop(): void {
|
||||
if (this.sendInterval) {
|
||||
clearInterval(this.sendInterval);
|
||||
this.sendInterval = null;
|
||||
}
|
||||
}
|
||||
|
||||
public async send(encoder: IEncoder, message: IMessage): Promise<string> {
|
||||
const requestId = await this.messageStore.queue(encoder, message);
|
||||
await this.lightPush.send(encoder, message);
|
||||
if (requestId) {
|
||||
const response = await this.lightPush.send(encoder, message);
|
||||
|
||||
if (response.successes.length > 0) {
|
||||
await this.messageStore.markSent(requestId);
|
||||
}
|
||||
|
||||
return requestId;
|
||||
}
|
||||
|
||||
private async backgroundSend(): Promise<void> {
|
||||
const pendingRequests = this.messageStore.getMessagesToSend();
|
||||
|
||||
// todo: implement chunking, error handling, retry, etc.
|
||||
// todo: implement backoff and batching potentially
|
||||
for (const { requestId, encoder, message } of pendingRequests) {
|
||||
const response = await this.lightPush.send(encoder, message);
|
||||
|
||||
if (response.successes.length > 0) {
|
||||
await this.messageStore.markSent(requestId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user