TeactN: Support Promise from actions (#5972)
This commit is contained in:
parent
5da492a24b
commit
42d76abbba
@ -47,6 +47,7 @@ type ActionHandlers = {
|
|||||||
export const getGlobal = typed.getGlobal;
|
export const getGlobal = typed.getGlobal;
|
||||||
export const setGlobal = typed.setGlobal;
|
export const setGlobal = typed.setGlobal;
|
||||||
export const getActions = typed.getActions;
|
export const getActions = typed.getActions;
|
||||||
|
export const getPromiseActions = typed.getPromiseActions;
|
||||||
export const addActionHandler = typed.addActionHandler as <ActionName extends ProjectActionNames>(
|
export const addActionHandler = typed.addActionHandler as <ActionName extends ProjectActionNames>(
|
||||||
name: ActionName,
|
name: ActionName,
|
||||||
handler: ActionHandlers[ActionName],
|
handler: ActionHandlers[ActionName],
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import type { FC, FC_withDebug, Props } from './teact';
|
|||||||
|
|
||||||
import { DEBUG, DEBUG_MORE } from '../../config';
|
import { DEBUG, DEBUG_MORE } from '../../config';
|
||||||
import arePropsShallowEqual, { logUnequalProps } from '../../util/arePropsShallowEqual';
|
import arePropsShallowEqual, { logUnequalProps } from '../../util/arePropsShallowEqual';
|
||||||
|
import Deferred from '../../util/Deferred';
|
||||||
import { handleError } from '../../util/handleError';
|
import { handleError } from '../../util/handleError';
|
||||||
import { orderBy } from '../../util/iteratees';
|
import { orderBy } from '../../util/iteratees';
|
||||||
import { throttleWithTickEnd } from '../../util/schedulers';
|
import { throttleWithTickEnd } from '../../util/schedulers';
|
||||||
@ -132,15 +133,25 @@ export function forceOnHeavyAnimationOnce() {
|
|||||||
|
|
||||||
let actionQueue: NoneToVoidFunction[] = [];
|
let actionQueue: NoneToVoidFunction[] = [];
|
||||||
|
|
||||||
function handleAction(name: string, payload?: ActionPayload, options?: ActionOptions) {
|
function handleAction(name: string, payload?: ActionPayload, options?: ActionOptions): Promise<void> {
|
||||||
|
const deferred = new Deferred<void>();
|
||||||
actionQueue.push(() => {
|
actionQueue.push(() => {
|
||||||
actionHandlers[name]?.forEach((handler) => {
|
actionHandlers[name]?.forEach((handler) => {
|
||||||
const response = handler(DEBUG ? getUntypedGlobal() : currentGlobal, actions, payload);
|
const response = handler(DEBUG ? getUntypedGlobal() : currentGlobal, actions, payload);
|
||||||
if (!response || typeof response.then === 'function') {
|
if (!response) {
|
||||||
|
deferred.resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof response.then === 'function') {
|
||||||
|
response.then(() => {
|
||||||
|
deferred.resolve();
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setUntypedGlobal(response as GlobalState, options);
|
setUntypedGlobal(response as GlobalState, options);
|
||||||
|
deferred.resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -154,6 +165,8 @@ function handleAction(name: string, payload?: ActionPayload, options?: ActionOpt
|
|||||||
actionQueue = [];
|
actionQueue = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return deferred.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateContainers() {
|
function updateContainers() {
|
||||||
@ -221,7 +234,7 @@ export function addUntypedActionHandler(name: ActionNames, handler: ActionHandle
|
|||||||
actionHandlers[name] = [];
|
actionHandlers[name] = [];
|
||||||
|
|
||||||
actions[name] = (payload?: ActionPayload, options?: ActionOptions) => {
|
actions[name] = (payload?: ActionPayload, options?: ActionOptions) => {
|
||||||
handleAction(name, payload, options);
|
return handleAction(name, payload, options);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,12 +325,12 @@ export function typify<
|
|||||||
type ProjectActionNames = keyof ActionPayloads;
|
type ProjectActionNames = keyof ActionPayloads;
|
||||||
|
|
||||||
// When payload is allowed to be `undefined` we consider it optional
|
// When payload is allowed to be `undefined` we consider it optional
|
||||||
type ProjectActions = {
|
type ProjectActions<ReturnType = void> = {
|
||||||
[ActionName in ProjectActionNames]:
|
[ActionName in ProjectActionNames]:
|
||||||
(undefined extends ActionPayloads[ActionName] ? (
|
(undefined extends ActionPayloads[ActionName] ? (
|
||||||
(payload?: ActionPayloads[ActionName], options?: ActionOptions) => void
|
(payload?: ActionPayloads[ActionName], options?: ActionOptions) => ReturnType
|
||||||
) : (
|
) : (
|
||||||
(payload: ActionPayloads[ActionName], options?: ActionOptions) => void
|
(payload: ActionPayloads[ActionName], options?: ActionOptions) => ReturnType
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -333,6 +346,7 @@ export function typify<
|
|||||||
getGlobal: getUntypedGlobal as <T extends ProjectGlobalState>() => T,
|
getGlobal: getUntypedGlobal as <T extends ProjectGlobalState>() => T,
|
||||||
setGlobal: setUntypedGlobal as (state: ProjectGlobalState, options?: ActionOptions) => void,
|
setGlobal: setUntypedGlobal as (state: ProjectGlobalState, options?: ActionOptions) => void,
|
||||||
getActions: getUntypedActions as () => ProjectActions,
|
getActions: getUntypedActions as () => ProjectActions,
|
||||||
|
getPromiseActions: getUntypedActions as () => ProjectActions<Promise<void>>,
|
||||||
addActionHandler: addUntypedActionHandler as <ActionName extends ProjectActionNames>(
|
addActionHandler: addUntypedActionHandler as <ActionName extends ProjectActionNames>(
|
||||||
name: ActionName,
|
name: ActionName,
|
||||||
handler: ActionHandlers[ActionName],
|
handler: ActionHandlers[ActionName],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user