From a57a32a6dd5d3b22b4a1f2373e4c3104c1a29089 Mon Sep 17 00:00:00 2001 From: webwarrior Date: Wed, 7 Aug 2024 10:57:31 +0200 Subject: [PATCH] bot/modules/events: convert to TS Convert bot/modules/eventsmodule to TypeScript. --- bot/modules/events/community.js | 14 -------------- bot/modules/events/community.ts | 16 ++++++++++++++++ bot/modules/events/index.js | 16 ---------------- bot/modules/events/index.ts | 26 ++++++++++++++++++++++++++ bot/modules/events/orders.js | 23 ----------------------- bot/modules/events/orders.ts | 24 ++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 53 deletions(-) delete mode 100644 bot/modules/events/community.js create mode 100644 bot/modules/events/community.ts delete mode 100644 bot/modules/events/index.js create mode 100644 bot/modules/events/index.ts delete mode 100644 bot/modules/events/orders.js create mode 100644 bot/modules/events/orders.ts diff --git a/bot/modules/events/community.js b/bot/modules/events/community.js deleted file mode 100644 index d06ff7d6..00000000 --- a/bot/modules/events/community.js +++ /dev/null @@ -1,14 +0,0 @@ -// @ts-check -const Events = require('./index'); - -const TYPES = (exports.TYPES = { - COMMUNITY_UPDATED: 'COMMUNITY_UPDATED', -}); - -exports.communityUpdated = community => { - Events.dispatch({ - type: TYPES.ORDER_CREATED, - payload: community, - }); -}; -exports.onCommunityUpdated = fn => Events.subscribe(TYPES.ORDER_CREATED, fn); diff --git a/bot/modules/events/community.ts b/bot/modules/events/community.ts new file mode 100644 index 00000000..3fccc45d --- /dev/null +++ b/bot/modules/events/community.ts @@ -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); diff --git a/bot/modules/events/index.js b/bot/modules/events/index.js deleted file mode 100644 index 26ea2daf..00000000 --- a/bot/modules/events/index.js +++ /dev/null @@ -1,16 +0,0 @@ -const subs = {}; - -exports.subscribe = (type, fn) => { - if (typeof fn !== 'function') throw new Error('HandlerNotAFunction'); - subs[type] = subs[type] || []; - subs[type].push(fn); - return () => { - subs[type] = subs[type].filter(sub => sub !== fn); - }; -}; - -exports.dispatch = event => { - const fns = subs[event.type] || []; - const results = fns.map(fn => fn(event.payload)); - return Promise.all(results); -}; diff --git a/bot/modules/events/index.ts b/bot/modules/events/index.ts new file mode 100644 index 00000000..77303d4b --- /dev/null +++ b/bot/modules/events/index.ts @@ -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); +}; diff --git a/bot/modules/events/orders.js b/bot/modules/events/orders.js deleted file mode 100644 index b7789b2b..00000000 --- a/bot/modules/events/orders.js +++ /dev/null @@ -1,23 +0,0 @@ -// @ts-check -const Events = require('./index'); - -const TYPES = (exports.TYPES = { - ORDER_CREATED: 'ORDER_CREATED', - ORDER_UPDATED: 'ORDER_UPDATED', -}); - -exports.orderCreated = order => { - Events.dispatch({ - type: TYPES.ORDER_CREATED, - payload: order, - }); -}; -exports.onOrderCreated = fn => Events.subscribe(TYPES.ORDER_CREATED, fn); - -exports.orderUpdated = order => { - Events.dispatch({ - type: TYPES.ORDER_UPDATED, - payload: order, - }); -}; -exports.onOrderUpdated = fn => Events.subscribe(TYPES.ORDER_UPDATED, fn); diff --git a/bot/modules/events/orders.ts b/bot/modules/events/orders.ts new file mode 100644 index 00000000..a38784cf --- /dev/null +++ b/bot/modules/events/orders.ts @@ -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);