-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bot/modules/events: convert to TS (#625)
Convert bot/modules/eventsmodule to TypeScript.
- Loading branch information
1 parent
695a91b
commit 4a5d20b
Showing
6 changed files
with
66 additions
and
53 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |