handler.ts 876 B

123456789101112131415161718192021222324252627
  1. import { AppKernel } from "@scripts/app/AppKernel";
  2. type StoreTarget = Record<string, unknown>;
  3. type StoreMethod = (...args: unknown[]) => unknown;
  4. /**
  5. * 调用所有 Store 上存在的同名方法。
  6. * 默认清空 Store 状态;可传入方法名及其参数执行其他批量操作。
  7. */
  8. export async function emitEvents(eventName = "clear", ...args: unknown[]): Promise<void> {
  9. const tasks: Promise<unknown>[] = [];
  10. for (const target of Object.values(AppKernel.stores)) {
  11. if (!target || typeof target !== "object") continue;
  12. const method = (target as unknown as StoreTarget)[eventName];
  13. if (typeof method !== "function") continue;
  14. tasks.push(Promise.resolve((method as StoreMethod).apply(target, args)));
  15. }
  16. await Promise.all(tasks);
  17. }
  18. export async function onUserLogout() {
  19. return emitEvents("onUserLogout")
  20. }