MCPcopy Create free account
hub / github.com/Snowflyt/tinyeffect / handle

Method handle

src/effected.ts:702–753  ·  view source on GitHub ↗
(
    name: string | symbol | ((name: string | symbol) => boolean),
    handler: (...args: any[]) => unknown,
  )

Source from the content-addressed store, hash-verified

700 : never,
701 ): Effected<ExcludeEffect<E, Effect<Name>> | F, R | T>;
702 handle(
703 name: string | symbol | ((name: string | symbol) => boolean),
704 handler: (...args: any[]) => unknown,
705 ): Effected<any, unknown> {
706 const matchEffect = (value: unknown) =>
707 value instanceof Effect &&
708 (typeof name === "function" ? name(value.name) : value.name === name);
709
710 return effected(() => {
711 const iterator = this[Symbol.iterator]();
712 const context = {
713 interceptIterator: null as typeof iterator | null,
714
715 terminated: false as false | "with-value" | "without-value",
716 terminatedValue: undefined as unknown,
717 };
718
719 return {
720 next: (...args: [] | [unknown]) => {
721 if (context.terminated)
722 return {
723 done: true,
724 ...(context.terminated === "with-value" ? { value: context.terminatedValue } : {}),
725 } as IteratorReturnResult<unknown>;
726
727 const result = (context.interceptIterator || iterator).next(...args);
728
729 const { done, value } = result;
730 if (done) return result;
731
732 if (matchEffect(value)) return handleEffect(context, name, value, handler as never);
733
734 if (value instanceof Effect && typeof (value as any).defaultHandler === "function") {
735 const originalDefaultHandler = (value as any).defaultHandler;
736 (value as any).defaultHandler = (context: any, ...payloads: unknown[]) => {
737 const result = originalDefaultHandler(context, ...payloads);
738 if (result instanceof Effected) return result.handle(name as never, handler as never);
739 if (isGenerator(result)) {
740 return effected(() => result as Generator<Effect, unknown, unknown>).handle(
741 name as never,
742 handler as never,
743 );
744 }
745 return result;
746 };
747 }
748
749 return result;
750 },
751 };
752 });
753 }
754
755 /**
756 * Resume an effect with the return value of the handler.

Callers 3

resumeMethod · 0.95
terminateMethod · 0.95
catchAllMethod · 0.95

Calls 4

effectedFunction · 0.85
handleEffectFunction · 0.85
isGeneratorFunction · 0.85
handleMethod · 0.65

Tested by

no test coverage detected