Skip to content

Commit

Permalink
types(vest): shared callback type
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Mar 22, 2022
1 parent 7722de7 commit 6c0d43c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
20 changes: 14 additions & 6 deletions packages/shared/src/bus.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import { CB } from 'utilityTypes';

export function createBus(): {
on: (event: string, handler: (...args: any[]) => void) => { off: () => void };
on: (event: string, handler: CB) => OnReturn;
emit: (event: string, ...args: any[]) => void;
} {
const listeners: Record<string, ((...args: any[]) => void)[]> = {};
const listeners: Record<string, CB[]> = {};

return {
emit(event: string, data: any) {
(listeners[event] || []).forEach(handler => {
listener(event).forEach(handler => {
handler(data);
});
},

on(event: string, handler: (...args: any[]) => void): { off: () => void } {
listeners[event] = (listeners[event] || []).concat(handler);
on(event: string, handler: CB): OnReturn {
listeners[event] = listener(event).concat(handler);

return {
off() {
listeners[event] = listeners[event].filter(h => h !== handler);
listeners[event] = listener(event).filter(h => h !== handler);
},
};
},
};

function listener(event: string): CB[] {
return listeners[event] || [];
}
}

type OnReturn = { off: () => void };
2 changes: 2 additions & 0 deletions packages/shared/src/utilityTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export type DropFirst<T extends unknown[]> = T extends [unknown, ...infer U]
: never;

export type TStringable = string | ((...args: any[]) => string);

export type CB = (...args: any[]) => void;
3 changes: 2 additions & 1 deletion packages/vest/src/core/ctx/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assign from 'assign';
import { createContext } from 'context';
import { createCursor } from 'cursor';
import { CB } from 'utilityTypes';

import { IsolateKeys, IsolateTypes } from 'IsolateTypes';
import { Modes } from 'Modes';
Expand Down Expand Up @@ -52,7 +53,7 @@ type CTXType = {
bus?: {
on: (
event: string,
handler: (...args: any[]) => void
handler: CB
) => {
off: () => void;
};
Expand Down
3 changes: 2 additions & 1 deletion packages/vest/src/core/isolate/isolates/omitWhen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import optionalFunctionValue from 'optionalFunctionValue';
import { CB } from 'utilityTypes';

import { IsolateTypes } from 'IsolateTypes';
import ctx from 'ctx';
Expand All @@ -16,7 +17,7 @@ import { produceSuiteResult, SuiteResult } from 'produceSuiteResult';
*/
export default function omitWhen(
conditional: boolean | ((draft: SuiteResult) => boolean),
callback: (...args: any[]) => void
callback: CB
): void {
isolate({ type: IsolateTypes.OMIT_WHEN }, () => {
ctx.run(
Expand Down
3 changes: 2 additions & 1 deletion packages/vest/src/core/isolate/isolates/skipWhen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import optionalFunctionValue from 'optionalFunctionValue';
import { CB } from 'utilityTypes';

import { IsolateTypes } from 'IsolateTypes';
import ctx from 'ctx';
Expand All @@ -16,7 +17,7 @@ import { produceSuiteResult, SuiteResult } from 'produceSuiteResult';
*/
export default function skipWhen(
conditional: boolean | ((draft: SuiteResult) => boolean),
callback: (...args: any[]) => void
callback: CB
): void {
isolate({ type: IsolateTypes.SKIP_WHEN }, () => {
ctx.run(
Expand Down
3 changes: 1 addition & 2 deletions packages/vest/src/core/suite/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assign from 'assign';
import genId from 'genId';
import invariant from 'invariant';
import isFunction from 'isFunction';
import { CB } from 'utilityTypes';
import { createState } from 'vast';

import { IsolateTypes } from 'IsolateTypes';
Expand All @@ -19,8 +20,6 @@ type CreateProperties = {
remove: (fieldName: string) => void;
};

type CB = (...args: any[]) => void;

export type Suite<T extends CB> = {
(...args: Parameters<T>): SuiteRunResult;
} & CreateProperties;
Expand Down

1 comment on commit 6c0d43c

@vercel
Copy link

@vercel vercel bot commented on 6c0d43c Mar 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next-git-latest-ealush.vercel.app
vest-next-ealush.vercel.app
vest-website.vercel.app
vest-next.vercel.app

Please sign in to comment.