{"version":3,"sources":["src/common/Timeout.ts"],"names":[],"mappings":"AAwDA,UAAU,aAAa;IACnB,YAAY,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACxC,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;CAC1D;AAID,qBAAa,OAAO;IAEhB,OAAO,CAAC,MAAM,CAAC,YAAY,CAA8B;IAEzD,OAAc,YAAY,EAAE,aAAa,CAAC,cAAc,CAAC,CAAqE;IAE9H,OAAc,UAAU,EAAE,aAAa,CAAC,YAAY,CAAC,CAAwF;WAE/H,IAAI,IAAI,GAAG;IA6FzB,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAY/B,OAAc,MAAM,EAAE,MAAM,aAAa,CAA8B;IAEvE,OAAO,CAAC,MAAM,CAAC,kBAAkB;IAIjC,OAAO,CAAC,MAAM,CAAC,eAAe;CAGjC","file":"Timeout.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\ntype TWorkerMessage = ICallNotification | IClearResponse | IErrorNotification | IErrorResponse;\r\n\r\ninterface IClearResponse {\r\n    error: null;\r\n    id: number;\r\n}\r\n\r\ninterface IErrorNotification {\r\n    error: {\r\n        message: string;\r\n    };\r\n    id: null;\r\n    result: null;\r\n}\r\n\r\ninterface IErrorResponse {\r\n    error: {\r\n        message: string;\r\n    };\r\n    id: number;\r\n    result: null;\r\n}\r\n\r\ninterface ICallNotification {\r\n    id: null;\r\n    method: \"call\";\r\n    params: {\r\n        timerId: number;\r\n    };\r\n}\r\n\r\ninterface ISetNotification {\r\n    id: null;\r\n    method: \"set\";\r\n    params: {\r\n        delay: number;\r\n        now: number;\r\n        timerId: number;\r\n    };\r\n}\r\n\r\ninterface IClearRequest {\r\n    id: number;\r\n    method: \"clear\";\r\n    params: {\r\n        timerId: number;\r\n    };\r\n}\r\n\r\ninterface IWorkerEvent extends Event {\r\n    data: TWorkerMessage;\r\n}\r\n\r\ninterface IWorkerTimers {\r\n    clearTimeout: (timerId: number) => void;\r\n    setTimeout: (func: () => any, delay: number) => number;\r\n}\r\n\r\ndeclare type Func = () => any;\r\n\r\nexport class Timeout {\r\n\r\n    private static workerTimers: null | IWorkerTimers = null;\r\n\r\n    public static clearTimeout: IWorkerTimers[\"clearTimeout\"] = (timerId: number): void => Timeout.timers().clearTimeout(timerId);\r\n\r\n    public static setTimeout: IWorkerTimers[\"setTimeout\"] = (func: () => any, delay: number): number => Timeout.timers().setTimeout(func, delay);\r\n\r\n    public static load(): any {\r\n        // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.\r\n        const scheduledTimeoutFunctions: Map<number, number | Func> = new Map([[0, (): void => { }]]); // eslint-disable-line @typescript-eslint/no-empty-function\r\n        const unhandledRequests: Map<number, number> = new Map();\r\n\r\n        // eslint-disable-next-line\r\n        const workerScript = `!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\"undefined\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\"Module\"}),Object.defineProperty(e,\"__esModule\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\"object\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\"default\",{enumerable:!0,value:e}),2&t&&\"string\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\"a\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\"\",n(n.s=14)}([function(e,t,n){\"use strict\";n.d(t,\"a\",(function(){return i})),n.d(t,\"b\",(function(){return u})),n.d(t,\"c\",(function(){return a})),n.d(t,\"d\",(function(){return d}));const r=new Map,o=new Map,i=e=>{const t=r.get(e);if(void 0===t)throw new Error('There is no interval scheduled with the given id \"'.concat(e,'\".'));clearTimeout(t),r.delete(e)},u=e=>{const t=o.get(e);if(void 0===t)throw new Error('There is no timeout scheduled with the given id \"'.concat(e,'\".'));clearTimeout(t),o.delete(e)},f=(e,t)=>{let n,r;if(\"performance\"in self){const o=performance.now();n=o,r=e-Math.max(0,o-t)}else n=Date.now(),r=e;return{expected:n+r,remainingDelay:r}},c=(e,t,n,r)=>{const o=\"performance\"in self?performance.now():Date.now();o>n?postMessage({id:null,method:\"call\",params:{timerId:t}}):e.set(t,setTimeout(c,n-o,e,t,n))},a=(e,t,n)=>{const{expected:o,remainingDelay:i}=f(e,n);r.set(t,setTimeout(c,i,r,t,o))},d=(e,t,n)=>{const{expected:r,remainingDelay:i}=f(e,n);o.set(t,setTimeout(c,i,o,t,r))}},function(e,t,n){\"use strict\";n.r(t);var r=n(2);for(var o in r)\"default\"!==o&&function(e){n.d(t,e,(function(){return r[e]}))}(o);var i=n(3);for(var o in i)\"default\"!==o&&function(e){n.d(t,e,(function(){return i[e]}))}(o);var u=n(4);for(var o in u)\"default\"!==o&&function(e){n.d(t,e,(function(){return u[e]}))}(o);var f=n(5);for(var o in f)\"default\"!==o&&function(e){n.d(t,e,(function(){return f[e]}))}(o);var c=n(6);for(var o in c)\"default\"!==o&&function(e){n.d(t,e,(function(){return c[e]}))}(o);var a=n(7);for(var o in a)\"default\"!==o&&function(e){n.d(t,e,(function(){return a[e]}))}(o);var d=n(8);for(var o in d)\"default\"!==o&&function(e){n.d(t,e,(function(){return d[e]}))}(o);var s=n(9);for(var o in s)\"default\"!==o&&function(e){n.d(t,e,(function(){return s[e]}))}(o)},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t,n){\"use strict\";n.r(t);var r=n(11);for(var o in r)\"default\"!==o&&function(e){n.d(t,e,(function(){return r[e]}))}(o);var i=n(12);for(var o in i)\"default\"!==o&&function(e){n.d(t,e,(function(){return i[e]}))}(o);var u=n(13);for(var o in u)\"default\"!==o&&function(e){n.d(t,e,(function(){return u[e]}))}(o)},function(e,t){},function(e,t){},function(e,t){},function(e,t,n){\"use strict\";n.r(t);var r=n(0),o=n(1);for(var i in o)\"default\"!==i&&function(e){n.d(t,e,(function(){return o[e]}))}(i);var u=n(10);for(var i in u)\"default\"!==i&&function(e){n.d(t,e,(function(){return u[e]}))}(i);addEventListener(\"message\",({data:e})=>{try{if(\"clear\"===e.method){const{id:t,params:{timerId:n}}=e;Object(r.b)(n),postMessage({error:null,id:t})}else{if(\"set\"!==e.method)throw new Error('The given method \"'.concat(e.method,'\" is not supported'));{const{params:{delay:t,now:n,timerId:o}}=e;Object(r.d)(t,o,n)}}}catch(t){postMessage({error:{message:t.message},id:e.id,result:null})}})}]);`;\r\n        const workerUrl = \"data:text/javascript;base64,\" + btoa(workerScript);\r\n        const worker = new Worker(workerUrl);\r\n\r\n        worker.addEventListener(\"message\", ({ data }: IWorkerEvent): void => {\r\n            if (Timeout.isCallNotification(data)) {\r\n                const {\r\n                    params: { timerId }\r\n                } = data;\r\n\r\n                const idOrFunc = scheduledTimeoutFunctions.get(timerId);\r\n\r\n                if (typeof idOrFunc === \"number\") {\r\n                    const unhandledTimerId = unhandledRequests.get(idOrFunc);\r\n\r\n                    if (\r\n                        unhandledTimerId === undefined ||\r\n                        unhandledTimerId !== timerId\r\n                    ) {\r\n                        throw new Error(\"The timer is in an undefined state.\");\r\n                    }\r\n                } else if (typeof idOrFunc !== \"undefined\") {\r\n                    idOrFunc();\r\n\r\n                    // A timeout can be safely deleted because it is only called once.\r\n                    scheduledTimeoutFunctions.delete(timerId);\r\n                } else {\r\n                    throw new Error(\"The timer is in an undefined state.\");\r\n                }\r\n            } else if (Timeout.isClearResponse(data)) {\r\n                const { id } = data;\r\n\r\n                const unhandledTimerId = unhandledRequests.get(id);\r\n\r\n                if (unhandledTimerId === undefined) {\r\n                    throw new Error(\"The timer is in an undefined state.\");\r\n                }\r\n\r\n                unhandledRequests.delete(id);\r\n\r\n                scheduledTimeoutFunctions.delete(unhandledTimerId);\r\n            } else {\r\n                const {\r\n                    error: { message }\r\n                } = data;\r\n\r\n                throw new Error(message);\r\n            }\r\n        });\r\n\r\n        const clearTimeout = (timerId: number): void => {\r\n            const id = Math.random();\r\n\r\n            unhandledRequests.set(id, timerId);\r\n            scheduledTimeoutFunctions.set(timerId, id);\r\n\r\n            worker.postMessage({\r\n                id,\r\n                method: \"clear\",\r\n                params: { timerId }\r\n            } as IClearRequest);\r\n        };\r\n\r\n        const setTimeout = (func: () => void, delay: number): number => {\r\n            const timerId = Math.random();\r\n\r\n            scheduledTimeoutFunctions.set(timerId, func);\r\n\r\n            worker.postMessage({\r\n                id: null,\r\n                method: \"set\",\r\n                params: {\r\n                    delay,\r\n                    now: performance.now(),\r\n                    timerId\r\n                }\r\n            } as ISetNotification);\r\n\r\n            return timerId;\r\n        };\r\n\r\n        return {\r\n            clearTimeout,\r\n            setTimeout\r\n        };\r\n    }\r\n\r\n    private static loadWorkerTimers(): () => IWorkerTimers {\r\n        return (): IWorkerTimers => {\r\n            if (Timeout.workerTimers !== null) {\r\n                return Timeout.workerTimers;\r\n            }\r\n\r\n            Timeout.workerTimers = Timeout.load() as IWorkerTimers;\r\n\r\n            return Timeout.workerTimers;\r\n        };\r\n    }\r\n\r\n    public static timers: () => IWorkerTimers = Timeout.loadWorkerTimers();\r\n\r\n    private static isCallNotification(message: TWorkerMessage): message is ICallNotification {\r\n        return (message as ICallNotification).method !== undefined && (message as ICallNotification).method === \"call\";\r\n    }\r\n\r\n    private static isClearResponse(message: TWorkerMessage): message is IClearResponse {\r\n        return (message as IClearResponse).error === null && typeof message.id === \"number\";\r\n    }\r\n}\r\n"]}