diff --git a/src/index.js b/src/index.js index bd70ef0..bee70d2 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,7 @@ export { ZalgoPromise as Promise } from 'zalgo-promise/src'; export * from './types'; export { ProxyWindow } from './serialize'; export { setup, destroy, serializeMessage, deserializeMessage, 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/exists.js b/src/public/exists.js new file mode 100644 index 0000000..a4df272 --- /dev/null +++ b/src/public/exists.js @@ -0,0 +1,32 @@ +/* @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 })); +} diff --git a/src/public/index.js b/src/public/index.js index 27569ea..709cb19 100644 --- a/src/public/index.js +++ b/src/public/index.js @@ -2,3 +2,4 @@ export * from './on'; export * from './send'; +export * from './exists';