1 | {"version":3,"sources":["../src/index.js"],"names":["MAX_DELAY","Job","constructor","schedule","fn","scheduledDate","wrapper","now","Date","_timeout","setTimeout","_isRunning","result","_","then","call","scheduleNext","_isEnabled","_createDate","_schedule","delay","_scheduleNext","undefined","start","stop","clearTimeout","Schedule","pattern","zone","toLowerCase","moment","utc","tz","createJob","next","n","dates","Array","date","i","toDate","startJob","job","bind","createSchedule","args"],"mappings":";;;;;;;AAAA;;AAEA;;AACA;;;;AAEA,MAAMA,SAAS,GAAG,YAAK,EAAL,IAAU,CAA5B;;AAEA,MAAMC,GAAN,CAAU;AACRC,EAAAA,WAAW,CAACC,QAAD,EAAWC,EAAX,EAAe;AACxB,QAAIC,aAAJ;;AACA,UAAMC,OAAO,GAAG,MAAM;AACpB,YAAMC,GAAG,GAAGC,IAAI,CAACD,GAAL,EAAZ;;AACA,UAAIF,aAAa,GAAGE,GAApB,EAAyB;AAMvB,aAAKE,QAAL,GAAgBC,UAAU,CAACJ,OAAD,EAAUD,aAAa,GAAGE,GAA1B,CAA1B;AACA;AACD;;AAED,WAAKI,UAAL,GAAkB,IAAlB;AAEA,UAAIC,MAAJ;;AACA,UAAI;AACFA,QAAAA,MAAM,GAAGR,EAAE,EAAX;AACD,OAFD,CAEE,OAAOS,CAAP,EAAU,CAEX;;AACD,UAAIC,IAAJ;;AACA,UAAIF,MAAM,IAAI,IAAV,IAAkB,QAAQE,IAAI,GAAGF,MAAM,CAACE,IAAtB,MAAgC,UAAtD,EAAkE;AAChEA,QAAAA,IAAI,CAACC,IAAL,CAAUH,MAAV,EAAkBI,YAAlB,EAAgCA,YAAhC;AACD,OAFD,MAEO;AACLA,QAAAA,YAAY;AACb;AACF,KA1BD;;AA2BA,UAAMA,YAAY,GAAG,MAAM;AACzB,WAAKL,UAAL,GAAkB,KAAlB;;AAEA,UAAI,KAAKM,UAAT,EAAqB;AACnB,cAAMV,GAAG,GAAGJ,QAAQ,CAACe,WAAT,EAAZ;;AACAb,QAAAA,aAAa,GAAG,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,IAAI,GAAG,KAAKpB,WAAL,EAAX;;AACA,SAAK,IAAIqB,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGJ,CAApB,EAAuB,EAAEI,CAAzB,EAA4B;AAC1BH,MAAAA,KAAK,CAACG,CAAD,CAAL,GAAW,CAACD,IAAI,GAAG,mBAAKnC,QAAL,EAAemC,IAAf,CAAR,EAA8BE,MAA9B,EAAX;AACD;;AACD,WAAOJ,KAAP;AACD;;AAEDK,EAAAA,QAAQ,CAACrC,EAAD,EAAK;AACX,UAAMsC,GAAG,GAAG,KAAKT,SAAL,CAAe7B,EAAf,CAAZ;AACAsC,IAAAA,GAAG,CAACnB,KAAJ;AACA,WAAOmB,GAAG,CAAClB,IAAJ,CAASmB,IAAT,CAAcD,GAAd,CAAP;AACD;;AA7BY;;AAgCR,MAAME,cAAc,GAAG,CAAC,GAAGC,IAAJ,KAAa,IAAInB,QAAJ,CAAa,GAAGmB,IAAhB,CAApC","sourcesContent":["import moment from 'moment-timezone'\n\nimport next from './next'\nimport parse from './parse'\n\nconst MAX_DELAY = 2 ** 31 - 1\n\nclass Job {\n constructor(schedule, fn) {\n let scheduledDate\n const wrapper = () => {\n const now = Date.now()\n if (scheduledDate > now) {\n // we're early, delay\n //\n // no need to check _isEnabled, we're just delaying the existing timeout\n //\n // see https://github.com/vatesfr/xen-orchestra/issues/4625\n this._timeout = setTimeout(wrapper, scheduledDate - now)\n return\n }\n\n this._isRunning = true\n\n let result\n try {\n result = fn()\n } catch (_) {\n // catch any thrown value to ensure it does not break the job\n }\n let then\n if (result != null && typeof (then = result.then) === 'function') {\n then.call(result, scheduleNext, scheduleNext)\n } else {\n scheduleNext()\n }\n }\n const scheduleNext = () => {\n this._isRunning = false\n\n if (this._isEnabled) {\n const now = schedule._createDate()\n scheduledDate = +next(schedule._schedule, now)\n const delay = scheduledDate - now\n this._timeout =\n delay < MAX_DELAY\n ? setTimeout(wrapper, delay)\n : setTimeout(scheduleNext, MAX_DELAY)\n }\n }\n\n this._isEnabled = false\n this._isRunning = false\n this._scheduleNext = scheduleNext\n this._timeout = undefined\n }\n\n start() {\n this.stop()\n\n this._isEnabled = true\n if (!this._isRunning) {\n this._scheduleNext()\n }\n }\n\n stop() {\n this._isEnabled = false\n clearTimeout(this._timeout)\n }\n}\n\nclass Schedule {\n constructor(pattern, zone = 'utc') {\n this._schedule = parse(pattern)\n this._createDate =\n zone.toLowerCase() === 'utc'\n ? moment.utc\n : zone === 'local'\n ? moment\n : () => moment.tz(zone)\n }\n\n createJob(fn) {\n return new Job(this, fn)\n }\n\n next(n) {\n const dates = new Array(n)\n const schedule = this._schedule\n let date = this._createDate()\n for (let i = 0; i < n; ++i) {\n dates[i] = (date = next(schedule, date)).toDate()\n }\n return dates\n }\n\n startJob(fn) {\n const job = this.createJob(fn)\n job.start()\n return job.stop.bind(job)\n }\n}\n\nexport const createSchedule = (...args) => new Schedule(...args)\n"],"file":"index.js"} |