-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js.map
1 lines (1 loc) · 11.2 KB
/
index.js.map
1
{"version":3,"file":"index.js","sources":["source/MessagePortTarget.js","source/utils.js","source/MessagePortEvent.js","source/MessagePortDispatcher.js"],"sourcesContent":["export class MessagePortTarget {\n constructor(sender, receiver) {\n this.sender = sender || [];\n this.receiver = receiver || [];\n\n if (!(this.sender instanceof Array)) {\n this.sender = [this.sender];\n }\n\n if (!(this.receiver instanceof Array)) {\n this.receiver = [this.receiver];\n }\n }\n\n /*\n @param data\n @param origin\n */\n postMessage(...args) {\n this.sender.forEach((item) => item.postMessage(...args));\n }\n\n addEventListener(type, handler) {\n this.receiver.forEach((item) => item.addEventListener(type, handler));\n }\n\n removeEventListener(type, handler) {\n this.receiver.forEach((item) => item.removeEventListener(type, handler));\n }\n}\n\nexport default MessagePortTarget;\n","/**\n * Created by Oleg Galaburda on 09.02.16.\n */\nimport { isObject } from '@actualwave/event-dispatcher';\n\nexport const createId = () => `MP/${Math.ceil(Math.random() * 10000)}/${Date.now()}`;\n\n/**\n * If toJSON method implemented on object, it will be called instead of converting to JSON string.\n * This was made to utilize structured cloning algorithm for raw objects.\n * https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm\n * In this case developer is responsible for converting linked objects.\n * @param object\n * @returns {Object}\n */\nexport const toRawData = (object) => {\n if (typeof object.toJSON === 'function') {\n return object.toJSON();\n }\n\n return JSON.stringify(object);\n};\n\n/**\n *\n * @param data {Object|String}\n * @returns {Object}\n */\nexport const parseRawData = (data) => {\n let object; // keep it undefined in case of error\n\n if (isObject(data)) {\n return data;\n }\n\n try {\n return JSON.parse(data);\n } catch (error) {\n // this isn't an event we are waiting for.\n }\n\n return object;\n};\n","/**\n * Created by Oleg Galaburda on 09.02.16.\n */\nimport hasOwn from '@actualwave/has-own';\nimport { isObject } from '@actualwave/event-dispatcher';\nimport { toRawData, parseRawData } from './utils';\n\nexport class MessagePortEvent {\n constructor(event, dispatcherId) {\n this.event = event;\n this.dispatcherId = dispatcherId;\n }\n\n toJSON() {\n return {\n event: toRawData(this.event),\n dispatcherId: this.dispatcherId,\n };\n }\n}\n\nexport const isMessagePortEvent = (object) =>\n isObject(object) && hasOwn(object, 'dispatcherId') && hasOwn(object, 'event');\n\nexport const parseMessagePortEvent = (object) => {\n const result = parseRawData(object);\n\n if (result && isMessagePortEvent(result)) {\n const { event, dispatcherId } = result;\n return new MessagePortEvent(parseRawData(event), dispatcherId);\n }\n\n return null;\n};\n","/**\n * Created by Oleg Galaburda on 09.02.16.\n */\n/* eslint-disable no-restricted-globals */\n\nimport { createEventDispatcher, getEvent } from '@actualwave/event-dispatcher';\nimport { MessagePortEvent, parseMessagePortEvent } from './MessagePortEvent';\nimport { createId, toRawData } from './utils';\n\nexport class MessagePortDispatcher {\n dispatcherId = createId();\n\n targetOrigin = '*';\n\n constructor(\n target = null,\n customPostMessageHandler = null,\n receiverEventPreprocessor = null,\n senderEventPreprocessor = null,\n ) {\n this.target = target || self;\n this.customPostMessageHandler = customPostMessageHandler;\n this.senderEventPreprocessor = senderEventPreprocessor;\n this.sender = createEventDispatcher();\n this.receiver = createEventDispatcher(receiverEventPreprocessor);\n\n this.target.addEventListener('message', (event) => this._postMessageListener(event));\n }\n\n addEventListener(eventType, listener, priority) {\n this.receiver.addEventListener(eventType, listener, priority);\n }\n\n hasEventListener(eventType) {\n return this.receiver.hasEventListener(eventType);\n }\n\n removeEventListener(eventType, listener) {\n this.receiver.removeEventListener(eventType, listener);\n }\n\n removeAllEventListeners(eventType) {\n this.receiver.removeAllEventListeners(eventType);\n }\n\n dispatchEvent(eventType, data, transferList) {\n let event = getEvent(eventType, data);\n\n if (this.senderEventPreprocessor) {\n event = this.senderEventPreprocessor.call(this, event);\n }\n\n const eventJson = toRawData(new MessagePortEvent(event, this.dispatcherId));\n\n return this._postMessageHandler(eventJson, transferList);\n }\n\n /**\n * @private\n */\n _postMessageHandler(data, transferList) {\n const handler = this.customPostMessageHandler;\n\n if (handler) {\n return handler.call(this, data, this.targetOrigin, transferList);\n }\n\n return this.target.postMessage(data, this.targetOrigin, transferList);\n }\n\n /**\n * @private\n */\n _postMessageListener(event) {\n // INFO .nativeEvent react-native thing, it contains event object coming from WebView\n event = event.nativeEvent || event;\n const message = parseMessagePortEvent(event.data);\n\n if (message) {\n if (message.dispatcherId === this.dispatcherId) {\n this.sender.dispatchEvent(message.event);\n } else {\n this.receiver.dispatchEvent(message.event);\n }\n }\n }\n}\n\nexport const createMessagePortDispatcher = (\n target,\n customPostMessageHandler,\n receiverEventPreprocessor,\n senderEventPreprocessor,\n) =>\n new MessagePortDispatcher(\n target,\n customPostMessageHandler,\n receiverEventPreprocessor,\n senderEventPreprocessor,\n );\n\nconst factory = (getTarget, dispatcher = null) => () => {\n if (!dispatcher) {\n dispatcher = createMessagePortDispatcher(getTarget());\n }\n\n return dispatcher;\n};\n\nexport const getForSelf = factory(() => self);\n\nexport const getForParent = factory(() => parent);\n\nexport const getForTop = factory(() => top);\n"],"names":["MessagePortTarget","constructor","sender","receiver","Array","postMessage","args","forEach","item","addEventListener","type","handler","removeEventListener","createId","Math","ceil","random","Date","now","toRawData","object","toJSON","JSON","stringify","parseRawData","data","isObject","parse","error","MessagePortEvent","event","dispatcherId","isMessagePortEvent","hasOwn","parseMessagePortEvent","result","MessagePortDispatcher","target","customPostMessageHandler","receiverEventPreprocessor","senderEventPreprocessor","targetOrigin","self","createEventDispatcher","_postMessageListener","eventType","listener","priority","hasEventListener","removeAllEventListeners","dispatchEvent","transferList","getEvent","call","eventJson","_postMessageHandler","nativeEvent","message","createMessagePortDispatcher","factory","getTarget","dispatcher","getForSelf","getForParent","parent","getForTop","top"],"mappings":";;;;;;;;;;;AAAO,MAAMA,iBAAN,CAAwB;AAC7BC,EAAAA,WAAW,CAACC,MAAD,EAASC,QAAT,EAAmB;AAC5B,SAAKD,MAAL,GAAcA,MAAM,IAAI,EAAxB;AACA,SAAKC,QAAL,GAAgBA,QAAQ,IA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