Skip to content

Instantly share code, notes, and snippets.

@paprikka
Created December 5, 2022 20:40
Show Gist options
  • Save paprikka/b8e129a8ca66d5afbe23ea18651cb3dc to your computer and use it in GitHub Desktop.
Save paprikka/b8e129a8ca66d5afbe23ea18651cb3dc to your computer and use it in GitHub Desktop.
queue.example.ts
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