UNPKG

@o3r/core

Version:
54 lines 1.84 kB
import { filter, map, } from 'rxjs'; import { applicationMessageTarget, otterMessageType, } from './message.interfaces'; /** * Determine if a message should be handle by the application * @param message Message to analyze */ export const isToAppOtterMessage = (message) => { return message?.to === applicationMessageTarget; }; /** * Determine if a message is emitted by an Otter tool * @param message Message to analyze */ export const isOtterMessage = (message) => { return message?.type === otterMessageType; }; /** * Send an Otter Message * @param dataType Type of the message * @param content content of the message * @param preStringify determine if the message should JSON.stringify before being send (will use the default mechanism otherwise) */ export const sendOtterMessage = (dataType, content, preStringify = true) => { const message = { type: otterMessageType, content: { ...content, dataType } }; return window.postMessage(preStringify ? JSON.stringify(message) : message, '*'); }; /** * Filter the Otter message that should be handle by the application and returns it content * @param predicate condition to filter the message * @returns content of the message */ /** * Operator to get only Otter messages that match the predicate * @param predicate */ export function filterMessageContent(predicate) { return (source$) => { const obs = source$.pipe(map((event) => { const data = event.data; return typeof data === 'string' ? JSON.parse(data) : data; }), filter(isOtterMessage), filter(isToAppOtterMessage), map((message) => message.content)); if (predicate) { return obs.pipe(filter(predicate)); } return obs; }; } //# sourceMappingURL=message.helpers.js.map