Skip to content

Commit

Permalink
fix: typings mapping & clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Feb 26, 2024
1 parent d976bcd commit 5b3cc4a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
"dist"
],
"type": "module",
"typings": "dist/index.d.ts",
"types": "dist/index.d.ts",
"main": "dist/index.mjs",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
"import": {
"default": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"require": {
"default": "./dist/index.cjs",
"types": "./dist/index.d.ts"
}
}
},
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface Bus<T extends Schema> {
function create<T extends Schema>({ schema, validate = true }: BusOptions<T>): Bus<T> {
const subPubPathMap = getSubPubPathMap(schema) as Record<SubscriptionKey<T>, PublishKey<T>[]>;
const eventNames = Array.from(new Set(Object.values(subPubPathMap).flat())) as PublishKey<T>[];
const listeners: Map<PublishKey<T>, Set<unknown>> = new Map();
const listeners = new Map<PublishKey<T>, Set<unknown>>();

const getListenerSetsForSubscriptionKey = (event: SubscriptionKey<T>): Set<unknown>[] => {
if (event === "*") return Array.from(listeners.values());
Expand All @@ -46,12 +46,12 @@ function create<T extends Schema>({ schema, validate = true }: BusOptions<T>): B

const validatePayloadOrPanic = (event: string, data: unknown): void => {
const pathFragments = event.split(".");
let currentSchema: ZodType | Schema = schema;
let currentSchema: Schema | ZodType = schema;
for (const fragment of pathFragments) {
if (typeof (currentSchema as Record<string, unknown>)[fragment] === "undefined") {
if ((currentSchema as Record<string, unknown>)[fragment] === undefined) {
throw new ValidationError(`Invalid event: "${event}". Could not resolve "${fragment}" fragment.`);
}
currentSchema = (currentSchema as Record<string, ZodType | Schema>)[fragment];
currentSchema = (currentSchema as Record<string, Schema | ZodType>)[fragment];
}
if (typeof currentSchema.parse === "function") {
(currentSchema as ZodType).parse(data);
Expand All @@ -76,7 +76,7 @@ function create<T extends Schema>({ schema, validate = true }: BusOptions<T>): B
const unsubscribe = <K extends SubscriptionKey<T>>(event: K, listener?: SubscriptionListeners<T>[K]): void => {
const eventListeners = getListenerSetsForSubscriptionKey(event);
for (const listenerSet of eventListeners) {
if (typeof listener === "undefined") {
if (listener === undefined) {
listenerSet.clear();
} else {
listenerSet.delete(listener);
Expand Down Expand Up @@ -167,7 +167,7 @@ function create<T extends Schema>({ schema, validate = true }: BusOptions<T>): B
event: K,
options: { timeout?: number; filter?: (data: SubscriptionListenerPayloads<T>[K]) => boolean } = {}
) => {
const { timeout = 5_000, filter } = options;
const { timeout = 5000, filter } = options;
return new Promise<SubscriptionListenerPayloads<T>[K]>((resolve, reject) => {
let timeoutId: ReturnType<typeof setTimeout>;
const listener = (data: unknown) => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export const getSubPubPathMap = (schema: Schema | ZodType): Record<string, strin
map[subscribePath] = publishPaths.filter((publishPath) => {
const publishParts = publishPath.split(".");
if (publishParts.length !== subscribeParts.length) return false;
for (let i = 0; i < publishParts.length; i++) {
if (subscribeParts[i] !== "*" && publishParts[i] !== subscribeParts[i]) return false;
for (const [i, publishPart] of publishParts.entries()) {
if (subscribeParts[i] !== "*" && publishPart !== subscribeParts[i]) return false;
}
return true;
});
Expand Down

0 comments on commit 5b3cc4a

Please sign in to comment.