diff --git a/src/index.js b/src/index.js index bc68e2e..7ef57e4 100644 --- a/src/index.js +++ b/src/index.js @@ -27,7 +27,7 @@ export { createProxyWindow, toProxyWindow, } from "./setup"; -export { on, once, send } from "./public"; +export { on, once, send, listenerExists } from "./public"; export { markWindowKnown } from "./lib"; export { cleanUpWindow } from "./clean"; diff --git a/src/public/index.js b/src/public/index.js index 6e3560d..a8abd47 100644 --- a/src/public/index.js +++ b/src/public/index.js @@ -2,3 +2,4 @@ export * from "./on"; export * from "./send"; +export * from "./listenerExists"; diff --git a/src/public/listenerExists.js b/src/public/listenerExists.js new file mode 100644 index 0000000..3025aaf --- /dev/null +++ b/src/public/listenerExists.js @@ -0,0 +1,34 @@ +/* @flow */ + +import { getRequestListener } from "../drivers"; +import type { ServerOptionsType } from "../types"; + +const getDefaultServerOptions = (): ServerOptionsType => { + // $FlowFixMe + return {}; +}; + +export function listenerExists( + name: string, + options: ?ServerOptionsType +): boolean { + if (!name) { + throw new Error("Expected name"); + } + + options = options || getDefaultServerOptions(); + + const win = options.window; + const domain = options.domain; + + if (Array.isArray(domain)) { + for (const item of domain) { + if (getRequestListener({ name, win, domain: item })) { + return true; + } + } + return false; + } + + return Boolean(getRequestListener({ name, win, domain })); +}