Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebWorker Safari iOS - ReferenceError: Can't find variable: document #2

Open
caritasverein opened this issue Feb 28, 2020 · 7 comments
Labels
bug Something isn't working

Comments

@caritasverein
Copy link

When runnning the polyfill inside a Webworker, Safari on iOS 12 complains about document not being available. I'm not entirely sure how this would be doable instead, as neither the createEvent nor CustomEvent function seems to be available in that context...

@WebReflection
Copy link
Member

workers have no dom, right? so I am not sure you're supposed to use CustomEvent within a worker, postMessage is usually the channel. What am I missing?

@caritasverein
Copy link
Author

I don't see how postMessage would remove the need for CustomEvent if the app-architecture inside the worker makes use of it.
There has been a discussion about this in a w3 mailing list, too. The TL:DR is that the new CustomEvent constructor would make createEvent obsolete in a worker context, which it does... but apparently Safari never enabled the CustomEvent constructor in a worker context

@WebReflection
Copy link
Member

any reason not to use Event instead? https://github.com/ungap/event

that does feature detection and it should patch Safari too.

@caritasverein
Copy link
Author

The reason to choose CustomEvent instead was the standardized details property, but I guess it would be possible to extend Event to create our own derived class... We'll consider doing so. You may close this issue if you have no intend to pursue this further! Thanks a lot for all your good work and help!

@WebReflection
Copy link
Member

@caritasverein the thing is, even IE11 fails at new CustomEvent, so maybe fixing that in here would be better than pointing at Event, as the detail payload is handy indeed.

will try to fix this soon(ish)

@WebReflection WebReflection added the bug Something isn't working label Mar 3, 2020
@WebReflection
Copy link
Member

@caritasverein although ...

Safari never enabled the CustomEvent constructor in a worker context

how am I supposed to patch that in there?

@WebReflection
Copy link
Member

WebReflection commented Mar 3, 2020

@caritasverein let's do this the other way around .... would this work?

/*! (c) Andrea Giammarchi - ISC */
var self = this || /* istanbul ignore next */ {};
try { new CustomEvent('!'); }
catch (shenanigans) {
  self.CustomEvent = typeof document === typeof void 0 ?
    // the Safari case
    (function (CustomEvent) {
      FakeCustomEvent.prototype = CustomEvent.prototype;
      return FakeCustomEvent;
      function FakeCustomEvent() {
        return Reflect.construct(CustomEvent, arguments, this.constructor);
      }
    }(CustomEvent)) :
    // the IE11 case
    (function () {
      FakeCustomEvent.prototype = CustomEvent.prototype;
      return FakeCustomEvent;
      function FakeCustomEvent(type, init) {
        if (!init) init = {};
        var e = document.createEvent('CustomEvent');
        e.initCustomEvent(type, !!init.bubbles, !!init.cancelable, init.detail);
        return e;
      }
    }());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants