Skip to content

Commit

Permalink
feat(eval): WorkerAPI.prototype.sync()
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 29, 2020
1 parent 7f8534c commit e01f740
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
14 changes: 10 additions & 4 deletions packages/plugin-eval-addons/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ interface AddonContext extends AddonArgv, Context {}
type AddonAction = (ctx: AddonContext) => string | void | Promise<string | void>
const commandMap: Record<string, AddonAction> = {}

WorkerAPI.prototype.callAddon = async function (options, argv) {
const addons: any = {
registerCommand(name: string, callback: AddonAction) {
commandMap[name] = callback
},
}

WorkerAPI.prototype.callAddon = async function (this: WorkerAPI, options, argv) {
const callback = commandMap[argv.name]
try {
const context = { ...argv, ...Context(options) }
const result = await callback(context)
await context.user._update()
const ctx = { ...argv, ...Context(options) }
const result = await callback(ctx)
await this.sync(ctx)
return result
} catch (error) {
if (!argv.options.debug) return logger.warn(error)
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-eval/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ interface TrappedArgv<O> extends ParsedArgv<never, never, O> {
type TrappedAction<O> = (argv: TrappedArgv<O>, ...args: string[]) => ReturnType<CommandAction>

function resolveAccess<T>(fields: Access<T>) {
return Array.isArray(fields) ? { readable: fields } : fields
return Array.isArray(fields)
? { readable: fields, writable: [] }
: { readable: [], writable: [], ...fields }
}

export interface FieldOptions {
Expand All @@ -94,8 +96,8 @@ export interface FieldOptions {
export function attachTraps<O>(command: Command<never, never, O>, options: FieldOptions, action: TrappedAction<O>) {
const userAccess = resolveAccess(options.userFields)
const groupAccess = resolveAccess(options.groupFields)
const userWritable = userAccess.writable || []
const groupWritable = groupAccess.writable || []
const userWritable = userAccess.writable
const groupWritable = groupAccess.writable

command.userFields(userTrap.fields(userAccess.readable))
command.groupFields(groupTrap.fields(groupAccess.readable))
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-eval/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export class WorkerAPI {
return response
}

async sync(ctx: Context) {
await ctx.user?._update()
await ctx.group?._update()
}

async eval(ctxOptions: ContextOptions, evalOptions: EvalOptions) {
const { source, silent } = evalOptions

Expand All @@ -142,8 +147,7 @@ export class WorkerAPI {
filename: 'stdin',
lineOffset: -4,
})
await ctx.user?._update()
await ctx.group?._update()
await this.sync(ctx)
} catch (error) {
return formatError(error)
}
Expand Down

0 comments on commit e01f740

Please sign in to comment.