forked from mystiq/hydrogen-web
support async callback in iterateResponseStateEvents
This commit is contained in:
parent
c8a8eb10b5
commit
22831e710c
1 changed files with 14 additions and 3 deletions
|
@ -53,12 +53,20 @@ type RoomResponse = {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** iterates over any state events in a sync room response, in the order that they should be applied (from older to younger events) */
|
/** iterates over any state events in a sync room response, in the order that they should be applied (from older to younger events) */
|
||||||
export function iterateResponseStateEvents(roomResponse: RoomResponse, callback: (StateEvent) => void) {
|
export function iterateResponseStateEvents(roomResponse: RoomResponse, callback: (StateEvent) => Promise<void> | void): Promise<void> | void {
|
||||||
|
let promises: Promise<void>[] | undefined = undefined;
|
||||||
|
const callCallback = stateEvent => {
|
||||||
|
const result = callback(stateEvent);
|
||||||
|
if (result instanceof Promise) {
|
||||||
|
promises = promises ?? [];
|
||||||
|
promises.push(result);
|
||||||
|
}
|
||||||
|
};
|
||||||
// first iterate over state events, they precede the timeline
|
// first iterate over state events, they precede the timeline
|
||||||
const stateEvents = roomResponse.state?.events;
|
const stateEvents = roomResponse.state?.events;
|
||||||
if (stateEvents) {
|
if (stateEvents) {
|
||||||
for (let i = 0; i < stateEvents.length; i++) {
|
for (let i = 0; i < stateEvents.length; i++) {
|
||||||
callback(stateEvents[i]);
|
callCallback(stateEvents[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// now see if there are any state events within the timeline
|
// now see if there are any state events within the timeline
|
||||||
|
@ -67,10 +75,13 @@ export function iterateResponseStateEvents(roomResponse: RoomResponse, callback:
|
||||||
for (let i = 0; i < timelineEvents.length; i++) {
|
for (let i = 0; i < timelineEvents.length; i++) {
|
||||||
const event = timelineEvents[i];
|
const event = timelineEvents[i];
|
||||||
if (typeof event.state_key === "string") {
|
if (typeof event.state_key === "string") {
|
||||||
callback(event);
|
callCallback(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (promises) {
|
||||||
|
return Promise.all(promises);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function tests() {
|
export function tests() {
|
||||||
|
|
Loading…
Reference in a new issue