Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bot/modules/events: convert to TS #625

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading