chore(pockethost): remove action/filter typings

This commit is contained in:
Ben Allfree 2024-06-06 23:28:41 -07:00
parent 9ed4fbbe3c
commit b55b3651da
3 changed files with 56 additions and 112 deletions

View File

@ -1,6 +1,5 @@
import { import {
LogLevelName, LogLevelName,
PocketHostAction,
PocketHostPlugin, PocketHostPlugin,
isLevelGte, isLevelGte,
isLevelLte, isLevelLte,
@ -18,7 +17,9 @@ export const LogLevelConsoleMap = {
} as const } as const
const plugin: PocketHostPlugin = async ({ registerAction }) => { const plugin: PocketHostPlugin = async ({ registerAction }) => {
registerAction(PocketHostAction.Log, (currentLevel, levelIn, args) => { registerAction(
`log`,
async (currentLevel: LogLevelName, levelIn: LogLevelName, args: any[]) => {
if (!isLevelGte(levelIn, currentLevel)) return if (!isLevelGte(levelIn, currentLevel)) return
const finalArgs = [args.shift()] const finalArgs = [args.shift()]
while (args.length > 0) { while (args.length > 0) {
@ -46,7 +47,8 @@ const plugin: PocketHostPlugin = async ({ registerAction }) => {
finalArgs.push(arg) finalArgs.push(arg)
} }
LogLevelConsoleMap[levelIn](...finalArgs) LogLevelConsoleMap[levelIn](...finalArgs)
}) },
)
} }
export default plugin export default plugin

View File

@ -2,7 +2,7 @@
import chalk from 'chalk' import chalk from 'chalk'
import stringify from 'json-stringify-safe' import stringify from 'json-stringify-safe'
import { PocketHostAction, action, mergeConfig, mkSingleton } from '.' import { action, mergeConfig, mkSingleton } from '.'
export type LoggerConfig = { export type LoggerConfig = {
level: LogLevelName level: LogLevelName
@ -74,7 +74,7 @@ export const createLogger = (config: Partial<LoggerConfig>) => {
.join(' ') .join(' ')
const _log = (levelIn: LogLevelName, ...args: any[]) => { const _log = (levelIn: LogLevelName, ...args: any[]) => {
action(PocketHostAction.Log, _config.level, levelIn, args) action(`log`, _config.level, levelIn, args)
} }
const raw = (...args: any[]) => { const raw = (...args: any[]) => {

View File

@ -1,107 +1,49 @@
import { isString } from '@s-libs/micro-dash' import { isString } from '@s-libs/micro-dash'
import { Request } from 'express'
import { InstanceFields, LogLevelName } from '.'
export enum PocketHostAction { export type PocketHostAction = string
Request = 'request',
BeforeDaemonStart = 'before_daemon_start',
Log = 'log',
InstanceApiTimeout = 'instance_api_timeout',
UpdateInstance = 'update_instance',
}
export enum PocketHostFilter { export type PocketHostFilter = string
ExtraBinds = 'extra_binds',
}
export type PocketHostPlugin = (api: PocketHostPluginApi) => Promise<void> export type PocketHostPlugin = (api: PocketHostPluginApi) => Promise<void>
const filters: { [key in PocketHostFilter]: FilterHandler[] } = { const filters: { [key in PocketHostFilter]: FilterHandler[] } = {}
[PocketHostFilter.ExtraBinds]: [],
}
const actions: { const actions: {
[key in PocketHostAction]: ActionHandler[] [key in PocketHostAction]: ActionHandler[]
} = { } = {}
[PocketHostAction.Request]: [],
[PocketHostAction.BeforeDaemonStart]: [],
[PocketHostAction.Log]: [],
[PocketHostAction.InstanceApiTimeout]: [],
[PocketHostAction.UpdateInstance]: [],
}
export type FilterHandler = (carry: any) => any export type FilterHandler = (carry: any) => any
export type ActionHandler = (...args: any[]) => void export type ActionHandler = (...args: any[]) => void
export async function registerFilter( export async function registerFilter(
filter: PocketHostFilter.ExtraBinds, filter: PocketHostFilter,
handler: (carry: string[]) => string[], handler: (carry: any) => any,
) { ) {
filters[filter].push(handler) if (!(filter in filters)) filters[filter] = []
filters[filter]!.push(handler)
} }
export async function registerAction(
action: PocketHostAction.BeforeDaemonStart,
handler: (req: Request) => void,
): Promise<void>
export async function registerAction(
action: PocketHostAction.Request,
handler: (req: Request) => void,
): Promise<void>
export async function registerAction(
action: PocketHostAction.Log,
handler: (
currentLevel: LogLevelName,
level: LogLevelName,
args: any[],
) => void,
): Promise<void>
export async function registerAction(
action: PocketHostAction.InstanceApiTimeout,
handler: (instance: InstanceFields) => void,
): Promise<void>
export async function registerAction(
action: PocketHostAction.UpdateInstance,
handler: (id: string, fields: InstanceFields) => void,
): Promise<void>
export async function registerAction( export async function registerAction(
action: PocketHostAction, action: PocketHostAction,
handler: ActionHandler, handler: (...args: any[]) => Promise<void>,
): Promise<void> {
actions[action].push(handler)
}
export async function filter(
filter: PocketHostFilter.ExtraBinds,
initial: string[],
) { ) {
return filters[filter].reduce((carry, handler) => handler(carry), initial) if (!(action in actions)) actions[action] = []
actions[action]!.push(handler)
} }
export async function action( export async function filter<T>(filterName: PocketHostFilter, initialValue: T) {
action: PocketHostAction.Request, const filter = filters[filterName]
req: Request, if (!filter) return initialValue
): Promise<void> return filter.reduce(
export async function action( (carry, handler) => carry.then((carryRes) => handler(carryRes)),
action: PocketHostAction.BeforeDaemonStart, Promise.resolve(initialValue),
): Promise<void> )
export async function action( }
action: PocketHostAction.Log,
currentLevel: LogLevelName, export async function action(actionName: PocketHostAction, ...rest: any[]) {
levelIn: LogLevelName, const action = actions[actionName]
...rest: any[] if (!action) return
): Promise<void> await Promise.all(action.map((handler) => handler(...rest)))
export async function action(
action: PocketHostAction.InstanceApiTimeout,
instance: InstanceFields,
): Promise<void>
export async function action(
action: PocketHostAction.UpdateInstance,
id: string,
fields: Partial<InstanceFields>,
): Promise<void>
export async function action(action: PocketHostAction, ...args: any[]) {
await Promise.all(actions[action].map((handler) => handler(...args)))
} }
export type PocketHostPluginApi = { export type PocketHostPluginApi = {