@cicada/message
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -14,2 +14,7 @@ 'use strict'; | ||
var CICADA_UUID = '__CICADA_UUID__'; | ||
var CICADA_PROMISE_EVENT = '__CICADA_PROMISE_EVENT__'; | ||
var CICADA_CIRCULAR_STRINGIFY = exports.CICADA_CIRCULAR_STRINGIFY = '[CICADA_CIRCULAR_STRINGIFY]'; | ||
var CICADA_FUNC_STRINGIFY = exports.CICADA_FUNC_STRINGIFY = '[CICADA_FUNC_STRINGIFY]'; | ||
function isIframe() { | ||
@@ -28,13 +33,13 @@ return window.parent !== window; | ||
} | ||
var CICADA_UUID = '__CICADA_UUID__'; | ||
var CICADA_PROMISE_EVENT = '__CICADA_PROMISE_EVENT__'; | ||
var CICADA_CIRCULAR_STRINGIFY = exports.CICADA_CIRCULAR_STRINGIFY = '[CICADA_CIRCULAR_STRINGIFY]'; | ||
function safeStringify(data) { | ||
function safeStringify(data, stringifyHolder) { | ||
var cache = []; | ||
return JSON.stringify(data, function (key, value) { | ||
if (typeof value === 'function') { | ||
return stringifyHolder ? CICADA_FUNC_STRINGIFY : undefined; | ||
} | ||
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value !== null) { | ||
if (cache.indexOf(value) !== -1) { | ||
// Circular reference found, discard key | ||
return CICADA_CIRCULAR_STRINGIFY; | ||
return stringifyHolder ? CICADA_CIRCULAR_STRINGIFY : undefined; | ||
} | ||
@@ -50,2 +55,3 @@ // Store value in our collection | ||
var messagePrefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '__CICADA_MESSAGE__'; | ||
var stringifyHolder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
@@ -70,3 +76,3 @@ var listeners = []; | ||
try { | ||
message = safeStringify({ event: event, data: data }); | ||
message = safeStringify({ event: event, data: data }, stringifyHolder); | ||
} catch (e) { | ||
@@ -120,6 +126,12 @@ throw new Error('Post message "' + event + '" stringify error.'); | ||
if (promiseCache[uuid]) { | ||
var resolve = promiseCache[uuid].resolve; | ||
var _promiseCache$uuid = promiseCache[uuid], | ||
resolve = _promiseCache$uuid.resolve, | ||
reject = _promiseCache$uuid.reject; | ||
delete promiseCache[uuid]; | ||
resolve(message.data.payload); | ||
try { | ||
resolve(message.data.payload); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
} | ||
@@ -126,0 +138,0 @@ return; |
{ | ||
"name": "@cicada/message", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "cicada message", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -0,1 +1,6 @@ | ||
const CICADA_UUID = '__CICADA_UUID__' | ||
const CICADA_PROMISE_EVENT = '__CICADA_PROMISE_EVENT__' | ||
export const CICADA_CIRCULAR_STRINGIFY = '[CICADA_CIRCULAR_STRINGIFY]' | ||
export const CICADA_FUNC_STRINGIFY = '[CICADA_FUNC_STRINGIFY]' | ||
export function isIframe() { | ||
@@ -14,13 +19,13 @@ return window.parent !== window | ||
} | ||
const CICADA_UUID = '__CICADA_UUID__' | ||
const CICADA_PROMISE_EVENT = '__CICADA_PROMISE_EVENT__' | ||
export const CICADA_CIRCULAR_STRINGIFY = '[CICADA_CIRCULAR_STRINGIFY]' | ||
function safeStringify(data) { | ||
function safeStringify(data, stringifyHolder) { | ||
const cache = [] | ||
return JSON.stringify(data, (key, value) => { | ||
if (typeof value === 'function') { | ||
return stringifyHolder ? CICADA_FUNC_STRINGIFY : undefined | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if (cache.indexOf(value) !== -1) { | ||
// Circular reference found, discard key | ||
return CICADA_CIRCULAR_STRINGIFY | ||
return stringifyHolder ? CICADA_CIRCULAR_STRINGIFY : undefined | ||
} | ||
@@ -34,3 +39,3 @@ // Store value in our collection | ||
export default function createMessage(messagePrefix = '__CICADA_MESSAGE__') { | ||
export default function createMessage(messagePrefix = '__CICADA_MESSAGE__', stringifyHolder = false) { | ||
const listeners = [] | ||
@@ -54,3 +59,3 @@ const promiseCache = {} | ||
try { | ||
message = safeStringify({ event, data }) | ||
message = safeStringify({ event, data }, stringifyHolder) | ||
} catch (e) { | ||
@@ -99,5 +104,9 @@ throw new Error(`Post message "${event}" stringify error.`) | ||
if (promiseCache[uuid]) { | ||
const { resolve } = promiseCache[uuid] | ||
const { resolve, reject } = promiseCache[uuid] | ||
delete promiseCache[uuid] | ||
resolve(message.data.payload) | ||
try { | ||
resolve(message.data.payload) | ||
} catch (error) { | ||
reject(error) | ||
} | ||
} | ||
@@ -104,0 +113,0 @@ return |
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
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
15092
292