edbrowse-dev - development list for edbrowse
 help / color / mirror / Atom feed
From: "Kevin Carhart" <kevin_carhart@fastmail.fm>
To: edbrowse-dev@edbrowse.org
Subject: proposed polyfill for MessageChannel
Date: Sat, 18 Sep 2021 15:23:33 -0700	[thread overview]
Message-ID: <b0ffd2bd-acc3-45ce-80b7-4bf31b7f838d@www.fastmail.com> (raw)

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();
}



                 reply	other threads:[~2021-09-18 22:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b0ffd2bd-acc3-45ce-80b7-4bf31b7f838d@www.fastmail.com \
    --to=kevin_carhart@fastmail.fm \
    --cc=edbrowse-dev@edbrowse.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).