-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpromisify.js
181 lines (143 loc) · 4.04 KB
/
promisify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Copyright (c) 2018-2022 voxgig and other contributors, MIT License */
'use strict'
// A promise interface for Seneca
var Util = require('util')
module.exports = promisify
module.exports.preload = function preload_promisify(plugin) {
var self = this
// Promisify builtin to Seneca >= 4
if (!self.version.startsWith('3.')) {
return
}
var options = plugin.options
var actives = {
...plugin.defaults.active,
...plugin.options.active,
}
self.root.send = function (msg) {
this.act(msg)
return this
}
if (actives.post) {
self.root.post = Util.promisify(this.act)
}
if (actives.message) {
self.root.message = function (pattern0, pattern1, action) {
let actfunc = action || pattern1
var action_wrapper =
null == actfunc
? null
: function (msg, reply, meta) {
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
}
}
if (actives.prepare) {
self.root.prepare = function (prepare) {
async function prepare_wrapper(msg) {
await prepare.call(this, msg)
return this.prior(msg)
}
if ('' != prepare.name) {
Object.defineProperty(prepare_wrapper, 'name', {
value: 'prepare_' + prepare.name,
})
}
const plugin = this.plugin
let pat = {
role: 'seneca',
plugin: 'init',
init: plugin.name,
}
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(prepare)
return this
}
}
if (actives.destroy) {
self.root.destroy = function (destroy) {
async function destroy_wrapper(msg) {
await destroy.call(this, msg)
return this.prior(msg)
}
if ('' != destroy.name) {
Object.defineProperty(destroy_wrapper, 'name', {
value: 'destroy_' + destroy.name,
})
}
this.message('role:seneca,cmd:close', destroy_wrapper)
this.plugin.destroy = this.plugin.destroy || []
this.plugin.destroy.push(destroy)
return this
}
}
const __prior$$ = self.root.prior
const __priorAsync$$ = Util.promisify(self.root.prior)
if (actives.prior) {
self.root.prior = async function () {
var last_is_func =
1 < arguments.length &&
'function' == typeof arguments[arguments.length - 1]
if (last_is_func) {
return __prior$$.apply(this, arguments)
} else {
return await __priorAsync$$.apply(this, arguments)
}
}
}
const __ready$$ = self.root.ready
const __readyAsync$$ = Util.promisify(self.root.ready)
if (actives.ready) {
self.root.ready = async function () {
var last_is_func =
0 < arguments.length &&
'function' == typeof arguments[arguments.length - 1]
if (last_is_func) {
return __ready$$.apply(this, arguments)
} else {
await __readyAsync$$.apply(this, arguments)
return this
}
}
}
self.root.__promisify$$ = true
}
function promisify(options) {}
promisify.defaults = {
active: {
post: true,
message: true,
prepare: true,
destroy: true,
prior: true,
ready: true,
},
}
// Prevent name mangling
Object.defineProperty(promisify, 'name', { value: 'promisify' })