-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #760 from interval/add-remove-actions
Allow dynamically adding/removing actions and groups after listen
- Loading branch information
Showing
7 changed files
with
353 additions
and
169 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,21 +1,55 @@ | ||
import { IntervalActionDefinitions } from '../types' | ||
import { Evt } from 'evt' | ||
import { IntervalActionDefinition, IntervalActionDefinitions } from '../types' | ||
|
||
export interface ActionGroupConfig { | ||
name: string | ||
actions: IntervalActionDefinitions | ||
actions?: IntervalActionDefinitions | ||
groups?: Record<string, ActionGroup> | ||
} | ||
|
||
export default class ActionGroup { | ||
name: string | ||
actions: IntervalActionDefinitions | ||
groups: Record<string, ActionGroup> = {} | ||
|
||
onChange: Evt<void> | ||
#groupChangeCtx = Evt.newCtx() | ||
|
||
constructor(config: ActionGroupConfig) { | ||
this.name = config.name | ||
this.actions = config.actions | ||
this.actions = config.actions ?? {} | ||
this.groups = config.groups ?? {} | ||
this.onChange = new Evt() | ||
|
||
for (const group of Object.values(this.groups)) { | ||
group.onChange.attach(this.#groupChangeCtx, this.onChange.post) | ||
} | ||
} | ||
|
||
use(groupSlug: string, group: ActionGroup) { | ||
addGroup(groupSlug: string, group: ActionGroup) { | ||
group.onChange.attach(this.#groupChangeCtx, this.onChange.post) | ||
this.groups[groupSlug] = group | ||
this.onChange.post() | ||
} | ||
|
||
removeGroup(groupSlug: string) { | ||
const group = this.groups[groupSlug] | ||
if (!group) return | ||
|
||
group.onChange.detach(this.#groupChangeCtx) | ||
delete this.groups[groupSlug] | ||
} | ||
|
||
add(slug: string, action: IntervalActionDefinition) { | ||
this.actions[slug] = action | ||
|
||
this.onChange.post() | ||
} | ||
|
||
remove(slug: string) { | ||
if (this.actions[slug]) { | ||
delete this.actions[slug] | ||
this.onChange.post() | ||
} | ||
} | ||
} |
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,128 @@ | ||
import { z } from 'zod' | ||
import fetch from 'node-fetch' | ||
import * as superjson from 'superjson' | ||
import Logger from './Logger' | ||
import Interval, { IntervalError, QueuedAction } from '..' | ||
import { ENQUEUE_ACTION, DEQUEUE_ACTION } from '../internalRpcSchema' | ||
|
||
/** | ||
* This is effectively a namespace inside of Interval with a little bit of its own state. | ||
*/ | ||
export default class Actions { | ||
protected interval: Interval | ||
#logger: Logger | ||
#apiKey?: string | ||
#endpoint: string | ||
|
||
constructor( | ||
interval: Interval, | ||
endpoint: string, | ||
logger: Logger, | ||
apiKey?: string | ||
) { | ||
this.interval = interval | ||
this.#apiKey = apiKey | ||
this.#logger = logger | ||
this.#endpoint = endpoint + '/api/actions' | ||
} | ||
|
||
#getAddress(path: string): string { | ||
if (path.startsWith('/')) { | ||
path = path.substring(1) | ||
} | ||
|
||
return `${this.#endpoint}/${path}` | ||
} | ||
|
||
async enqueue( | ||
slug: string, | ||
{ assignee, params }: Pick<QueuedAction, 'assignee' | 'params'> = {} | ||
): Promise<QueuedAction> { | ||
let body: z.infer<typeof ENQUEUE_ACTION['inputs']> | ||
try { | ||
const { json, meta } = params | ||
? superjson.serialize(params) | ||
: { json: undefined, meta: undefined } | ||
body = ENQUEUE_ACTION.inputs.parse({ | ||
assignee, | ||
slug, | ||
params: json, | ||
paramsMeta: meta, | ||
}) | ||
} catch (err) { | ||
this.#logger.debug(err) | ||
throw new IntervalError('Invalid input.') | ||
} | ||
|
||
const response = await fetch(this.#getAddress('enqueue'), { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.#apiKey}`, | ||
}, | ||
body: JSON.stringify(body), | ||
}) | ||
.then(r => r.json()) | ||
.then(r => ENQUEUE_ACTION.returns.parseAsync(r)) | ||
.catch(err => { | ||
this.#logger.debug(err) | ||
throw new IntervalError('Received invalid API response.') | ||
}) | ||
|
||
if (response.type === 'error') { | ||
throw new IntervalError( | ||
`There was a problem enqueuing the action: ${response.message}` | ||
) | ||
} | ||
|
||
return { | ||
id: response.id, | ||
assignee, | ||
params, | ||
} | ||
} | ||
|
||
async dequeue(id: string): Promise<QueuedAction> { | ||
let body: z.infer<typeof DEQUEUE_ACTION['inputs']> | ||
try { | ||
body = DEQUEUE_ACTION.inputs.parse({ | ||
id, | ||
}) | ||
} catch (err) { | ||
this.#logger.debug(err) | ||
throw new IntervalError('Invalid input.') | ||
} | ||
|
||
const response = await fetch(this.#getAddress('dequeue'), { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.#apiKey}`, | ||
}, | ||
body: JSON.stringify(body), | ||
}) | ||
.then(r => r.json()) | ||
.then(r => DEQUEUE_ACTION.returns.parseAsync(r)) | ||
.catch(err => { | ||
this.#logger.debug(err) | ||
throw new IntervalError('Received invalid API response.') | ||
}) | ||
|
||
if (response.type === 'error') { | ||
throw new IntervalError( | ||
`There was a problem enqueuing the action: ${response.message}` | ||
) | ||
} | ||
|
||
let { type, params, paramsMeta, ...rest } = response | ||
|
||
if (paramsMeta && params) { | ||
params = superjson.deserialize({ json: params, meta: paramsMeta }) | ||
} | ||
|
||
return { | ||
...rest, | ||
params, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.