@stories-js/core
Version:
Stories web components to build stories
70 lines (67 loc) • 2.62 kB
JavaScript
/*!
* (C) StoriesJS https://storiesjs.org - GPL-2.0 License
*/
import { m as messages } from './messages-1b750fe6.js';
const ACTION_EVENT = 'ACTION_EVENT';
const DefaultActionOptions = {
clearOnStoryChange: true,
limit: 50,
};
const findProto = (obj, callback) => {
const proto = Object.getPrototypeOf(obj);
if (!proto || callback(proto))
return proto;
return findProto(proto, callback);
};
const isReactSyntheticEvent = (e) => Boolean(typeof e === 'object' &&
e &&
findProto(e, (proto) => /^Synthetic(?:Base)?Event$/.test(proto.constructor.name)) &&
typeof e.persist === 'function');
const serializeArg = (a) => {
if (isReactSyntheticEvent(a)) {
const e = Object.create(a.constructor.prototype, Object.getOwnPropertyDescriptors(a));
e.persist();
const viewDescriptor = Object.getOwnPropertyDescriptor(e, 'view');
// dont send the entire window object over.
const view = viewDescriptor === null || viewDescriptor === void 0 ? void 0 : viewDescriptor.value;
if (typeof view === 'object' && (view === null || view === void 0 ? void 0 : view.constructor.name) === 'Window') {
Object.defineProperty(e, 'view', Object.assign(Object.assign({}, viewDescriptor), { value: Object.create(view.constructor.prototype) }));
}
return e;
}
return a;
};
function action(name, options = {}) {
const actionOptions = Object.assign(Object.assign({}, DefaultActionOptions), options);
const handler = (...args) => {
const id = (new Date()).getTime().toString(); // uuidv4();
const maxDepth = 1;
const serializedArgs = args.map(serializeArg);
const normalizedArgs = args.length > 1 ? serializedArgs : serializedArgs[0];
const actionDisplayToEmit = {
id,
count: 0,
data: { name, args: normalizedArgs },
options: Object.assign(Object.assign({}, actionOptions), { maxDepth: maxDepth, allowFunction: actionOptions.allowFunction || false }),
};
let cache = [];
const event = JSON.stringify(actionDisplayToEmit, (_, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Duplicate reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null; // Enable garbage collection
const original = JSON.parse(event);
console.log(ACTION_EVENT, original);
messages.emit(ACTION_EVENT, original);
};
return handler;
}
export { ACTION_EVENT as A, DefaultActionOptions as D, action as a };
//# sourceMappingURL=action-1e3f583f.js.map