Skip to content

Commit

Permalink
(temp) test fix edgecase
Browse files Browse the repository at this point in the history
  • Loading branch information
toger5 committed Nov 6, 2024
1 parent e11f9de commit 0eb0346
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
23 changes: 23 additions & 0 deletions spec/unit/embedded.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
ITurnServer,
IRoomEvent,
IOpenIDCredentials,
ISendEventFromWidgetResponseData,
} from "matrix-widget-api";

import { createRoomWidgetClient, MsgType, UpdateDelayedEventAction } from "../../src/matrix";
Expand Down Expand Up @@ -187,6 +188,28 @@ describe("RoomWidgetClient", () => {
.map((e) => e.getEffectiveEvent()),
).toEqual([event]);
});
it("updates local echo", async () => {
await makeClient({
receiveEvent: ["org.matrix.rageshake_request"],
sendEvent: ["org.matrix.rageshake_request"],
});
expect(widgetApi.requestCapabilityForRoomTimeline).toHaveBeenCalledWith("!1:example.org");
expect(widgetApi.requestCapabilityToReceiveEvent).toHaveBeenCalledWith("org.matrix.rageshake_request");
// const sendSpy = jest.spyOn(widgetApi.transport, "send");
const sendMock = jest.fn();
new Promise((resolve, _) => {
widgetApi.sendRoomEvent.mockImplementation(
async (eType, content, roomId): Promise<ISendEventFromWidgetResponseData> => {},

Check failure on line 202 in spec/unit/embedded.spec.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
);
});
// widgetApi.transport.o;
client.sendEvent("!1:example.org", "org.matrix.rageshake_request", { request_id: 12 }, "widgetTxId");
expect(sendMock).toHaveBeenCalledWith("abc");
widgetApi.emit(
`action:${WidgetApiToWidgetAction.SendEvent}`,
new CustomEvent(`action:${WidgetApiToWidgetAction.SendEvent}`, { detail: { data: event } }),
);
});
});

describe("delayed events", () => {
Expand Down
33 changes: 25 additions & 8 deletions src/embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// eslint-disable-next-line no-restricted-imports
import { EventEmitter } from "events";
import {
WidgetApi,
WidgetApiToWidgetAction,
Expand Down Expand Up @@ -46,7 +48,7 @@ import {
import { SyncApi, SyncState } from "./sync.ts";
import { SlidingSyncSdk } from "./sliding-sync-sdk.ts";
import { User } from "./models/user.ts";
import { Room, RoomEvent } from "./models/room.ts";
import { Room } from "./models/room.ts";
import { ToDeviceBatch, ToDevicePayload } from "./models/ToDeviceMessage.ts";
import { DeviceInfo } from "./crypto/deviceinfo.ts";
import { IOlmDevice } from "./crypto/algorithms/megolm.ts";
Expand Down Expand Up @@ -117,6 +119,10 @@ export interface ICapabilities {
updateDelayedEvents?: boolean;
}

export enum PendingEvent {
PendingEventsChanged = "PendingEvent.pendingEventsChanged",
}

/**
* A MatrixClient that routes its requests through the widget API instead of the
* real CS API.
Expand All @@ -130,6 +136,8 @@ export class RoomWidgetClient extends MatrixClient {

private pendingSendingEventsTxId: { type: string; id: string | undefined; txId: string }[] = [];

// TODO make typed event emitter.
private pendingEventsEmitter = new EventEmitter();
/**
*
* @param widgetApi - The widget api to use for communication.
Expand Down Expand Up @@ -325,16 +333,17 @@ export class RoomWidgetClient extends MatrixClient {
throw e;
}

// This also checks for an event id on the response
room.updatePendingEvent(event, EventStatus.SENT, response.event_id);

// Update the pending events list with the eventId
if (txId) {
this.pendingSendingEventsTxId.map((old) => {
if (old.txId === txId) old.id = response.event_id;
return old;
});
}

// This also checks for an event id on the response
room.updatePendingEvent(event, EventStatus.SENT, response.event_id);
this.pendingEventsEmitter.emit(PendingEvent.PendingEventsChanged);

return { event_id: response.event_id! };
}
Expand Down Expand Up @@ -500,16 +509,24 @@ export class RoomWidgetClient extends MatrixClient {
let matchingTxId = this.pendingSendingEventsTxId.find((p) => p.id === event.getId())?.txId;
// Block injection this event until we have received the sending response.
// -> until we know the event id.
while (!matchingTxId) {
while (!matchingTxId && this.pendingSendingEventsTxId.length > 0) {
// Recheck whenever the LocalEchoUpdated
await new Promise<void>((resolve) => this.room?.once(RoomEvent.LocalEchoUpdated, () => resolve()));
await new Promise<void>((resolve) =>
this.pendingEventsEmitter.once(PendingEvent.PendingEventsChanged, () => resolve()),
);
matchingTxId = this.pendingSendingEventsTxId.find((p) => p.id === event.getId())?.txId;
}

// We found the correct txId: we update the event and delete the entry of the pending events.
event.setTxnId(matchingTxId);
event.setUnsigned({ ...event.getUnsigned(), transaction_id: matchingTxId });
if (matchingTxId) {
event.setTxnId(matchingTxId);
event.setUnsigned({ ...event.getUnsigned(), transaction_id: matchingTxId });
}
this.pendingSendingEventsTxId.filter((p) => p.id !== event.getId());

if (this.pendingSendingEventsTxId.length === 0) {
this.pendingEventsEmitter.emit(PendingEvent.PendingEventsChanged);
}
}

await this.syncApi!.injectRoomEvents(this.room!, [], [event]);
Expand Down

0 comments on commit 0eb0346

Please sign in to comment.