本文整理汇总了TypeScript中eventemitter2.EventEmitter2类的典型用法代码示例。如果您正苦于以下问题:TypeScript EventEmitter2类的具体用法?TypeScript EventEmitter2怎么用?TypeScript EventEmitter2使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EventEmitter2类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: addEventListener
export function addEventListener(name, fn) {
emitter.addListener(name, fn)
}
示例2: fireFetchEvent
export function fireFetchEvent(url, req, body, callback) {
logger.debug("handling request event")
const selfCleaningCallback = (...args) => {
callback.apply(null, args)
try {
callback.release()
} catch (e) {
// ignore
}
if (body) {
try {
body.release()
} catch (e) {
// ignore
}
}
}
const fetchEvent = new FetchEvent(
"fetch",
{
request: new Request(
url,
Object.assign(req, {
body: (body && refToStream(body)) || null
})
)
},
async function fetchEventCallback(err, res) {
logger.debug("request event callback called", typeof err, typeof res, res instanceof Response)
if (err) {
logger.error(err, err.stack)
return selfCleaningCallback.apply(null, [err.toString()])
}
if (res.bodyUsed) {
return selfCleaningCallback.apply(null, [new Error("Body has already been used").toString()])
}
let b = null
if (isFlyStream(res.body)) {
b = res.body.flyStreamId
} else {
logger.debug("body source type:", res.bodySource.constructor.name)
if (typeof res.bodySource === "string") {
b = res.bodySource
} else {
b = bridge.wrapValue(await res.arrayBuffer())
}
}
logger.debug("got ourselves a body")
selfCleaningCallback.apply(undefined, [
null,
bridge.wrapValue({
headers: (res.headers && res.headers.toJSON()) || {},
status: res.status
}),
b
])
}
)
const fn = emitter.listeners("fetch").slice(-1)[0]
if (typeof fn !== "function") {
return selfCleaningCallback.apply(null, [
"No HTTP handler attached: make sure your app calls `fly.http.respondWith(...)."
])
}
if ((fn(fetchEvent) as any) instanceof Promise) {
return selfCleaningCallback.apply(null, ["'fetch' event handler function cannot return a promise."])
}
if (!fetchEvent.respondWithEntered) {
return selfCleaningCallback.apply(null, ["respondWith was not called for FetchEvent"])
}
}
示例3: dispatchEvent
export function dispatchEvent(event) {
emitter.emit(event.type, event)
}
示例4:
export const emitLog = (logData: any) => {
emitter.emit("log", logData);
};