signify-ts icon indicating copy to clipboard operation
signify-ts copied to clipboard

Bug: Exchanges.send() should not return early and instead execute for each recipient

Open kentbull opened this issue 10 months ago • 1 comments

Version

0.3.0-rc1

Environment

All

Expected behavior

Exchanges.send() should send an exchange message to each recipient.

Actual behavior

Instead it returns early on the first recipient as shown in the code below.

async send(
        name: string,
        topic: string,
        sender: HabState,
        route: string,
        payload: Dict<any>,
        embeds: Dict<any>,
        recipients: string[]
    ): Promise<any> {
        for (const recipient of recipients) {
            const [exn, sigs, atc] = await this.createExchangeMessage(
                sender,
                route,
                payload,
                embeds,
                recipient
            );
            return await this.sendFromEvents(
                name,
                topic,
                exn,
                sigs,
                atc,
                recipients
            );
        }
    }

Steps to reproduce

The code shows the bug. The corrected implementation is shown below:

async send(
    name: string,
    topic: string,
    sender: HabState,
    route: string,
    payload: Dict<any>,
    embeds: Dict<any>,
    recipients: string[]
): Promise<any[]> {  // Return an array of responses
    const results = [];  // Collect results for each recipient

    for (const recipient of recipients) {
        const [exn, sigs, atc] = await this.createExchangeMessage(
            sender,
            route,
            payload,
            embeds,
            recipient
        );
        
        const result = await this.sendFromEvents(
            name,
            topic,
            exn,
            sigs,
            atc,
            [recipient]  // Send individually
        );

        results.push(result);  // Store the response
    }

    return results;  // Return all results
}

kentbull avatar Feb 04 '25 20:02 kentbull

For reference. This was discussed in this PR some time ago: https://github.com/WebOfTrust/signify-ts/pull/267#discussion_r1642546563

lenkan avatar Feb 05 '25 11:02 lenkan