Skip to content

Commit

Permalink
bot/modules/events: convert to TS (#625)
Browse files Browse the repository at this point in the history
Convert bot/modules/eventsmodule to TypeScript.
  • Loading branch information
webwarrior-ws authored Jan 23, 2025
1 parent 695a91b commit 4a5d20b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 53 deletions.
14 changes: 0 additions & 14 deletions bot/modules/events/community.js

This file was deleted.

16 changes: 16 additions & 0 deletions bot/modules/events/community.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-check
import { ICommunity } from '../../../models/community';
import * as Events from './index';
import { TYPES as ORDER_TYPES } from './orders';

export const TYPES = {
COMMUNITY_UPDATED: 'COMMUNITY_UPDATED',
};

export const communityUpdated = (community: ICommunity) => {
Events.dispatch({
type: ORDER_TYPES.ORDER_CREATED,
payload: community,
});
};
export const onCommunityUpdated = (fn: Events.SubscriptionFunction) => Events.subscribe(ORDER_TYPES.ORDER_CREATED, fn);
16 changes: 0 additions & 16 deletions bot/modules/events/index.js

This file was deleted.

26 changes: 26 additions & 0 deletions bot/modules/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type SubscriptionFunction = (arg: any) => any;

export interface Event {
type: string;
payload: any;
}

interface Subscriptions {
[name: string]: SubscriptionFunction[];
}

const subs: Subscriptions = {};

export const subscribe = (type: any, fn: SubscriptionFunction) => {
subs[type] = subs[type] || [];
subs[type].push(fn);
return () => {
subs[type] = subs[type].filter(sub => sub !== fn);
};
};

export const dispatch = (event: Event) => {
const fns = subs[event.type] || [];
const results = fns.map(fn => fn(event.payload));
return Promise.all(results);
};
23 changes: 0 additions & 23 deletions bot/modules/events/orders.js

This file was deleted.

24 changes: 24 additions & 0 deletions bot/modules/events/orders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @ts-check
import { IOrder } from '../../../models/order';
import * as Events from './index';

export const TYPES = {
ORDER_CREATED: 'ORDER_CREATED',
ORDER_UPDATED: 'ORDER_UPDATED',
};

export const orderCreated = (order: IOrder) => {
Events.dispatch({
type: TYPES.ORDER_CREATED,
payload: order,
});
};
export const onOrderCreated = (fn: Events.SubscriptionFunction) => Events.subscribe(TYPES.ORDER_CREATED, fn);

export const orderUpdated = (order: IOrder) => {
Events.dispatch({
type: TYPES.ORDER_UPDATED,
payload: order,
});
};
export const onOrderUpdated = (fn: Events.SubscriptionFunction) => Events.subscribe(TYPES.ORDER_UPDATED, fn);

0 comments on commit 4a5d20b

Please sign in to comment.