Created
December 5, 2022 20:40
-
-
Save paprikka/b8e129a8ca66d5afbe23ea18651cb3dc to your computer and use it in GitHub Desktop.
queue.example.ts
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
type User = any | |
type Event = { | |
id: 'user:new', | |
user: User | |
} | { | |
id: 'tick', | |
interval: number | |
} | |
type EventID = Event['id'] // user:new, tick, etc... | |
type Action = { | |
id: 'notification:email', | |
template: string, | |
subject: string, | |
// ... | |
} | |
type ActionID = Action['id'] | |
type Condition = (event: Event) => boolean | |
type Filter = { | |
conditions: Condition[] | |
} | |
type Trigger = { | |
action: Action, | |
when: Filter[] | |
} | |
type Handler = (trigger: Trigger, event: Event) => void | |
let handlers: Record<ActionID, Handler> = { | |
"notification:email": (trigger, event) => { | |
}, | |
} | |
let triggers: Trigger[] = [] // ... | |
onEvent('tick', event => { | |
let triggersToFire= triggers.filter( | |
trigger => trigger.when.every(event) | |
) | |
triggersToFire.forEach(trigger => { | |
handlers[trigger.action.id](trigger, event) | |
}); | |
}) | |
onEvent('db:new-user', event => {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment