當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript eventemitter2.EventEmitter2類代碼示例

本文整理匯總了TypeScript中eventemitter2.EventEmitter2的典型用法代碼示例。如果您正苦於以下問題:TypeScript EventEmitter2類的具體用法?TypeScript EventEmitter2怎麽用?TypeScript EventEmitter2使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了EventEmitter2類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: addEventListener

export function addEventListener(name, fn) {
  emitter.addListener(name, fn)
}
開發者ID:dianpeng,項目名稱:fly,代碼行數:3,代碼來源:events.ts

示例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"])
  }
}
開發者ID:dianpeng,項目名稱:fly,代碼行數:80,代碼來源:events.ts

示例3: dispatchEvent

export function dispatchEvent(event) {
  emitter.emit(event.type, event)
}
開發者ID:dianpeng,項目名稱:fly,代碼行數:3,代碼來源:events.ts

示例4:

export const emitLog = (logData: any) => {
  emitter.emit("log", logData);
};
開發者ID:concord-consortium,項目名稱:lara,代碼行數:3,代碼來源:events.ts


注:本文中的eventemitter2.EventEmitter2類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。