browserify/timers-browserify

Latest version breaks Webpack builds

Closed this issue · 6 comments

Hello,

Version 2.0.7 of timers-browserify breaks our Webpack build.

This is the code that ends up in our bundle:

        (function(e) {
            function a(e, t) {
                this._id = e, this._clearFn = t
            }
            var r = this,
                o = Function.prototype.apply;
            t.setTimeout = function() {
                return new a(o.call(setTimeout, r, arguments), clearTimeout)
            }, t.setInterval = function() {
                return new a(o.call(setInterval, r, arguments), clearInterval)
            }, t.clearTimeout = t.clearInterval = function(e) {
                e && e.close()
            }, a.prototype.unref = a.prototype.ref = function() {}, a.prototype.close = function() {
                this._clearFn.call(r, this._id)
            }, t.enroll = function(e, t) {
                clearTimeout(e._idleTimeoutId), e._idleTimeout = t
            }, t.unenroll = function(e) {
                clearTimeout(e._idleTimeoutId), e._idleTimeout = -1
            }, t._unrefActive = t.active = function(e) {
                clearTimeout(e._idleTimeoutId);
                var t = e._idleTimeout;
                t >= 0 && (e._idleTimeoutId = setTimeout(function() {
                    e._onTimeout && e._onTimeout()
                }, t))
            }, n(2206), t.setImmediate = "undefined" != typeof self && self.setImmediate || void 0 !== e && e.setImmediate || this && this.setImmediate, t.clearImmediate = "undefined" != typeof self && self.clearImmediate || void 0 !== e && e.clearImmediate || this && this.clearImmediate
        }).call(t, n(38))
    },

This is the line that throws the exception:

return new a(o.call(setTimeout, r, arguments), clearTimeout)

And this is the exception in Safari:
TypeError: Can only call Window.setTimeout on instances of Window

I'm sorry for not having a simple repro step, I wanted to let you know as soon as we found out.

@thiagofelix Any thoughts about how we could address this?

@yoshicarroll Could you add a line to log what gets captured as r = this for you?

This is the value if r as it evaluates on the console, in Safari, paused on a breakpoint on
return new a(o.call(setTimeout, r, arguments), clearTimeout)

Object = $3
_unrefActive: function(e)
active: function(e)
clearImmediate: function(e)
clearInterval: function(e)
clearTimeout: function(e)
enroll: function(e,t)
setImmediate: function(e)
setInterval: function()
setTimeout: function()
unenroll: function(e)
Object Prototype

Does that help?

@yoshicarroll Okay, thanks. If you replace r = this with r = self || window, does it fix the issue?

That does, indeed fix the issue.

self was window, in this case.

Thanks for testing that out! I have published that as a fix in 2.0.8. Hopefully that takes care of it. 😁