@scalecube/utils
Advanced tools
Comparing version
@@ -13,2 +13,3 @@ declare type Collection<T> = T[] | { | ||
export declare const assertArray: (val: any, msg?: string) => void; | ||
export declare const asserNotEmptytArray: (val: any, msg?: string) => void; | ||
export declare const isObject: (val: any) => boolean; | ||
@@ -15,0 +16,0 @@ export declare const assertObject: (val: any, msg?: string) => void; |
export declare const initialize: () => void; | ||
export declare const addWorker: (worker: Worker) => void; | ||
export declare const removeWorker: (worker: Worker) => void; | ||
export declare const addIframe: (iframe: HTMLIFrameElement) => void; |
@@ -12,2 +12,3 @@ import * as check from './check'; | ||
initialize: () => void; | ||
addIframe: (iframe: HTMLIFrameElement) => void; | ||
} | { | ||
@@ -17,4 +18,5 @@ addWorker?: undefined; | ||
initialize?: undefined; | ||
addIframe?: undefined; | ||
}; | ||
declare const mockMessageChannel: () => void; | ||
export { check, getFullAddress, validateAddress, constants, getAddress, workers, saveToLogs, isNodejs, validateServiceDefinition, getQualifier, mockMessageChannel, }; |
@@ -27,2 +27,5 @@ 'use strict'; | ||
}; | ||
const asserNotEmptytArray = (val, msg = 'Expected to be an array') => { | ||
assert(isNonEmptyArray(val), msg); | ||
}; | ||
const isObject = (val) => Object.prototype.toString.call(val) === '[object Object]'; | ||
@@ -72,2 +75,3 @@ const assertObject = (val, msg = 'Expected to be an object') => { | ||
assertArray: assertArray, | ||
asserNotEmptytArray: asserNotEmptytArray, | ||
isObject: isObject, | ||
@@ -172,2 +176,16 @@ assertObject: assertObject, | ||
const workersMap = {}; | ||
const registeredIframes = {}; | ||
const iframes = []; | ||
/** | ||
* check from which iframe the event arrived, | ||
* @param ev | ||
*/ | ||
const registerIframe = (ev) => { | ||
iframes.some((iframe) => { | ||
if (ev.source === iframe.contentWindow) { | ||
registeredIframes[ev.data.detail.whoAmI || ev.data.detail.origin] = iframe; | ||
} | ||
return ev.source === iframe.contentWindow; | ||
}); | ||
}; | ||
const initialize = () => { | ||
@@ -182,5 +200,7 @@ if (!isNodejs()) { | ||
if (ev && ev.data && !ev.data.workerId) { | ||
if (ev.data.detail) { | ||
ev.data.type === 'ConnectIframe' && registerIframe(ev); | ||
const detail = ev.data.detail; | ||
if (detail) { | ||
ev.data.workerId = 1; | ||
const propogateTo = workersMap[ev.data.detail.to] || workersMap[ev.data.detail.address]; // discoveryEvents || rsocketEvents | ||
const propogateTo = workersMap[detail.to] || workersMap[detail.address]; // discoveryEvents || rsocketEvents | ||
if (propogateTo) { | ||
@@ -190,2 +210,6 @@ // @ts-ignore | ||
} | ||
const iframe = registeredIframes[detail.to] || registeredIframes[detail.address]; | ||
if (iframe) { | ||
iframe.contentWindow.postMessage(ev.data, '*', ev.ports); | ||
} | ||
} | ||
@@ -228,2 +252,5 @@ } | ||
}; | ||
const addIframe = (iframe) => { | ||
iframes.push(iframe); | ||
}; | ||
@@ -399,3 +426,3 @@ const colorsMap = {}; | ||
const workers = !isNodejs() ? { addWorker, removeWorker, initialize } : {}; | ||
const workers = !isNodejs() ? { addWorker, removeWorker, initialize, addIframe } : {}; | ||
const mockMessageChannel = () => { | ||
@@ -402,0 +429,0 @@ applyPostMessagePolyfill(); |
@@ -13,2 +13,3 @@ declare type Collection<T> = T[] | { | ||
export declare const assertArray: (val: any, msg?: string) => void; | ||
export declare const asserNotEmptytArray: (val: any, msg?: string) => void; | ||
export declare const isObject: (val: any) => boolean; | ||
@@ -15,0 +16,0 @@ export declare const assertObject: (val: any, msg?: string) => void; |
@@ -23,2 +23,5 @@ export const assert = (predicate, msg) => { | ||
}; | ||
export const asserNotEmptytArray = (val, msg = 'Expected to be an array') => { | ||
assert(isNonEmptyArray(val), msg); | ||
}; | ||
export const isObject = (val) => Object.prototype.toString.call(val) === '[object Object]'; | ||
@@ -25,0 +28,0 @@ export const assertObject = (val, msg = 'Expected to be an object') => { |
export declare const initialize: () => void; | ||
export declare const addWorker: (worker: Worker) => void; | ||
export declare const removeWorker: (worker: Worker) => void; | ||
export declare const addIframe: (iframe: HTMLIFrameElement) => void; |
import { isNodejs } from './checkEnvironemnt'; | ||
const workersMap = {}; | ||
const registeredIframes = {}; | ||
const iframes = []; | ||
/** | ||
* check from which iframe the event arrived, | ||
* @param ev | ||
*/ | ||
const registerIframe = (ev) => { | ||
iframes.some((iframe) => { | ||
if (ev.source === iframe.contentWindow) { | ||
registeredIframes[ev.data.detail.whoAmI || ev.data.detail.origin] = iframe; | ||
} | ||
return ev.source === iframe.contentWindow; | ||
}); | ||
}; | ||
export const initialize = () => { | ||
@@ -12,5 +26,7 @@ if (!isNodejs()) { | ||
if (ev && ev.data && !ev.data.workerId) { | ||
if (ev.data.detail) { | ||
ev.data.type === 'ConnectIframe' && registerIframe(ev); | ||
const detail = ev.data.detail; | ||
if (detail) { | ||
ev.data.workerId = 1; | ||
const propogateTo = workersMap[ev.data.detail.to] || workersMap[ev.data.detail.address]; // discoveryEvents || rsocketEvents | ||
const propogateTo = workersMap[detail.to] || workersMap[detail.address]; // discoveryEvents || rsocketEvents | ||
if (propogateTo) { | ||
@@ -20,2 +36,6 @@ // @ts-ignore | ||
} | ||
const iframe = registeredIframes[detail.to] || registeredIframes[detail.address]; | ||
if (iframe) { | ||
iframe.contentWindow.postMessage(ev.data, '*', ev.ports); | ||
} | ||
} | ||
@@ -58,1 +78,4 @@ } | ||
}; | ||
export const addIframe = (iframe) => { | ||
iframes.push(iframe); | ||
}; |
@@ -12,2 +12,3 @@ import * as check from './check'; | ||
initialize: () => void; | ||
addIframe: (iframe: HTMLIFrameElement) => void; | ||
} | { | ||
@@ -17,4 +18,5 @@ addWorker?: undefined; | ||
initialize?: undefined; | ||
addIframe?: undefined; | ||
}; | ||
declare const mockMessageChannel: () => void; | ||
export { check, getFullAddress, validateAddress, constants, getAddress, workers, saveToLogs, isNodejs, validateServiceDefinition, getQualifier, mockMessageChannel, }; |
import * as check from './check'; | ||
import { getFullAddress, validateAddress, getAddress } from './address'; | ||
import * as constants from './constants'; | ||
import { addWorker, removeWorker, initialize } from './connectWorkers'; | ||
import { addWorker, removeWorker, initialize, addIframe } from './connectWorkers'; | ||
import { saveToLogs } from './logs'; | ||
@@ -11,3 +11,3 @@ import { isNodejs } from './checkEnvironemnt'; | ||
import { applyMessageChannelPolyfill } from './mocks/MessageChannelPolyfill'; | ||
const workers = !isNodejs() ? { addWorker, removeWorker, initialize } : {}; | ||
const workers = !isNodejs() ? { addWorker, removeWorker, initialize, addIframe } : {}; | ||
const mockMessageChannel = () => { | ||
@@ -14,0 +14,0 @@ applyPostMessagePolyfill(); |
{ | ||
"name": "@scalecube/utils", | ||
"version": "0.2.7", | ||
"version": "0.2.8-develop.1593098019", | ||
"private": false, | ||
@@ -24,3 +24,3 @@ "main": "cjs/index.js", | ||
"devDependencies": { | ||
"@scalecube/api": "^0.2.7", | ||
"@scalecube/api": "^0.2.8-develop.1593098019", | ||
"jest": "^24.6.0", | ||
@@ -36,3 +36,3 @@ "rollup": "^1.14.6", | ||
}, | ||
"gitHead": "b88f3c294fd730466509b0e3425238b8dfd31c0e" | ||
"gitHead": "242b2696983847ad0f9b896af4b02c9e826bcc15" | ||
} |
57291
4.48%1078
6%