Created
December 7, 2016 20:05
-
-
Save pstjvn/5946037bfcd48c119f2416a752cce29a to your computer and use it in GitHub Desktop.
Web worker, raf and nexTick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function aa(a, b) { | |
function c() { | |
} | |
c.prototype = b.prototype; | |
a.prototype = new c; | |
a.prototype.constructor = a; | |
for (var d in b) { | |
if (Object.defineProperties) { | |
var e = Object.getOwnPropertyDescriptor(b, d); | |
e && Object.defineProperty(a, d, e); | |
} else { | |
a[d] = b[d]; | |
} | |
} | |
} | |
for (var ba = "function" == typeof Object.defineProperties ? Object.defineProperty : function(a, b, c) { | |
if (c.get || c.set) { | |
throw new TypeError("ES3 does not support getters and setters."); | |
} | |
a != Array.prototype && a != Object.prototype && (a[b] = c.value); | |
}, h = "undefined" != typeof window && window === this ? this : "undefined" != typeof global && null != global ? global : this, k = ["String", "prototype", "repeat"], l = 0;l < k.length - 1;l++) { | |
var m = k[l]; | |
m in h || (h[m] = {}); | |
h = h[m]; | |
} | |
var ca = k[k.length - 1], n = h[ca], p = n ? n : function(a) { | |
var b; | |
if (null == this) { | |
throw new TypeError("The 'this' value for String.prototype.repeat must not be null or undefined"); | |
} | |
b = this + ""; | |
if (0 > a || 1342177279 < a) { | |
throw new RangeError("Invalid count value"); | |
} | |
a |= 0; | |
for (var c = "";a;) { | |
if (a & 1 && (c += b), a >>>= 1) { | |
b += b; | |
} | |
} | |
return c; | |
}; | |
p != n && null != p && ba(h, ca, {configurable:!0, writable:!0, value:p}); | |
var q = this; | |
function r(a) { | |
var b = typeof a; | |
if ("object" == b) { | |
if (a) { | |
if (a instanceof Array) { | |
return "array"; | |
} | |
if (a instanceof Object) { | |
return b; | |
} | |
var c = Object.prototype.toString.call(a); | |
if ("[object Window]" == c) { | |
return "object"; | |
} | |
if ("[object Array]" == c || "number" == typeof a.length && "undefined" != typeof a.splice && "undefined" != typeof a.propertyIsEnumerable && !a.propertyIsEnumerable("splice")) { | |
return "array"; | |
} | |
if ("[object Function]" == c || "undefined" != typeof a.call && "undefined" != typeof a.propertyIsEnumerable && !a.propertyIsEnumerable("call")) { | |
return "function"; | |
} | |
} else { | |
return "null"; | |
} | |
} else { | |
if ("function" == b && "undefined" == typeof a.call) { | |
return "object"; | |
} | |
} | |
return b; | |
} | |
var da = "closure_uid_" + (1E9 * Math.random() >>> 0), ea = 0; | |
function fa(a, b, c) { | |
return a.call.apply(a.bind, arguments); | |
} | |
function ga(a, b, c) { | |
if (!a) { | |
throw Error(); | |
} | |
if (2 < arguments.length) { | |
var d = Array.prototype.slice.call(arguments, 2); | |
return function() { | |
var c = Array.prototype.slice.call(arguments); | |
Array.prototype.unshift.apply(c, d); | |
return a.apply(b, c); | |
}; | |
} | |
return function() { | |
return a.apply(b, arguments); | |
}; | |
} | |
function t(a, b, c) { | |
t = Function.prototype.bind && -1 != Function.prototype.bind.toString().indexOf("native code") ? fa : ga; | |
return t.apply(null, arguments); | |
} | |
var ha = Date.now || function() { | |
return +new Date; | |
}; | |
function v(a, b) { | |
function c() { | |
} | |
c.prototype = b.prototype; | |
a.C = b.prototype; | |
a.prototype = new c; | |
a.prototype.constructor = a; | |
a.F = function(a, c, f) { | |
for (var e = Array(arguments.length - 2), d = 2;d < arguments.length;d++) { | |
e[d - 2] = arguments[d]; | |
} | |
return b.prototype[c].apply(a, e); | |
}; | |
} | |
;function w() { | |
0 != ia && (this[da] || (this[da] = ++ea)); | |
this.u = this.u; | |
this.D = this.D; | |
} | |
var ia = 0; | |
w.prototype.u = !1; | |
var ja = String.prototype.trim ? function(a) { | |
return a.trim(); | |
} : function(a) { | |
return a.replace(/^[\s\xa0]+|[\s\xa0]+$/g, ""); | |
}; | |
function x(a, b) { | |
return a < b ? -1 : a > b ? 1 : 0; | |
} | |
;var ka = Array.prototype.indexOf ? function(a, b, c) { | |
return Array.prototype.indexOf.call(a, b, c); | |
} : function(a, b, c) { | |
c = null == c ? 0 : 0 > c ? Math.max(0, a.length + c) : c; | |
if ("string" == typeof a) { | |
return "string" == typeof b && 1 == b.length ? a.indexOf(b, c) : -1; | |
} | |
for (;c < a.length;c++) { | |
if (c in a && a[c] === b) { | |
return c; | |
} | |
} | |
return -1; | |
}; | |
var y; | |
a: { | |
var la = q.navigator; | |
if (la) { | |
var ma = la.userAgent; | |
if (ma) { | |
y = ma; | |
break a; | |
} | |
} | |
y = ""; | |
} | |
function z(a) { | |
return -1 != y.indexOf(a); | |
} | |
;var na = "constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "); | |
function oa(a, b) { | |
for (var c, d, e = 1;e < arguments.length;e++) { | |
d = arguments[e]; | |
for (c in d) { | |
a[c] = d[c]; | |
} | |
for (var f = 0;f < na.length;f++) { | |
c = na[f], Object.prototype.hasOwnProperty.call(d, c) && (a[c] = d[c]); | |
} | |
} | |
} | |
;function A(a) { | |
A[" "](a); | |
return a; | |
} | |
A[" "] = function() { | |
}; | |
var pa = z("Opera"), B = z("Trident") || z("MSIE"), qa = z("Edge"), ra = z("Gecko") && !(-1 != y.toLowerCase().indexOf("webkit") && !z("Edge")) && !(z("Trident") || z("MSIE")) && !z("Edge"), sa = -1 != y.toLowerCase().indexOf("webkit") && !z("Edge"); | |
function ta() { | |
var a = q.document; | |
return a ? a.documentMode : void 0; | |
} | |
var C; | |
a: { | |
var D = "", E = function() { | |
var a = y; | |
if (ra) { | |
return /rv\:([^\);]+)(\)|;)/.exec(a); | |
} | |
if (qa) { | |
return /Edge\/([\d\.]+)/.exec(a); | |
} | |
if (B) { | |
return /\b(?:MSIE|rv)[: ]([^\);]+)(\)|;)/.exec(a); | |
} | |
if (sa) { | |
return /WebKit\/(\S+)/.exec(a); | |
} | |
if (pa) { | |
return /(?:Version)[ \/]?(\S+)/.exec(a); | |
} | |
}(); | |
E && (D = E ? E[1] : ""); | |
if (B) { | |
var F = ta(); | |
if (null != F && F > parseFloat(D)) { | |
C = String(F); | |
break a; | |
} | |
} | |
C = D; | |
} | |
var G = {}, ua; | |
var va = q.document; | |
ua = va && B ? ta() || ("CSS1Compat" == va.compatMode ? parseInt(C, 10) : 5) : void 0; | |
var H; | |
(H = !B) || (H = 9 <= Number(ua)); | |
var wa = H, I; | |
if (I = B) { | |
var J; | |
if (Object.prototype.hasOwnProperty.call(G, "9")) { | |
J = G["9"]; | |
} else { | |
for (var K = 0, xa = ja(String(C)).split("."), ya = ja("9").split("."), za = Math.max(xa.length, ya.length), L = 0;!K && L < za;L++) { | |
var Aa = xa[L] || "", Ba = ya[L] || ""; | |
do { | |
var M = /(\d*)(\D*)(.*)/.exec(Aa) || ["", "", "", ""], N = /(\d*)(\D*)(.*)/.exec(Ba) || ["", "", "", ""]; | |
if (0 == M[0].length && 0 == N[0].length) { | |
break; | |
} | |
K = x(0 == M[1].length ? 0 : parseInt(M[1], 10), 0 == N[1].length ? 0 : parseInt(N[1], 10)) || x(0 == M[2].length, 0 == N[2].length) || x(M[2], N[2]); | |
Aa = M[3]; | |
Ba = N[3]; | |
} while (!K); | |
} | |
J = G["9"] = 0 <= K; | |
} | |
I = !J; | |
} | |
var Ca = I; | |
function O(a, b) { | |
this.type = a; | |
this.g = this.h = b; | |
this.v = !0; | |
} | |
O.prototype.i = function() { | |
this.v = !1; | |
}; | |
function P(a, b) { | |
O.call(this, a ? a.type : ""); | |
this.j = this.g = this.h = null; | |
if (a) { | |
this.type = a.type; | |
this.h = a.target || a.srcElement; | |
this.g = b; | |
var c = a.relatedTarget; | |
if (c && ra) { | |
try { | |
A(c.nodeName); | |
} catch (d) { | |
} | |
} | |
this.j = a; | |
a.defaultPrevented && this.i(); | |
} | |
} | |
v(P, O); | |
P.prototype.i = function() { | |
P.C.i.call(this); | |
var a = this.j; | |
if (a.preventDefault) { | |
a.preventDefault(); | |
} else { | |
if (a.returnValue = !1, Ca) { | |
try { | |
if (a.ctrlKey || 112 <= a.keyCode && 123 >= a.keyCode) { | |
a.keyCode = -1; | |
} | |
} catch (b) { | |
} | |
} | |
} | |
}; | |
var Q = "closure_listenable_" + (1E6 * Math.random() | 0), Da = 0; | |
function Fa(a, b, c, d, e) { | |
this.listener = a; | |
this.g = null; | |
this.src = b; | |
this.type = c; | |
this.capture = !!d; | |
this.o = e; | |
++Da; | |
this.l = this.m = !1; | |
} | |
function R(a) { | |
a.l = !0; | |
a.listener = null; | |
a.g = null; | |
a.src = null; | |
a.o = null; | |
} | |
;function S(a) { | |
this.src = a; | |
this.g = {}; | |
this.h = 0; | |
} | |
function Ga(a, b, c, d, e) { | |
var f = b.toString(); | |
b = a.g[f]; | |
b || (b = a.g[f] = [], a.h++); | |
var g = T(b, c, d, e); | |
-1 < g ? (a = b[g], a.m = !1) : (a = new Fa(c, a.src, f, !!d, e), a.m = !1, b.push(a)); | |
return a; | |
} | |
function U(a, b) { | |
var c = b.type; | |
if (c in a.g) { | |
var d = a.g[c], e = ka(d, b), f; | |
(f = 0 <= e) && Array.prototype.splice.call(d, e, 1); | |
f && (R(b), a.g[c].length || (delete a.g[c], a.h--)); | |
} | |
} | |
function T(a, b, c, d) { | |
for (var e = 0;e < a.length;++e) { | |
var f = a[e]; | |
if (!f.l && f.listener == b && f.capture == !!c && f.o == d) { | |
return e; | |
} | |
} | |
return -1; | |
} | |
;var Ha = "closure_lm_" + (1E6 * Math.random() | 0), Ia = {}, Ja = 0; | |
function Ka(a, b, c, d, e) { | |
if ("array" == r(b)) { | |
for (var f = 0;f < b.length;f++) { | |
Ka(a, b[f], c, d, e); | |
} | |
return null; | |
} | |
c = La(c); | |
if (a && a[Q]) { | |
a = Ga(a.g, String(b), c, d, e); | |
} else { | |
if (!b) { | |
throw Error("Invalid event type"); | |
} | |
var f = !!d, g = V(a); | |
g || (a[Ha] = g = new S(a)); | |
c = Ga(g, b, c, d, e); | |
if (!c.g) { | |
d = Ma(); | |
c.g = d; | |
d.src = a; | |
d.listener = c; | |
if (a.addEventListener) { | |
a.addEventListener(b.toString(), d, f); | |
} else { | |
if (a.attachEvent) { | |
a.attachEvent(Na(b.toString()), d); | |
} else { | |
throw Error("addEventListener and attachEvent are unavailable."); | |
} | |
} | |
Ja++; | |
} | |
a = c; | |
} | |
return a; | |
} | |
function Ma() { | |
var a = Oa, b = wa ? function(c) { | |
return a.call(b.src, b.listener, c); | |
} : function(c) { | |
c = a.call(b.src, b.listener, c); | |
if (!c) { | |
return c; | |
} | |
}; | |
return b; | |
} | |
function Pa(a, b, c, d, e) { | |
if ("array" == r(b)) { | |
for (var f = 0;f < b.length;f++) { | |
Pa(a, b[f], c, d, e); | |
} | |
} else { | |
(c = La(c), a && a[Q]) ? (a = a.g, b = String(b).toString(), b in a.g && (f = a.g[b], c = T(f, c, d, e), -1 < c && (R(f[c]), Array.prototype.splice.call(f, c, 1), f.length || (delete a.g[b], a.h--)))) : a && (a = V(a)) && (b = a.g[b.toString()], a = -1, b && (a = T(b, c, !!d, e)), (c = -1 < a ? b[a] : null) && W(c)); | |
} | |
} | |
function W(a) { | |
if ("number" != typeof a && a && !a.l) { | |
var b = a.src; | |
if (b && b[Q]) { | |
U(b.g, a); | |
} else { | |
var c = a.type, d = a.g; | |
b.removeEventListener ? b.removeEventListener(c, d, a.capture) : b.detachEvent && b.detachEvent(Na(c), d); | |
Ja--; | |
(c = V(b)) ? (U(c, a), c.h || (c.src = null, b[Ha] = null)) : R(a); | |
} | |
} | |
} | |
function Na(a) { | |
return a in Ia ? Ia[a] : Ia[a] = "on" + a; | |
} | |
function Qa(a, b, c, d) { | |
var e = !0; | |
if (a = V(a)) { | |
if (b = a.g[b.toString()]) { | |
for (b = b.concat(), a = 0;a < b.length;a++) { | |
var f = b[a]; | |
f && f.capture == c && !f.l && (f = Ra(f, d), e = e && !1 !== f); | |
} | |
} | |
} | |
return e; | |
} | |
function Ra(a, b) { | |
var c = a.listener, d = a.o || a.src; | |
a.m && W(a); | |
return c.call(d, b); | |
} | |
function Oa(a, b) { | |
if (a.l) { | |
return !0; | |
} | |
if (!wa) { | |
var c; | |
if (!(c = b)) { | |
a: { | |
c = ["window", "event"]; | |
for (var d = q, e;e = c.shift();) { | |
if (null != d[e]) { | |
d = d[e]; | |
} else { | |
c = null; | |
break a; | |
} | |
} | |
c = d; | |
} | |
} | |
e = c; | |
c = new P(e, this); | |
d = !0; | |
if (!(0 > e.keyCode || void 0 != e.returnValue)) { | |
a: { | |
var f = !1; | |
if (!e.keyCode) { | |
try { | |
e.keyCode = -1; | |
break a; | |
} catch (Ea) { | |
f = !0; | |
} | |
} | |
if (f || void 0 == e.returnValue) { | |
e.returnValue = !0; | |
} | |
} | |
e = []; | |
for (f = c.g;f;f = f.parentNode) { | |
e.push(f); | |
} | |
for (var f = a.type, g = e.length - 1;0 <= g;g--) { | |
c.g = e[g]; | |
var u = Qa(e[g], f, !0, c), d = d && u; | |
} | |
for (g = 0;g < e.length;g++) { | |
c.g = e[g], u = Qa(e[g], f, !1, c), d = d && u; | |
} | |
} | |
return d; | |
} | |
return Ra(a, new P(b, this)); | |
} | |
function V(a) { | |
a = a[Ha]; | |
return a instanceof S ? a : null; | |
} | |
var Sa = "__closure_events_fn_" + (1E9 * Math.random() >>> 0); | |
function La(a) { | |
if ("function" == r(a)) { | |
return a; | |
} | |
a[Sa] || (a[Sa] = function(b) { | |
return a.handleEvent(b); | |
}); | |
return a[Sa]; | |
} | |
;function X() { | |
w.call(this); | |
this.g = new S(this); | |
this.i = this; | |
} | |
v(X, w); | |
X.prototype[Q] = !0; | |
X.prototype.removeEventListener = function(a, b, c, d) { | |
Pa(this, a, b, c, d); | |
}; | |
function Ta(a, b, c, d) { | |
b = a.g.g[String(b)]; | |
if (!b) { | |
return !0; | |
} | |
b = b.concat(); | |
for (var e = !0, f = 0;f < b.length;++f) { | |
var g = b[f]; | |
if (g && !g.l && g.capture == c) { | |
var u = g.listener, Ea = g.o || g.src; | |
g.m && U(a.g, g); | |
e = !1 !== u.call(Ea, d) && e; | |
} | |
} | |
return e && 0 != d.v; | |
} | |
;function Ua(a, b) { | |
O.call(this, "message", a); | |
this.data = b; | |
} | |
aa(Ua, O); | |
function Va(a) { | |
X.call(this); | |
this.h = new Worker(a); | |
this.h.addEventListener("message", t(this.j, this), !1); | |
} | |
aa(Va, X); | |
Va.prototype.j = function(a) { | |
var b = this.i; | |
a = new Ua(this, a.data); | |
var c = a.type || a; | |
if ("string" == typeof a) { | |
a = new O(a, b); | |
} else { | |
if (a instanceof O) { | |
a.h = a.h || b; | |
} else { | |
var d = a; | |
a = new O(c, b); | |
oa(a, d); | |
} | |
} | |
d = !0; | |
b = a.g = b; | |
d = Ta(b, c, !0, a) && d; | |
Ta(b, c, !1, a); | |
}; | |
function Y(a) { | |
Wa.h.postMessage(a); | |
} | |
;var Xa = null; | |
function Ya(a) { | |
var b; | |
b = b || 0; | |
return function() { | |
return a.apply(this, Array.prototype.slice.call(arguments, 0, b)); | |
}; | |
} | |
;function Z(a, b, c) { | |
w.call(this); | |
this.g = null; | |
this.j = !1; | |
this.B = a; | |
this.A = c; | |
this.h = b || window; | |
this.i = t(this.w, this); | |
} | |
v(Z, w); | |
Z.prototype.start = function() { | |
if (null != this.g) { | |
var a = Za(this), b = $a(this); | |
a && !b && this.h.mozRequestAnimationFrame ? W(this.g) : a && b ? b.call(this.h, this.g) : this.h.clearTimeout(this.g); | |
} | |
this.g = null; | |
this.j = !1; | |
a = Za(this); | |
b = $a(this); | |
a && !b && this.h.mozRequestAnimationFrame ? (this.g = Ka(this.h, "MozBeforePaint", this.i), this.h.mozRequestAnimationFrame(null), this.j = !0) : this.g = a && b ? a.call(this.h, this.i) : this.h.setTimeout(Ya(this.i), 20); | |
}; | |
Z.prototype.w = function() { | |
this.j && this.g && W(this.g); | |
this.g = null; | |
this.B.call(this.A, ha()); | |
}; | |
function Za(a) { | |
a = a.h; | |
return a.requestAnimationFrame || a.webkitRequestAnimationFrame || a.mozRequestAnimationFrame || a.oRequestAnimationFrame || a.msRequestAnimationFrame || null; | |
} | |
function $a(a) { | |
a = a.h; | |
return a.cancelAnimationFrame || a.cancelRequestAnimationFrame || a.webkitCancelRequestAnimationFrame || a.mozCancelRequestAnimationFrame || a.oCancelRequestAnimationFrame || a.msCancelRequestAnimationFrame || null; | |
} | |
;function ab(a) { | |
"function" != r(q.setImmediate) || q.Window && q.Window.prototype && !z("Edge") && q.Window.prototype.setImmediate == q.setImmediate ? (bb || (bb = cb()), bb(a)) : q.setImmediate(a); | |
} | |
var bb; | |
function cb() { | |
var a = q.MessageChannel; | |
"undefined" === typeof a && "undefined" !== typeof window && window.postMessage && window.addEventListener && !z("Presto") && (a = function() { | |
var a = document.createElement("IFRAME"); | |
a.style.display = "none"; | |
a.src = ""; | |
document.documentElement.appendChild(a); | |
var b = a.contentWindow, a = b.document; | |
a.open(); | |
a.write(""); | |
a.close(); | |
var c = "callImmediate" + Math.random(), d = "file:" == b.location.protocol ? "*" : b.location.protocol + "//" + b.location.host, a = t(function(a) { | |
if (("*" == d || a.origin == d) && a.data == c) { | |
this.port1.onmessage(); | |
} | |
}, this); | |
b.addEventListener("message", a, !1); | |
this.port1 = {}; | |
this.port2 = {postMessage:function() { | |
b.postMessage(c, d); | |
}}; | |
}); | |
if ("undefined" !== typeof a && !z("Trident") && !z("MSIE")) { | |
var b = new a, c = {}, d = c; | |
b.port1.onmessage = function() { | |
if (void 0 !== c.next) { | |
c = c.next; | |
var a = c.s; | |
c.s = null; | |
a(); | |
} | |
}; | |
return function(a) { | |
d.next = {s:a}; | |
d = d.next; | |
b.port2.postMessage(0); | |
}; | |
} | |
return "undefined" !== typeof document && "onreadystatechange" in document.createElement("SCRIPT") ? function(a) { | |
var b = document.createElement("SCRIPT"); | |
b.onreadystatechange = function() { | |
b.onreadystatechange = null; | |
b.parentNode.removeChild(b); | |
b = null; | |
a(); | |
a = null; | |
}; | |
document.documentElement.appendChild(b); | |
} : function(a) { | |
q.setTimeout(a, 0); | |
}; | |
} | |
;console.log("Obtaining instance"); | |
!Xa && (Xa = new Va("worker.min.js")); | |
var Wa = Xa; | |
console.log("Subscribe for messages"); | |
Ka(Wa, "message", function(a) { | |
console.log("Message from worker:" + a.data); | |
}); | |
console.log("Wait for the worker to load..."); | |
setTimeout(function() { | |
console.log("Start tests:"); | |
console.log("Sending to worker: direct (000)"); | |
Y("000"); | |
console.log("Done sending: direct (000)"); | |
console.log("Schedule sending: next tick"); | |
ab(function() { | |
console.log("Sending to worker: next tick (222)"); | |
Y("222"); | |
console.log("Done sending: next tick (222)"); | |
}); | |
console.log("Schedule sending: RAF1 (111)"); | |
(new Z(function() { | |
console.log("Sending to worker: RAF1 (111)"); | |
Y("111"); | |
console.log("Done sending: RAF1 (111)"); | |
console.log("Schedule sending: RAF2"); | |
(new Z(function() { | |
console.log("Sending to worker: RAF2 (333)"); | |
Y("333"); | |
console.log("Done sending: RAF2 (333)"); | |
}, null, null)).start(); | |
}, null, null)).start(); | |
console.log("Sending to worker: direct (444)"); | |
Y("444"); | |
console.log("Done sending: direct (444)"); | |
console.log("Schedule sending: next tick (666)"); | |
ab(function() { | |
console.log("Sending to worker: next tick (666)"); | |
Y("666"); | |
console.log("Done sending: next tick (666)"); | |
}); | |
console.log("Sending to worker: direct (555)"); | |
Y("555"); | |
console.log("Done sending: direct (555)"); | |
console.log("Done tests"); | |
}, 3E3); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Testbed</title> | |
</head> | |
<body> | |
<script src="app.min.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const worker = goog.require('pstj.app.worker'); | |
const events = goog.require('goog.events'); | |
const EventType = goog.require('goog.events.EventType'); | |
const AnimationDelay = goog.require('goog.async.AnimationDelay'); | |
const nextTick = goog.require('goog.async.nextTick'); | |
let handleMessage = function(e) { | |
console.log('Message from worker:' + e.data); | |
}; | |
console.log('Obtaining instance'); | |
const workerInstance = worker.getInstance(); | |
console.log('Subscribe for messages'); | |
events.listen(workerInstance, EventType.MESSAGE, handleMessage); | |
console.log('Wait for the worker to load...'); | |
setTimeout(() => { | |
console.log('Start tests:'); | |
console.log('Sending to worker: direct (000)'); | |
workerInstance.send('000'); | |
console.log('Done sending: direct (000)'); | |
console.log('Schedule sending: next tick'); | |
nextTick(() => { | |
console.log('Sending to worker: next tick (222)'); | |
workerInstance.send('222'); | |
console.log('Done sending: next tick (222)'); | |
}); | |
console.log('Schedule sending: RAF1 (111)'); | |
(new AnimationDelay(() => { | |
console.log('Sending to worker: RAF1 (111)'); | |
workerInstance.send('111'); | |
console.log('Done sending: RAF1 (111)'); | |
console.log('Schedule sending: RAF2'); | |
(new AnimationDelay(() => { | |
console.log('Sending to worker: RAF2 (333)'); | |
workerInstance.send('333'); | |
console.log('Done sending: RAF2 (333)'); | |
}, null, null)).start(); | |
}, null, null)).start(); | |
console.log('Sending to worker: direct (444)'); | |
workerInstance.send('444'); | |
console.log('Done sending: direct (444)'); | |
console.log('Schedule sending: next tick (666)'); | |
nextTick(() => { | |
console.log('Sending to worker: next tick (666)'); | |
workerInstance.send('666'); | |
console.log('Done sending: next tick (666)'); | |
}); | |
console.log('Sending to worker: direct (555)'); | |
workerInstance.send('555'); | |
console.log('Done sending: direct (555)'); | |
console.log('Done tests'); | |
}, 3000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It simply does onmessage=function(e) { self.postMessage(e.data); } // echo.... | |
function h(a,c){function b(){}b.prototype=c.prototype;a.prototype=new b;a.prototype.constructor=a;for(var d in c)if(Object.defineProperties){var e=Object.getOwnPropertyDescriptor(c,d);e&&Object.defineProperty(a,d,e)}else a[d]=c[d]} | |
for(var aa="function"==typeof Object.defineProperties?Object.defineProperty:function(a,c,b){if(b.get||b.set)throw new TypeError("ES3 does not support getters and setters.");a!=Array.prototype&&a!=Object.prototype&&(a[c]=b.value)},k="undefined"!=typeof window&&window===this?this:"undefined"!=typeof global&&null!=global?global:this,l=["String","prototype","repeat"],m=0;m<l.length-1;m++){var n=l[m];n in k||(k[n]={});k=k[n]} | |
var p=l[l.length-1],q=k[p],r=q?q:function(a){var c;if(null==this)throw new TypeError("The 'this' value for String.prototype.repeat must not be null or undefined");c=this+"";if(0>a||1342177279<a)throw new RangeError("Invalid count value");a|=0;for(var b="";a;)if(a&1&&(b+=c),a>>>=1)c+=c;return b};r!=q&&null!=r&&aa(k,p,{configurable:!0,writable:!0,value:r});var t=this; | |
function u(a){var c=typeof a;if("object"==c)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return c;var b=Object.prototype.toString.call(a);if("[object Window]"==b)return"object";if("[object Array]"==b||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==b||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null"; | |
else if("function"==c&&"undefined"==typeof a.call)return"object";return c}function v(a){return"string"==typeof a}var w="closure_uid_"+(1E9*Math.random()>>>0),ba=0;function ca(a,c,b){return a.call.apply(a.bind,arguments)}function da(a,c,b){if(!a)throw Error();if(2<arguments.length){var d=Array.prototype.slice.call(arguments,2);return function(){var b=Array.prototype.slice.call(arguments);Array.prototype.unshift.apply(b,d);return a.apply(c,b)}}return function(){return a.apply(c,arguments)}} | |
function x(a,c,b){x=Function.prototype.bind&&-1!=Function.prototype.bind.toString().indexOf("native code")?ca:da;return x.apply(null,arguments)}function y(a){var c=z;function b(){}b.prototype=c.prototype;a.v=c.prototype;a.prototype=new b;a.prototype.constructor=a;a.u=function(a,b,f){for(var d=Array(arguments.length-2),e=2;e<arguments.length;e++)d[e-2]=arguments[e];return c.prototype[b].apply(a,d)}};function z(){0!=ea&&(this[w]||(this[w]=++ba));this.b=this.b;this.o=this.o}var ea=0;z.prototype.b=!1;var ga=Array.prototype.indexOf?function(a,c,b){return Array.prototype.indexOf.call(a,c,b)}:function(a,c,b){b=null==b?0:0>b?Math.max(0,a.length+b):b;if(v(a))return v(c)&&1==c.length?a.indexOf(c,b):-1;for(;b<a.length;b++)if(b in a&&a[b]===c)return b;return-1},ha=Array.prototype.forEach?function(a,c,b){Array.prototype.forEach.call(a,c,b)}:function(a,c,b){for(var d=a.length,e=v(a)?a.split(""):a,f=0;f<d;f++)f in e&&c.call(b,e[f],f,a)}; | |
function A(a,c){var b=ga(a,c),d;(d=0<=b)&&Array.prototype.splice.call(a,b,1);return d};var B="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" ");function ia(a,c){for(var b,d,e=1;e<arguments.length;e++){d=arguments[e];for(b in d)a[b]=d[b];for(var f=0;f<B.length;f++)b=B[f],Object.prototype.hasOwnProperty.call(d,b)&&(a[b]=d[b])}};function C(a,c){this.type=a;this.b=this.a=c;this.s=!0};var D="closure_listenable_"+(1E6*Math.random()|0),ja=0;function ka(){var a=E.src,c=F;this.listener=G;this.a=null;this.src=a;this.type=c;this.capture=!1;this.j=void 0;++ja;this.f=this.g=!1}function H(a){a.f=!0;a.listener=null;a.a=null;a.src=null;a.j=null};function I(a){this.src=a;this.a={};this.b=0}function J(a,c){var b=c.type;b in a.a&&A(a.a[b],c)&&(H(c),a.a[b].length||(delete a.a[b],a.b--))}function K(a,c,b,d){for(var e=0;e<a.length;++e){var f=a[e];if(!f.f&&f.listener==c&&f.capture==!!b&&f.j==d)return e}return-1};var L="closure_lm_"+(1E6*Math.random()|0),M={},la=0; | |
function N(a,c,b,d,e){if("array"==u(c))for(var f=0;f<c.length;f++)N(a,c[f],b,d,e);else(b=ma(b),a&&a[D])?(a=a.a,c=String(c).toString(),c in a.a&&(f=a.a[c],b=K(f,b,d,e),-1<b&&(H(f[b]),Array.prototype.splice.call(f,b,1),f.length||(delete a.a[c],a.b--)))):a&&(a=O(a))&&(c=a.a[c.toString()],a=-1,c&&(a=K(c,b,!!d,e)),(b=-1<a?c[a]:null)&&"number"!=typeof b&&b&&!b.f&&((d=b.src)&&d[D]?J(d.a,b):(e=b.type,c=b.a,d.removeEventListener?d.removeEventListener(e,c,b.capture):d.detachEvent&&d.detachEvent(e in M?M[e]: | |
M[e]="on"+e,c),la--,(e=O(d))?(J(e,b),e.b||(e.src=null,d[L]=null)):H(b))))}function O(a){a=a[L];return a instanceof I?a:null}var P="__closure_events_fn_"+(1E9*Math.random()>>>0);function ma(a){if("function"==u(a))return a;a[P]||(a[P]=function(c){return a.handleEvent(c)});return a[P]};function Q(){z.call(this);this.a=new I(this);this.h=this}y(Q);Q.prototype[D]=!0;Q.prototype.removeEventListener=function(a,c,b,d){N(this,a,c,b,d)};function R(a,c){var b=a.h,d=c,e=d.type||d;if(v(d))d=new C(d,b);else if(d instanceof C)d.a=d.a||b;else{var f=d,d=new C(e,b);ia(d,f)}f=!0;b=d.b=b;f=S(b,e,!0,d)&&f;S(b,e,!1,d)} | |
function S(a,c,b,d){c=a.a.a[String(c)];if(!c)return!0;c=c.concat();for(var e=!0,f=0;f<c.length;++f){var g=c[f];if(g&&!g.f&&g.capture==b){var fa=g.listener,oa=g.j||g.src;g.g&&J(a.a,g);e=!1!==fa.call(oa,d)&&e}}return e&&0!=d.s};function T(a,c){C.call(this,"message",a);this.data=c}h(T,C);function U(a){Q.call(this);this.c=a;this.c.addEventListener("message",x(this.i,this),!1)}h(U,Q);U.prototype.i=function(a){R(this,new T(this,a.data))};function V(a,c,b){z.call(this);this.c=a;this.l=c||0;this.m=b;this.h=x(this.i,this)}y(V);V.prototype.a=0;V.prototype.start=function(a){this.a&&t.clearTimeout(this.a);this.a=0;var c=this.h;a=void 0!==a?a:this.l;if("function"!=u(c))if(c&&"function"==typeof c.handleEvent)c=x(c.handleEvent,c);else throw Error("Invalid listener argument");this.a=2147483647<Number(a)?-1:t.setTimeout(c,a||0)};V.prototype.i=function(){this.a=0;this.c&&this.c.call(this.m)};new V(function(){for(;W.length;)ha(W,function(a){R(a,"a");A(W,a)})},200);var W=[];var X=new U(self),E=X.a;function G(a){X.c.postMessage(a.data)}var F="message".toString(),Y=E.a[F];Y||(Y=E.a[F]=[],E.b++);var Z,na=K(Y,G,void 0,void 0);-1<na?(Z=Y[na],Z.g=!1):(Z=new ka,Z.g=!1,Y.push(Z)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment