edbrowse-dev - development list for edbrowse
 help / color / mirror / Atom feed
* proposed polyfill for MessageChannel
@ 2021-09-18 22:23 Kevin Carhart
  0 siblings, 0 replies; only message in thread
From: Kevin Carhart @ 2021-09-18 22:23 UTC (permalink / raw)
  To: edbrowse-dev

Could we add the following as a polyfill for MessageChannel? 
https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel

I wish everyone good health
Kevin

----------

// polyfill MessagePort and MessageChannel
var MessagePortPolyfill = /** @class */ (function () {
    function MessagePortPolyfill() {
        this.onmessage = null;
        this.onmessageerror = null;
        this.otherPort = null;
        this.onmessageListeners = [];
    }
    MessagePortPolyfill.prototype.dispatchEvent = function (event) {
        if (this.onmessage) {
            this.onmessage(event);
        }
        this.onmessageListeners.forEach(function (listener) { return listener(event); });
        return true;
    };
    MessagePortPolyfill.prototype.postMessage = function (message) {
        if (!this.otherPort) {
            return;
        }
        this.otherPort.dispatchEvent({ data: message });
    };
    MessagePortPolyfill.prototype.addEventListener = function (type, listener) {
        if (type !== 'message') {
            return;
        }
        if (typeof listener !== 'function' ||
            this.onmessageListeners.indexOf(listener) !== -1) {
            return;
        }
        this.onmessageListeners.push(listener);
    };
    MessagePortPolyfill.prototype.removeEventListener = function (type, listener) {
        if (type !== 'message') {
            return;
        }
        var index = this.onmessageListeners.indexOf(listener);
        if (index === -1) {
            return;
        }
        this.onmessageListeners.splice(index, 1);
    };
    MessagePortPolyfill.prototype.start = function () {
        // do nothing at this moment
    };
    MessagePortPolyfill.prototype.close = function () {
        // do nothing at this moment
    };
    return MessagePortPolyfill;
}());
var MessageChannelPolyfill = /** @class */ (function () {
    function MessageChannelPolyfill() {
        this.port1 = new MessagePortPolyfill();
        this.port2 = new MessagePortPolyfill();
        this.port1.otherPort = this.port2;
        this.port2.otherPort = this.port1;
    }
    return MessageChannelPolyfill;
}());
/**
 * https://github.com/zloirock/core-js/blob/master/packages/core-js/internals/global.js
 */
var globalObj = typeof window !== 'undefined' && window.Math === Math ? window :
    typeof self !== 'undefined' && self.Math === Math ? self :
        Function('return this')();
function applyPolyfill() {
    globalObj.MessagePort = MessagePortPolyfill;
    globalObj.MessageChannel = MessageChannelPolyfill;
}
if (!globalObj.MessagePort || !globalObj.MessageChannel) {
    applyPolyfill();
}



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-09-18 22:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-18 22:23 proposed polyfill for MessageChannel Kevin Carhart

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).