@cicada/message
Advanced tools
Comparing version 0.1.4 to 0.2.0
128
lib/index.js
@@ -6,3 +6,12 @@ 'use strict'; | ||
}); | ||
exports.isIframe = exports.createMessage = undefined; | ||
var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); | ||
var _defineProperty3 = _interopRequireDefault(_defineProperty2); | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
@@ -12,5 +21,2 @@ | ||
exports.isIframe = isIframe; | ||
exports.default = createMessage; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -22,2 +28,14 @@ | ||
/* eslint-disable no-bitwise */ | ||
function generateUUID() { | ||
var d = Date.now(); | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = (d + Math.random() * 16) % 16 | 0; | ||
d = Math.floor(d / 16); | ||
return (c === 'x' ? r : r & 0x3 | 0x8).toString(16); | ||
}); | ||
} | ||
var CICADA_UUID = '__CICADA_UUID__'; | ||
var CICADA_PROMISE_EVENT = '__CICADA_PROMISE_EVENT__'; | ||
function createMessage() { | ||
@@ -27,3 +45,52 @@ var messagePrefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '__CICADA_MESSAGE__'; | ||
var listeners = []; | ||
var promiseCache = {}; | ||
var prefixReg = new RegExp('^' + messagePrefix); | ||
function listen(event, callback) { | ||
var listener = { event: event, callback: callback }; | ||
listeners.push(listener); | ||
return function removeListener() { | ||
var index = listeners.indexOf(listener); | ||
if (index !== -1) { | ||
listeners.splice(index, 1); | ||
} | ||
}; | ||
} | ||
function dispatch(event, data, toParent) { | ||
var message = void 0; | ||
try { | ||
message = (0, _stringify2.default)({ event: event, data: data }); | ||
} catch (e) { | ||
throw new Error('Post message "' + event + '" stringify error.'); | ||
} | ||
if (toParent !== undefined ? toParent : isIframe()) { | ||
window.parent.postMessage(messagePrefix + message, '*'); | ||
} else { | ||
var frames = window.frames; | ||
for (var i = 0; i < frames.length; i++) { | ||
frames[i].postMessage(messagePrefix + message, '*'); | ||
} | ||
} | ||
} | ||
function invoke(event, data) { | ||
var timeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 30000; | ||
var toParent = arguments[3]; | ||
return new _promise2.default(function (resolve, reject) { | ||
var _dispatch; | ||
var uuid = generateUUID(); | ||
dispatch(event, (_dispatch = { payload: data }, (0, _defineProperty3.default)(_dispatch, CICADA_UUID, uuid), (0, _defineProperty3.default)(_dispatch, 'toParent', toParent), _dispatch), toParent); | ||
promiseCache[uuid] = { resolve: resolve, reject: reject }; | ||
setTimeout(function () { | ||
if (promiseCache[uuid]) { | ||
delete promiseCache[uuid]; | ||
reject(new Error('[cicada-message] Remote invoke timeout in ' + timeout + ' ms.')); | ||
} | ||
}, timeout); | ||
}); | ||
} | ||
// Init post message | ||
@@ -42,2 +109,13 @@ window.addEventListener('message', function (e) { | ||
} | ||
var uuid = message.data && message.data[CICADA_UUID] ? message.data[CICADA_UUID] : null; | ||
if (message.event === CICADA_PROMISE_EVENT) { | ||
if (promiseCache[uuid]) { | ||
var resolve = promiseCache[uuid].resolve; | ||
delete promiseCache[uuid]; | ||
resolve(message.data.payload); | ||
} | ||
return; | ||
} | ||
listeners.forEach(function (_ref) { | ||
@@ -48,3 +126,11 @@ var event = _ref.event, | ||
if (message.event === event) { | ||
callback(message.data); | ||
if (!uuid) { | ||
callback(message.data); | ||
} else { | ||
_promise2.default.resolve(callback(message.data.payload)).then(function (result) { | ||
var toParent = message.data.toParent; | ||
dispatch(CICADA_PROMISE_EVENT, (0, _defineProperty3.default)({ payload: result }, CICADA_UUID, uuid), toParent === undefined ? toParent : !toParent); | ||
}); | ||
} | ||
} | ||
@@ -56,29 +142,9 @@ }); | ||
return { | ||
listen: function listen(event, callback) { | ||
var listener = { event: event, callback: callback }; | ||
listeners.push(listener); | ||
return function removeListener() { | ||
var index = listeners.indexOf(listener); | ||
if (index !== -1) { | ||
listeners.splice(index, 1); | ||
} | ||
}; | ||
}, | ||
dispatch: function dispatch(event, data, toParent) { | ||
var message = void 0; | ||
try { | ||
message = (0, _stringify2.default)({ event: event, data: data }); | ||
} catch (e) { | ||
throw new Error('Post message "' + event + '" stringify error.'); | ||
} | ||
if (toParent !== undefined ? toParent : isIframe()) { | ||
window.parent.postMessage(messagePrefix + message, '*'); | ||
} else { | ||
var frames = window.frames; | ||
for (var i = 0; i < frames.length; i++) { | ||
frames[i].postMessage(messagePrefix + message, '*'); | ||
} | ||
} | ||
} | ||
listen: listen, | ||
dispatch: dispatch, | ||
invoke: invoke | ||
}; | ||
} | ||
} | ||
exports.createMessage = createMessage; | ||
exports.isIframe = isIframe; |
{ | ||
"name": "@cicada/message", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "cicada message", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -14,9 +14,11 @@ ## cicada-message | ||
const message = createMesssage() | ||
const { listen, dispatch, invoke } = createMesssage() | ||
message.listen('yourMessage', (data) => { | ||
listen('yourMessage', (data) => { | ||
}) | ||
message.dispatch('yourMessage', { data: 'data' }) | ||
dispatch('yourMessage', { data: 'data' }) | ||
invoke('yourMessage', { data: 'data' }, 10000).then(result => console.log(result)) | ||
``` | ||
@@ -26,4 +28,5 @@ | ||
- listen(event, callback) | ||
- dispatch(event, data, toParent) | ||
- listen(event, callback) 监听事件 | ||
- dispatch(event, data, toParent) 远程触发事件 | ||
- invoke(vent, data, timeout, toParent) 远程服务调用并直接返回 | ||
113
src/index.js
@@ -1,8 +0,64 @@ | ||
export function isIframe() { | ||
function isIframe() { | ||
return window.parent !== window | ||
} | ||
export default function createMessage(messagePrefix = '__CICADA_MESSAGE__') { | ||
/* eslint-disable no-bitwise */ | ||
function generateUUID() { | ||
let d = Date.now() | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { | ||
const r = (d + Math.random() * 16) % 16 | 0 | ||
d = Math.floor(d / 16) | ||
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16) | ||
}) | ||
} | ||
const CICADA_UUID = '__CICADA_UUID__' | ||
const CICADA_PROMISE_EVENT = '__CICADA_PROMISE_EVENT__' | ||
function createMessage(messagePrefix = '__CICADA_MESSAGE__') { | ||
const listeners = [] | ||
const promiseCache = {} | ||
const prefixReg = new RegExp(`^${messagePrefix}`) | ||
function listen(event, callback) { | ||
const listener = { event, callback } | ||
listeners.push(listener) | ||
return function removeListener() { | ||
const index = listeners.indexOf(listener) | ||
if (index !== -1) { | ||
listeners.splice(index, 1) | ||
} | ||
} | ||
} | ||
function dispatch(event, data, toParent) { | ||
let message | ||
try { | ||
message = JSON.stringify({ event, data }) | ||
} catch (e) { | ||
throw new Error(`Post message "${event}" stringify error.`) | ||
} | ||
if (toParent !== undefined ? toParent : isIframe()) { | ||
window.parent.postMessage(messagePrefix + message, '*') | ||
} else { | ||
const frames = window.frames | ||
for (let i = 0; i < frames.length; i++) { | ||
frames[i].postMessage(messagePrefix + message, '*') | ||
} | ||
} | ||
} | ||
function invoke(event, data, timeout = 30000, toParent) { | ||
return new Promise((resolve, reject) => { | ||
const uuid = generateUUID() | ||
dispatch(event, { payload: data, [CICADA_UUID]: uuid, toParent }, toParent) | ||
promiseCache[uuid] = { resolve, reject } | ||
setTimeout(() => { | ||
if (promiseCache[uuid]) { | ||
delete promiseCache[uuid] | ||
reject(new Error(`[cicada-message] Remote invoke timeout in ${timeout} ms.`)) | ||
} | ||
}, timeout) | ||
}) | ||
} | ||
// Init post message | ||
@@ -21,5 +77,22 @@ window.addEventListener('message', (e) => { | ||
} | ||
const uuid = (message.data && message.data[CICADA_UUID]) ? message.data[CICADA_UUID] : null | ||
if (message.event === CICADA_PROMISE_EVENT) { | ||
if (promiseCache[uuid]) { | ||
const { resolve } = promiseCache[uuid] | ||
delete promiseCache[uuid] | ||
resolve(message.data.payload) | ||
} | ||
return | ||
} | ||
listeners.forEach(({ event, callback }) => { | ||
if (message.event === event) { | ||
callback(message.data) | ||
if (!uuid) { | ||
callback(message.data) | ||
} else { | ||
Promise.resolve(callback(message.data.payload)).then((result) => { | ||
const { toParent } = message.data | ||
dispatch(CICADA_PROMISE_EVENT, { payload: result, [CICADA_UUID]: uuid }, toParent === undefined ? toParent : !toParent) | ||
}) | ||
} | ||
} | ||
@@ -31,29 +104,11 @@ }) | ||
return { | ||
listen(event, callback) { | ||
const listener = { event, callback } | ||
listeners.push(listener) | ||
return function removeListener() { | ||
const index = listeners.indexOf(listener) | ||
if (index !== -1) { | ||
listeners.splice(index, 1) | ||
} | ||
} | ||
}, | ||
dispatch(event, data, toParent) { | ||
let message | ||
try { | ||
message = JSON.stringify({ event, data }) | ||
} catch (e) { | ||
throw new Error(`Post message "${event}" stringify error.`) | ||
} | ||
if (toParent !== undefined ? toParent : isIframe()) { | ||
window.parent.postMessage(messagePrefix + message, '*') | ||
} else { | ||
const frames = window.frames | ||
for (let i = 0; i < frames.length; i++) { | ||
frames[i].postMessage(messagePrefix + message, '*') | ||
} | ||
} | ||
}, | ||
listen, | ||
dispatch, | ||
invoke, | ||
} | ||
} | ||
export { | ||
createMessage, | ||
isIframe, | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12292
221
31
1