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

worker: shared async context #492

Open
myfreeer opened this issue Mar 10, 2022 · 0 comments
Open

worker: shared async context #492

myfreeer opened this issue Mar 10, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@myfreeer
Copy link
Member

myfreeer commented Mar 10, 2022

The async context should be shared pre PipelineExecutor, and can be obtained from instances of PipelineExecutor.
The async context should be shared between main thread and worker threads, and should not affect existing message channels.
Keys and values should be stored in the main thread, and copied to workers when requested.

import type {AsyncResult} from './life-cycle/types';

export interface SharedAsyncLock<T> {
  currentValue?: T;
  unlock(content?: LoadingValue<T>): Promise<void>;
}

export type ContextKey = string | number;
export type ContextKeyMatcher = (key: ContextKey) => boolean;
export type Loader<T> = () => AsyncResult<T>;
export type LoadingValue<T> = AsyncResult<T> | Loader<T>;

export interface SharedAsyncContext {
  put<T>(key: ContextKey, value: AsyncResult<T>): Promise<T | void>;
  putIfAbsent<T>(key: ContextKey, value: LoadingValue<T>): Promise<boolean>;
  putIfPreset<T>(key: ContextKey, value: LoadingValue<T>): Promise<boolean>;
  contains<T>(key: ContextKey, value?: AsyncResult<T>): boolean;
  clear(): Promise<boolean>;
  get<T>(key: ContextKey): Promise<T | void>;
  getOrLock<T>(key: ContextKey): Promise<T | SharedAsyncLock<T>>;
  lock<T>(key: ContextKey): Promise<SharedAsyncLock<T>>;
  getOrLoad<T>(key: ContextKey, defaultValue: LoadingValue<T>): Promise<T>;
  getOrLoadOnMainThread<T>(key: ContextKey, loaderKey: ContextKey): Promise<T>;
  remove<T>(key: ContextKey): Promise<T | void>;
  remove<T>(key: ContextKey, expectedValue: AsyncResult<T>): Promise<T | void>;
  isLockObject<T>(obj: T | SharedAsyncLock<T>): obj is SharedAsyncLock<T>;
  isOnMainThread(): this is MainThreadSharedAsyncContext;
  call<T>(name: string, ...args: unknown[]): Promise<T>;
}

export interface MainThreadSharedAsyncContext extends SharedAsyncContext {
  registerLoader<T>(key: ContextKey | ContextKeyMatcher, loader: Loader<T>): boolean;
  deregisterLoader<T>(key: ContextKey | ContextKeyMatcher): Loader<T> | void;
  deregisterLoader<T>(loader: Loader<T>): Loader<T> | void;

  registerFunction<T>(name: string, fn: (...args: unknown[]) => T | Promise<T>): boolean;
  deregisterFunction<T>(name: string, fn?: (...args: unknown[]) => T | Promise<T>): boolean;
}
@myfreeer myfreeer added the enhancement New feature or request label Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant