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

feat: add async interceptor #3894

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
95 changes: 94 additions & 1 deletion packages/core/src/common/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { remove } from './array';
import type { Nullable } from '../shared/types';
import { remove } from './array';

export type InterceptorHandler<M = unknown, C = unknown> = (
value: Nullable<M>,
Expand Down Expand Up @@ -118,3 +118,96 @@ export class InterceptorManager<P extends Record<string, IInterceptor<any, any>>
this._interceptorsByName.clear();
}
}

export function createAsyncInterceptorKey<T, C>(key: string): IAsyncInterceptor<T, C> {
const symbol = `sheet_interceptor_${key}`;
ybzky marked this conversation as resolved.
Show resolved Hide resolved
return symbol as unknown as IAsyncInterceptor<T, C>; // FIXME: priority and handler is completely missing?
};

export type AsyncInterceptorHandler<M = unknown, C = unknown> = (
value: Nullable<M>,
context: C,
next?: (value: Nullable<M>) => Promise<Nullable<M>>
) => Promise<Nullable<M>>;

export interface IAsyncInterceptor<M, C> {
priority?: number;
handler: AsyncInterceptorHandler<M, C>;
}

export type IComposeAsyncInterceptors<T = any, C = any> = (
interceptors: Array<IAsyncInterceptor<T, C>>
) => (initValue: Nullable<T>, initContext: C) => Promise<Nullable<T>>;

export const composeAsyncInterceptors = <T, C>(
interceptors: Array<IAsyncInterceptor<T, C>>
): ((initialValue: Nullable<T>, context: C) => Promise<Nullable<T>>) => {
return async function (initialValue: Nullable<T>, context: C) {
let index = -1;
let value: Nullable<T> = initialValue;

for (let i = 0; i <= interceptors.length; i++) {
if (i <= index) {
throw new Error('[SheetInterceptorService]: next() called multiple times!');
}

index = i;

if (i === interceptors.length) {
return value;
}

const interceptor = interceptors[i];
let nextCalled = false;

value = await interceptor.handler!(value, context, async (nextValue) => {
nextCalled = true;
return nextValue;
});

if (!nextCalled) {
break;
}
}

return value;
};
};

export class AsyncInterceptorManager<P extends Record<string, IAsyncInterceptor<any, any>>> {
private _asyncInterceptorsByName: Map<string, Array<IAsyncInterceptor<unknown, unknown>>> = new Map();
private _asyncInterceptorPoints: P;

constructor(asyncInterceptorPoints: P) {
this._asyncInterceptorPoints = asyncInterceptorPoints;
}

public fetchThroughAsyncInterceptors<T, C>(name: IAsyncInterceptor<T, C>) {
const key = name as unknown as string;
const interceptors = this._asyncInterceptorsByName.get(key) as unknown as Array<typeof name>;
return composeAsyncInterceptors(interceptors || []);
}

public async interceptAsync<T extends IAsyncInterceptor<any, any>>(name: T, interceptor: T) {
const key = name as unknown as string;
if (!this._asyncInterceptorsByName.has(key)) {
this._asyncInterceptorsByName.set(key, []);
}
const interceptors = this._asyncInterceptorsByName.get(key)!;
interceptors.push(interceptor);

this._asyncInterceptorsByName.set(
key,
interceptors.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0)) // from large to small
);
return () => remove(this._asyncInterceptorsByName.get(key)!, interceptor);
}

public getInterceptPoints() {
return this._asyncInterceptorPoints;
}

public dispose() {
this._asyncInterceptorsByName.clear();
}
}
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export { shallowEqual } from './common/equal';
export { CustomCommandExecutionError } from './common/error';
export { throttle } from './common/function';
export type { ICellInterceptor, IComposeInterceptors, IInterceptor, InterceptorHandler } from './common/interceptor';
export { composeInterceptors, createInterceptorKey, InterceptorEffectEnum, InterceptorManager } from './common/interceptor';
export { AsyncInterceptorManager, composeInterceptors, createAsyncInterceptorKey, createInterceptorKey, InterceptorEffectEnum, InterceptorManager } from './common/interceptor';
export type { Serializable } from './common/json';
export { MemoryCursor } from './common/memory-cursor';
export { mixinClass } from './common/mixin';
Expand Down
Loading