UNPKG

9.4 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/realtime-callbacks.js"],"names":["TIMER_CHECK_PERIOD_MS","_count","_realCallbackKey","_callbackList","debuglog","module","exports","setNow","f","_now","Date","now","setTimeout","func","delayMs","params","Array","prototype","slice","call","arguments","runAt","key","data","idx","binarySearch","el","splice","_scheduleRealCallback","clearTimeout","length","i","cb","global","first","Math","min","_runCallbacks","callbacksToRun","shift","push","apply","e","console","error","stack","array","max","mid","res"],"mappings":"AAAA;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;AASA;;AAEA;AACA;;AACA,IAAMA,wBAAwB,IAA9B;;AAEA;AACA,IAAIC,SAAS,CAAb;;AAEA;AACA,IAAIC,yBAAJ;;AAEA;AACA;AACA,IAAMC,gBAAgB,EAAtB;;AAEA;AACA,IAAMC,WAAW,SAAXA,QAAW,GAAW,CAAE,CAA9B;;AAEA;;;;;;;;;AASAC,OAAOC,OAAP,CAAeC,MAAf,GAAwB,UAASC,CAAT,EAAY;AAChCC,WAAOD,KAAKE,KAAKC,GAAjB;AACH,CAFD;AAGA,IAAIF,OAAOC,KAAKC,GAAhB;;AAEA;;;;;;;;;;AAUAN,OAAOC,OAAP,CAAeM,UAAf,GAA4B,UAASC,IAAT,EAAeC,OAAf,EAAwB;AAChDA,cAAUA,WAAW,CAArB;AACA,QAAIA,UAAU,CAAd,EAAiB;AACbA,kBAAU,CAAV;AACH;;AAED,QAAMC,SAASC,MAAMC,SAAN,CAAgBC,KAAhB,CAAsBC,IAAtB,CAA2BC,SAA3B,EAAsC,CAAtC,CAAf;AACA,QAAMC,QAAQZ,SAASK,OAAvB;AACA,QAAMQ,MAAMrB,QAAZ;AACAG,aAAS,2BAAT,EAAsCkB,GAAtC,EAA2C,IAA3C,EAAiDD,KAAjD,EACS,QADT,EACmBP,OADnB,EAC4B,GAD5B;AAEA,QAAMS,OAAO;AACTF,eAAOA,KADE;AAETR,cAAMA,IAFG;AAGTE,gBAAQA,MAHC;AAITO,aAA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AAMrB,MAAMF,MAAZ;AACAL,aAAS,qBAAT,EAAgCO,GAAhC;;AAEA;AACA,QAAM2B,iBAAiB,EAAvB;AACA,WAAO,IAAP,EAAa;AACT,YAAMJ,QAAQ/B,cAAc,CAAd,CAAd;AACA,YAAI,CAAC+B,KAAD,IAAUA,MAAMb,KAAN,GAAcV,GAA5B,EAAiC;AAC7B;AACH;AACDqB,aAAK7B,cAAcoC,KAAd,EAAL;AACAnC,iBAAS,wBAAT,EAAmC4B,GAAGV,GAAtC;AACAgB,uBAAeE,IAAf,CAAoBR,EAApB;AACH;;AAED;AACA;AACA;AACAJ;;AAEA,SAAK,IAAIG,IAAI,CAAb,EAAgBA,IAAIO,eAAeR,MAAnC,EAA2CC,GAA3C,EAAgD;AAC5CC,aAAKM,eAAeP,CAAf,CAAL;AACA,YAAI;AACAC,eAAGnB,IAAH,CAAQ4B,KAAR,CAAcR,MAAd,EAAsBD,GAAGjB,MAAzB;AACH,SAFD,CAEE,OAAO2B,CAAP,EAAU;AACRC,oBAAQC,KAAR,CAAc,yCAAd,EACcF,EAAEG,KAAF,IAAWH,CADzB;AAEH;AACJ;AACJ;;AAGD;;;;;AAKA,SAASjB,YAAT,CAAsBqB,KAAtB,EAA6BjC,IAA7B,EAAmC;AAC/B;AACA,QAAIuB,MAAM,CAAV;AAAA,QACIW,MAAMD,MAAMhB,MADhB;;AAGA,WAAOM,MAAMW,GAAb,EAAkB;AACd,YAAMC,MAAOZ,MAAMW,GAAP,IAAe,CAA3B;AACA,YAAME,MAAMpC,KAAKiC,MAAME,GAAN,CAAL,CAAZ;AACA,YAAIC,MAAM,CAAV,EAAa;AACT;AACAF,kBAAMC,GAAN;AACH,SAHD,MAGO;AACH;AACAZ,kBAAMY,MAAM,CAAZ;AACH;AACJ;AACD;AACA,WAAOZ,GAAP;AACH","file":"realtime-callbacks.js","sourcesContent":["/*\nCopyright 2016 OpenMarket Ltd\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n/* A re-implementation of the javascript callback functions (setTimeout,\n * clearTimeout; setInterval and clearInterval are not yet implemented) which\n * try to improve handling of large clock jumps (as seen when\n * suspending/resuming the system).\n *\n * In particular, if a timeout would have fired while the system was suspended,\n * it will instead fire as soon as possible after resume.\n */\n\n\"use strict\";\n\n// we schedule a callback at least this often, to check if we've missed out on\n// some wall-clock time due to being suspended.\nconst TIMER_CHECK_PERIOD_MS = 1000;\n\n// counter, for making up ids to return from setTimeout\nlet _count = 0;\n\n// the key for our callback with the real global.setTimeout\nlet _realCallbackKey;\n\n// a sorted list of the callbacks to be run.\n// each is an object with keys [runAt, func, params, key].\nconst _callbackList = [];\n\n// var debuglog = console.log.bind(console);\nconst debuglog = function() {};\n\n/**\n * Replace the function used by this module to get the current time.\n *\n * Intended for use by the unit tests.\n *\n * @param {function} f function which should return a millisecond counter\n *\n * @internal\n */\nmodule.exports.setNow = function(f) {\n _now = f || Date.now;\n};\nlet _now = Date.now;\n\n/**\n * reimplementation of window.setTimeout, which will call the callback if\n * the wallclock time goes past the deadline.\n *\n * @param {function} func callback to be called after a delay\n * @param {Number} delayMs number of milliseconds to delay by\n *\n * @return {Number} an identifier for this callback, which may be passed into\n * clearTimeout later.\n */\nmodule.exports.setTimeout = function(func, delayMs) {\n delayMs = delayMs || 0;\n if (delayMs < 0) {\n delayMs = 0;\n }\n\n const params = Array.prototype.slice.call(arguments, 2);\n const runAt = _now() + delayMs;\n const key = _count++;\n debuglog(\"setTimeout: scheduling cb\", key, \"at\", runAt,\n \"(delay\", delayMs, \")\");\n const data = {\n runAt: runAt,\n func: func,\n params: params,\n key: key,\n };\n\n // figure out where it goes in the list\n const idx = binarySearch(\n _callbackList, function(el) {\n return el.runAt - runAt;\n },\n );\n\n _callbackList.splice(idx, 0, data);\n _scheduleRealCallback();\n\n return key;\n};\n\n/**\n * reimplementation of window.clearTimeout, which mirrors setTimeout\n *\n * @param {Number} key result from an earlier setTimeout call\n */\nmodule.exports.clearTimeout = function(key) {\n if (_callbackList.length === 0) {\n return;\n }\n\n // remove the element from the list\n let i;\n for (i = 0; i < _callbackList.length; i++) {\n const cb = _callbackList[i];\n if (cb.key == key) {\n _callbackList.splice(i, 1);\n break;\n }\n }\n\n // iff it was the first one in the list, reschedule our callback.\n if (i === 0) {\n _scheduleRealCallback();\n }\n};\n\n// use the real global.setTimeout to schedule a callback to _runCallbacks.\nfunction _scheduleRealCallback() {\n if (_realCallbackKey) {\n global.clearTimeout(_realCallbackKey);\n }\n\n const first = _callbackList[0];\n\n if (!first) {\n debuglog(\"_scheduleRealCallback: no more callbacks, not rescheduling\");\n return;\n }\n\n const now = _now();\n const delayMs = Math.min(first.runAt - now, TIMER_CHECK_PERIOD_MS);\n\n debuglog(\"_scheduleRealCallback: now:\", now, \"delay:\", delayMs);\n _realCallbackKey = global.setTimeout(_runCallbacks, delayMs);\n}\n\nfunction _runCallbacks() {\n let cb;\n const now = _now();\n debuglog(\"_runCallbacks: now:\", now);\n\n // get the list of things to call\n const callbacksToRun = [];\n while (true) {\n const first = _callbackList[0];\n if (!first || first.runAt > now) {\n break;\n }\n cb = _callbackList.shift();\n debuglog(\"_runCallbacks: popping\", cb.key);\n callbacksToRun.push(cb);\n }\n\n // reschedule the real callback before running our functions, to\n // keep the codepaths the same whether or not our functions\n // register their own setTimeouts.\n _scheduleRealCallback();\n\n for (let i = 0; i < callbacksToRun.length; i++) {\n cb = callbacksToRun[i];\n try {\n cb.func.apply(global, cb.params);\n } catch (e) {\n console.error(\"Uncaught exception in callback function\",\n e.stack || e);\n }\n }\n}\n\n\n/* search in a sorted array.\n *\n * returns the index of the last element for which func returns\n * greater than zero, or array.length if no such element exists.\n */\nfunction binarySearch(array, func) {\n // min is inclusive, max exclusive.\n let min = 0,\n max = array.length;\n\n while (min < max) {\n const mid = (min + max) >> 1;\n const res = func(array[mid]);\n if (res > 0) {\n // the element at 'mid' is too big; set it as the new max.\n max = mid;\n } else {\n // the element at 'mid' is too small. 'min' is inclusive, so +1.\n min = mid + 1;\n }\n }\n // presumably, min==max now.\n return min;\n}\n"]}
\No newline at end of file