Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cancelable): simplify code #876

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 17 additions & 33 deletions packages/autocomplete-core/src/utils/createCancelablePromise.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
type CancelablePromiseState = {
isCanceled: boolean;
onCancelList: Array<(...args: any[]) => any>;
};

type PromiseExecutor<TValue> = (
resolve: (value: TValue | PromiseLike<TValue>) => void,
reject: (reason?: any) => void
) => void;

type CreateInternalCancelablePromiseParams<TValue> = {
promise: Promise<TValue>;
initialState: CancelablePromiseState;
type CancelablePromiseState = {
isCanceled: boolean;
onCancelList: Array<(...args: any[]) => any>;
};

function createInternalCancelablePromise<TValue>({
initialState,
promise,
}: CreateInternalCancelablePromiseParams<TValue>): CancelablePromise<TValue> {
function createInternalCancelablePromise<TValue>(
promise: Promise<TValue>,
initialState: CancelablePromiseState
): CancelablePromise<TValue> {
const state = initialState;

return {
then(onfulfilled, onrejected) {
return createCancelable(
return createInternalCancelablePromise(
promise.then(
createCallback(onfulfilled, state, promise),
createCallback(onrejected, state, promise)
Expand All @@ -30,7 +25,7 @@ function createInternalCancelablePromise<TValue>({
);
},
catch(onrejected) {
return createCancelable(
return createInternalCancelablePromise(
promise.catch(createCallback(onrejected, state, promise)),
state
);
Expand All @@ -40,7 +35,7 @@ function createInternalCancelablePromise<TValue>({
state.onCancelList.push(onfinally);
}

return createCancelable<TValue>(
return createInternalCancelablePromise<TValue>(
promise.finally(
createCallback(
onfinally &&
Expand Down Expand Up @@ -110,12 +105,12 @@ export type CancelablePromise<TValue> = {
export function createCancelablePromise<TValue>(
executor: PromiseExecutor<TValue>
): CancelablePromise<TValue> {
return createInternalCancelablePromise({
initialState: createInitialState(),
promise: new Promise<TValue>((resolve, reject) => {
return createInternalCancelablePromise(
new Promise<TValue>((resolve, reject) => {
return executor(resolve, reject);
}),
});
{ isCanceled: false, onCancelList: [] }
);
}

createCancelablePromise.resolve = <TValue>(
Expand All @@ -126,16 +121,9 @@ createCancelablePromise.reject = (reason?: any) =>
cancelable(Promise.reject(reason));

export function cancelable<TValue>(promise: Promise<TValue>) {
return createCancelable(promise, createInitialState());
}

function createCancelable<TValue>(
promise: Promise<TValue>,
initialState: CancelablePromiseState
) {
return createInternalCancelablePromise<TValue>({
promise,
initialState,
return createInternalCancelablePromise(promise, {
isCanceled: false,
onCancelList: [],
});
}

Expand All @@ -156,7 +144,3 @@ function createCallback(
return onResult(arg);
};
}

function createInitialState(): CancelablePromiseState {
return { isCanceled: false, onCancelList: [] };
}