本文整理匯總了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);
};