Skip to content

Commit

Permalink
promisified
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Jun 5, 2024
1 parent 45889a4 commit 07f5c12
Show file tree
Hide file tree
Showing 29 changed files with 782 additions and 346 deletions.
3 changes: 2 additions & 1 deletion lib/add.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TaskSpec } from 'ordu';
declare function api_add(this: any): any;
declare function api_message(this: any, pattern0: any, pattern1: any, action: any): any;
declare const task: {
translate(spec: TaskSpec): {
op: string;
Expand Down Expand Up @@ -60,4 +61,4 @@ declare const task: {
op: string;
};
};
export { api_add, task, };
export { api_add, api_message, task, };
35 changes: 34 additions & 1 deletion lib/add.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/add.js.map

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions lib/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,47 @@ function api_add(this: any) {
}


function api_message(this: any, pattern0: any, pattern1: any, action: any) {
let actfunc = action || pattern1
const action_wrapper: any =
null == actfunc
? null
: function(this: any, msg: any, reply: any, meta: any) {
actfunc.call(this, msg, meta).then(reply).catch(reply)
}

if (null != actfunc && null != action_wrapper) {
if ('' != actfunc.name) {
Object.defineProperty(action_wrapper, 'name', { value: actfunc.name })
}

for (var p in actfunc) {
action_wrapper[p] = actfunc[p]
}

// NOTE: also set properties defined after call to seneca.message
setImmediate(function() {
for (var p in actfunc) {
action_wrapper[p] = actfunc[p]
}
})
}

if (action) {
this.add(pattern0, pattern1, action_wrapper)
}
else if (action_wrapper) {
this.add(pattern0, action_wrapper)
}
else {
this.add(pattern0)
}

return this
}



const task = {
translate(spec: TaskSpec) {
const args = spec.ctx.args
Expand Down Expand Up @@ -411,5 +452,6 @@ const intern = (module.exports.intern = {

export {
api_add,
api_message,
task,
}
4 changes: 4 additions & 0 deletions lib/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ declare function reply(this: any, spec: any): boolean;
declare function listen(this: any, callpoint: any): (this: any, ...argsarr: any[]) => any;
declare function client(this: any, callpoint: any): (this: any) => any;
declare function decorate(this: any): void;
declare function prepare(this: any, prepareAction: Function): any;
declare function destroy(this: any, destroyAction: any): any;
declare let API: {
wrap: typeof wrap;
fix: typeof fix;
Expand Down Expand Up @@ -78,5 +80,7 @@ declare let API: {
listen: typeof listen;
client: typeof client;
decorate: typeof decorate;
prepare: typeof prepare;
destroy: typeof destroy;
};
export { API };
45 changes: 45 additions & 0 deletions lib/api.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/api.js.map

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,67 @@ function decorate(this: any) {
}


function prepare(this: any, prepareAction: Function) {
if (null == prepareAction || 'function' != typeof prepareAction) {
throw new Error('seneca: first argument to prepare must be a function')
}

async function prepare_wrapper(this: any, msg: any) {
await prepareAction.call(this, msg)
return this.prior(msg)
}

if ('' != prepareAction.name) {
Object.defineProperty(prepare_wrapper, 'name', {
value: 'prepare_' + prepareAction.name,
})
}

const plugin = this.plugin

let pat = {
role: 'seneca',
plugin: 'init',
init: plugin.name,
tag: undefined,
}

if (null != plugin.tag && '-' != plugin.tag) {
pat.tag = plugin.tag
}

this.message(pat, prepare_wrapper)

this.plugin.prepare = this.plugin.prepare || []
this.plugin.prepare.push(prepareAction)

return this
}


function destroy(this: any, destroyAction: any) {
async function destroy_wrapper(this: any, msg: any) {
await destroyAction.call(this, msg)
return this.prior(msg)
}

if ('' != destroyAction.name) {
Object.defineProperty(destroy_wrapper, 'name', {
value: 'destroy_' + destroyAction.name,
})
}

this.message('role:seneca,cmd:close', destroy_wrapper)

this.plugin.destroy = this.plugin.destroy || []
this.plugin.destroy.push(destroyAction)

return this
}





intern.parse_config = function(args: any) {
let out: any = {}
Expand Down Expand Up @@ -1031,6 +1092,8 @@ let API = {
listen,
client,
decorate,
prepare,
destroy,
}


Expand Down
65 changes: 7 additions & 58 deletions lib/prior.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/prior.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 07f5c12

Please sign in to comment.