penpal
Version:
A promise-based library for communicating with iframes via postMessage.
19 lines (18 loc) • 498 B
JavaScript
export default (localName, log) => {
const callbacks = [];
let destroyed = false;
return {
destroy(error) {
if (!destroyed) {
destroyed = true;
log(`${localName}: Destroying connection`);
callbacks.forEach((callback) => {
callback(error);
});
}
},
onDestroy(callback) {
destroyed ? callback() : callbacks.push(callback);
},
};
};