2022-02-23 19:21:11 +08:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import EventEmitter from "events";
|
|
|
|
|
2022-05-24 00:29:16 +08:00
|
|
|
import { ActionPayload } from "../../src/dispatcher/payloads";
|
|
|
|
import defaultDispatcher from "../../src/dispatcher/dispatcher";
|
|
|
|
import { DispatcherAction } from "../../src/dispatcher/actions";
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
export const emitPromise = (e: EventEmitter, k: string | symbol) => new Promise((r) => e.once(k, r));
|
2022-02-23 19:21:11 +08:00
|
|
|
|
2022-09-20 23:32:39 +08:00
|
|
|
/**
|
|
|
|
* Waits for a certain payload to be dispatched.
|
|
|
|
* @param waitForAction The action string to wait for or the callback which is invoked for every dispatch. If this returns true, stops waiting.
|
|
|
|
* @param timeout The max time to wait before giving up and stop waiting. If 0, no timeout.
|
|
|
|
* @param dispatcher The dispatcher to listen on.
|
|
|
|
* @returns A promise which resolves when the callback returns true. Resolves with the payload that made it stop waiting.
|
|
|
|
* Rejects when the timeout is reached.
|
|
|
|
*/
|
|
|
|
export function untilDispatch(
|
2022-12-12 19:24:14 +08:00
|
|
|
waitForAction: DispatcherAction | ((payload: ActionPayload) => boolean),
|
|
|
|
dispatcher = defaultDispatcher,
|
|
|
|
timeout = 1000,
|
2022-09-20 23:32:39 +08:00
|
|
|
): Promise<ActionPayload> {
|
2023-02-17 01:21:44 +08:00
|
|
|
const callerLine = new Error().stack!.toString().split("\n")[2];
|
2022-09-20 23:32:39 +08:00
|
|
|
if (typeof waitForAction === "string") {
|
|
|
|
const action = waitForAction;
|
|
|
|
waitForAction = (payload) => {
|
|
|
|
return payload.action === action;
|
|
|
|
};
|
|
|
|
}
|
2022-12-12 19:24:14 +08:00
|
|
|
const callback = waitForAction as (payload: ActionPayload) => boolean;
|
2022-09-20 23:32:39 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let fulfilled = false;
|
2023-02-13 19:39:16 +08:00
|
|
|
let timeoutId: number;
|
2022-09-20 23:32:39 +08:00
|
|
|
// set a timeout handler if needed
|
|
|
|
if (timeout > 0) {
|
2022-11-30 19:32:56 +08:00
|
|
|
timeoutId = window.setTimeout(() => {
|
2022-09-20 23:32:39 +08:00
|
|
|
if (!fulfilled) {
|
|
|
|
reject(new Error(`untilDispatch: timed out at ${callerLine}`));
|
|
|
|
fulfilled = true;
|
|
|
|
}
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
// listen for dispatches
|
|
|
|
const token = dispatcher.register((p: ActionPayload) => {
|
|
|
|
const finishWaiting = callback(p);
|
2022-12-12 19:24:14 +08:00
|
|
|
if (finishWaiting || fulfilled) {
|
|
|
|
// wait until we're told or we timeout
|
2022-09-20 23:32:39 +08:00
|
|
|
// if we haven't timed out, resolve now with the payload.
|
|
|
|
if (!fulfilled) {
|
|
|
|
resolve(p);
|
|
|
|
fulfilled = true;
|
|
|
|
}
|
|
|
|
// cleanup
|
|
|
|
dispatcher.unregister(token);
|
|
|
|
if (timeoutId) {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
}
|
2022-05-24 00:29:16 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-20 23:32:39 +08:00
|
|
|
/**
|
|
|
|
* Waits for a certain event to be emitted.
|
|
|
|
* @param emitter The EventEmitter to listen on.
|
|
|
|
* @param eventName The event string to wait for.
|
|
|
|
* @param check Optional function which is invoked when the event fires. If this returns true, stops waiting.
|
|
|
|
* @param timeout The max time to wait before giving up and stop waiting. If 0, no timeout.
|
|
|
|
* @returns A promise which resolves when the callback returns true or when the event is emitted if
|
|
|
|
* no callback is provided. Rejects when the timeout is reached.
|
|
|
|
*/
|
|
|
|
export function untilEmission(
|
2022-12-12 19:24:14 +08:00
|
|
|
emitter: EventEmitter,
|
|
|
|
eventName: string,
|
2023-02-17 01:21:44 +08:00
|
|
|
check?: (...args: any[]) => boolean,
|
2022-12-12 19:24:14 +08:00
|
|
|
timeout = 1000,
|
2022-09-20 23:32:39 +08:00
|
|
|
): Promise<void> {
|
2023-02-17 01:21:44 +08:00
|
|
|
const callerLine = new Error().stack!.toString().split("\n")[2];
|
2022-09-20 23:32:39 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let fulfilled = false;
|
2023-02-13 19:39:16 +08:00
|
|
|
let timeoutId: number;
|
2022-09-20 23:32:39 +08:00
|
|
|
// set a timeout handler if needed
|
|
|
|
if (timeout > 0) {
|
2022-11-30 19:32:56 +08:00
|
|
|
timeoutId = window.setTimeout(() => {
|
2022-09-20 23:32:39 +08:00
|
|
|
if (!fulfilled) {
|
|
|
|
reject(new Error(`untilEmission: timed out at ${callerLine}`));
|
|
|
|
fulfilled = true;
|
|
|
|
}
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
const callback = (...args: any[]) => {
|
|
|
|
// if they supplied a check function, call it now. Bail if it returns false.
|
|
|
|
if (check) {
|
|
|
|
if (!check(...args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we didn't time out, resolve. Otherwise, we already rejected so don't resolve now.
|
|
|
|
if (!fulfilled) {
|
|
|
|
resolve();
|
|
|
|
fulfilled = true;
|
|
|
|
}
|
|
|
|
// cleanup
|
|
|
|
emitter.off(eventName, callback);
|
|
|
|
if (timeoutId) {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// listen for emissions
|
|
|
|
emitter.on(eventName, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
export const flushPromises = async () => await new Promise((resolve) => window.setTimeout(resolve));
|
2022-02-23 19:21:11 +08:00
|
|
|
|
2022-03-25 19:30:50 +08:00
|
|
|
// with jest's modern fake timers process.nextTick is also mocked,
|
|
|
|
// flushing promises in the normal way then waits for some advancement
|
|
|
|
// of the fake timers
|
|
|
|
// https://gist.github.com/apieceofbart/e6dea8d884d29cf88cdb54ef14ddbcc4?permalink_comment_id=4018174#gistcomment-4018174
|
|
|
|
export const flushPromisesWithFakeTimers = async (): Promise<void> => {
|
2022-12-12 19:24:14 +08:00
|
|
|
const promise = new Promise((resolve) => process.nextTick(resolve));
|
2022-03-25 19:30:50 +08:00
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
await promise;
|
|
|
|
};
|
|
|
|
|
2022-02-23 19:21:11 +08:00
|
|
|
/**
|
|
|
|
* Call fn before calling componentDidUpdate on a react component instance, inst.
|
|
|
|
* @param {React.Component} inst an instance of a React component.
|
|
|
|
* @param {number} updates Number of updates to wait for. (Defaults to 1.)
|
|
|
|
* @returns {Promise} promise that resolves when componentDidUpdate is called on
|
|
|
|
* given component instance.
|
|
|
|
*/
|
|
|
|
export function waitForUpdate(inst: React.Component, updates = 1): Promise<void> {
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
const cdu = inst.componentDidUpdate;
|
|
|
|
|
|
|
|
console.log(`Waiting for ${updates} update(s)`);
|
|
|
|
|
|
|
|
inst.componentDidUpdate = (prevProps, prevState, snapshot) => {
|
|
|
|
updates--;
|
|
|
|
console.log(`Got update, ${updates} remaining`);
|
|
|
|
|
|
|
|
if (updates == 0) {
|
|
|
|
inst.componentDidUpdate = cdu;
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cdu) cdu(prevProps, prevState, snapshot);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2022-03-25 19:30:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance jests fake timers and Date.now mock by ms
|
|
|
|
* Useful for testing code using timeouts or intervals
|
|
|
|
* that also checks timestamps
|
|
|
|
*/
|
|
|
|
export const advanceDateAndTime = (ms: number) => {
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(Date.now() + ms);
|
2022-03-25 19:30:50 +08:00
|
|
|
jest.advanceTimersByTime(ms);
|
|
|
|
};
|