wxPromise.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * wxPromise
  3. * https://github.com/youngjuning/wxPromise
  4. *
  5. * NPM INSTALL
  6. * https://www.npmjs.com/package/wx-promise-pro
  7. *
  8. * Copyright 2018, youngjuning
  9. * http://www.wakeuptocode.me
  10. *
  11. * Licensed under the MIT license:
  12. * https://opensource.org/licenses/MIT
  13. */
  14. // 把普通函数变成promise函数
  15. const promisify = api => {
  16. return (options, ...params) => {
  17. return new Promise((resolve, reject) => {
  18. api(Object.assign({}, options, {
  19. success: resolve,
  20. fail: reject
  21. }), ...params);
  22. Promise.prototype.finally = function (callback) {
  23. let P = this.constructor;
  24. return this.then(value => P.resolve(callback()).then(() => value), reason => P.resolve(callback()).then(() => {
  25. throw reason;
  26. }));
  27. };
  28. });
  29. };
  30. };
  31. uni.pro = {}; // 以下是没有 success、fail、complete 属性的api
  32. // 1、...Sync【√】
  33. // 2、on...【√】
  34. // 3、create... 除了 createBLEConnection【√】
  35. // 4、...Manager【√】
  36. // 5、pause...【√】
  37. // 6、stopRecord、stopVoice、stopBackgroundAudio、stopPullDownRefresh【√】
  38. // 7、hideKeyboard、hideToast、hideLoading、showNavigationBarLoading、hideNavigationBarLoading【√】
  39. // 8、canIUse、navigateBack、closeSocket、pageScrollTo、drawCanvas【√】
  40. const wxPromise = () => {
  41. // 将 promise 方法 挂载到 wx.pro 对象上
  42. for (let key in wx) {
  43. if (uni.hasOwnProperty(key)) {
  44. if (/^on|^create|Sync$|Manager$|^pause/.test(key) && key !== 'createBLEConnection' || key === 'stopRecord' || key === 'stopVoice' || key === 'stopBackgroundAudio' || key === 'stopPullDownRefresh' || key === 'hideKeyboard' || key === 'hideToast' || key === 'hideLoading' || key === 'showNavigationBarLoading' || key === 'hideNavigationBarLoading' || key === 'canIUse' || key === 'navigateBack' || key === 'closeSocket' || key === 'closeSocket' || key === 'pageScrollTo' || key === 'drawCanvas') {
  45. uni.pro[key] = uni[key];
  46. } else {
  47. uni.pro[key] = promisify(uni[key]);
  48. }
  49. }
  50. }
  51. /**
  52. * 正则匹配
  53. * @param {[string]} type [类型]
  54. * @param {[string]} str [要匹配的字符串]
  55. */
  56. uni.pro.match = (type, str) => {
  57. let reg = '';
  58. if (type === 'chinese') {
  59. // 匹配中文字符
  60. reg = /[\u4e00-\u9fa5]/gm;
  61. } else if (type === 'email') {
  62. // 匹配email地址
  63. reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
  64. } else if (type === 'url') {
  65. // 匹配URL地址
  66. reg = /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i;
  67. } else if (type === 'phoneNumber') {
  68. // 匹配手机号码
  69. reg = /^(0|86|17951)?(13[0-9]|14[579]|15[012356789]|16[56]|17[1235678]|18[0-9]|19[89])[0-9]{8}$/;
  70. } else if (type === 'cardid') {
  71. // 匹配身份证号
  72. reg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
  73. } else if (type === 'mail') {
  74. // 匹配邮编号
  75. reg = /^[1-9]\d{5}(?!\d)$/;
  76. }
  77. if (reg.test(str)) {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }, // 顶部提示框
  83. uni.pro.showTopTips = (option, that) => {
  84. return new Promise((resolve, reject) => {
  85. if (!option) {
  86. reject('缺少配置项!');
  87. }
  88. if (!option.content) {
  89. reject('option.content属性是必须的');
  90. } // 如果topTips属性不存在就初始化为一个对象
  91. let topTips = that.data.topTips || {}; // 如果已经有一个定时器在了,就清理掉先
  92. if (topTips.timeout) {
  93. clearTimeout(topTips.timeout);
  94. topTips.timeout = 0;
  95. } // 如果没有设置duration则默认3000ms
  96. if (option.duration === undefined) {
  97. option.duration = 3000;
  98. } // 设置超时定时器,定时关闭topTips
  99. var timeout = setTimeout(() => {
  100. that.setData({
  101. "topTips.show": false,
  102. "topTips.timeout": 0
  103. }, () => {
  104. resolve();
  105. });
  106. }, option.duration); // 展示出topTips
  107. that.setData({
  108. topTips: {
  109. show: true,
  110. content: option.content,
  111. // content属性必选
  112. timeout // 把超时定时器赋值给topTip对象
  113. }
  114. });
  115. });
  116. }; // 初始化 echarts
  117. uni.pro.initChart = (option, echarts) => {
  118. return (canvas, width, height) => {
  119. const chart = echarts.init(canvas, null, {
  120. width: width,
  121. height: height
  122. });
  123. canvas.setChart(chart);
  124. chart.setOption(option);
  125. return chart;
  126. };
  127. }, // echarts 延迟加载模式初始化
  128. uni.pro.lazyInitChart = (option, echarts, componentId, that) => {
  129. return new Promise((resolve, reject, canvas, width, height) => {
  130. that.$mp.page.selectComponent(componentId).$vm.init((canvas, width, height) => {
  131. // 获取组件的 canvas、width、height 后的回调函数
  132. // 在这里初始化图表
  133. const chart = echarts.init(canvas, null, {
  134. width: width,
  135. height: height
  136. });
  137. chart.setOption(option); // 将图表实例绑定到 this 上,可以在其他成员函数(如 dispose)中访问
  138. that.chart = chart; // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  139. resolve(chart);
  140. });
  141. });
  142. },
  143. /**
  144. * 更新 echarts
  145. * echats 没有提供 update 的方法,因此我们利用 clear() 和 setOption() api 组合实现了这个功能
  146. */
  147. uni.pro.updateChart = (chart, option) => {
  148. // 在chart.setOption之前用chart.clear来清除之前的图表,否则会出现一个非常奇怪的图表
  149. chart.clear(); // option 是在网络请求之后异步动态获取的
  150. chart.setOption(option); // 因为不是 dispose,所以不需要重新绑定到this上,也不需要再次返回实例
  151. }; // 保存图片到系统相册。需要用户授权 scope.writePhotosAlbum
  152. uni.pro.saveImageToPhotosAlbum = tempFilePath => {
  153. return new Promise((resolve, reject) => {
  154. uni.getSetting({
  155. success: res => {
  156. if (!res.authSetting['scope.writePhotosAlbum']) {
  157. // 没有授权,向用户发起授权请求
  158. uni.authorize({
  159. scope: 'scope.writePhotosAlbum',
  160. success: () => {
  161. // 用户同意授权,调用 api 保存图片
  162. uni.saveImageToPhotosAlbum({
  163. filePath: tempFilePath,
  164. success(res) {
  165. resolve(res);
  166. },
  167. fail(err) {
  168. reject(err);
  169. }
  170. });
  171. },
  172. fail: err => {
  173. // 用户拒绝授权,提醒用户打开设置页面
  174. uni.hideLoading();
  175. uni.pro.showModal({
  176. title: '温馨提示',
  177. content: '请授权系统使用保存图片接口',
  178. confirmText: '知道了',
  179. showCancel: false
  180. }).then(res => {
  181. uni.openSetting();
  182. });
  183. }
  184. });
  185. } else {
  186. // 已经授权,直接调用 api 保存图片
  187. uni.saveImageToPhotosAlbum({
  188. filePath: tempFilePath,
  189. success(res) {
  190. resolve(res);
  191. },
  192. fail(err) {
  193. reject(err);
  194. }
  195. });
  196. }
  197. },
  198. fail: err => {
  199. reject(err);
  200. }
  201. });
  202. });
  203. }, // 把当前画布指定区域的内容导出生成指定大小的图片,并返回文件路径
  204. // TODO: 增加更多的参数配置
  205. uni.pro.canvasToTempFilePath = canvasContext => {
  206. return new Promise((resolve, reject) => {
  207. canvasContext.draw(true, () => {
  208. uni.canvasToTempFilePath({
  209. canvasId: 'card',
  210. success: res => {
  211. resolve(res);
  212. },
  213. fail: err => {
  214. reject(err);
  215. }
  216. });
  217. });
  218. });
  219. };
  220. };
  221. wxPromise();
  222. /**
  223. * Copyright (c) 2014-present, Facebook, Inc.
  224. *
  225. * This source code is licensed under the MIT license found in the
  226. * LICENSE file in the root directory of this source tree.
  227. */
  228. !function (global) {
  229. "use strict";
  230. var Op = Object.prototype;
  231. var hasOwn = Op.hasOwnProperty;
  232. var undefined; // More compressible than void 0.
  233. var $Symbol = typeof Symbol === "function" ? Symbol : {};
  234. var iteratorSymbol = $Symbol.iterator || "@@iterator";
  235. var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
  236. var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  237. var inModule = typeof module === "object";
  238. var runtime = global.regeneratorRuntime;
  239. if (runtime) {
  240. if (inModule) {
  241. module.exports = runtime;
  242. }
  243. return;
  244. }
  245. runtime = global.regeneratorRuntime = inModule ? module.exports : {};
  246. function wrap(innerFn, outerFn, self, tryLocsList) {
  247. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
  248. var generator = Object.create(protoGenerator.prototype);
  249. var context = new Context(tryLocsList || []);
  250. generator._invoke = makeInvokeMethod(innerFn, self, context);
  251. return generator;
  252. }
  253. runtime.wrap = wrap;
  254. function tryCatch(fn, obj, arg) {
  255. try {
  256. return {
  257. type: "normal",
  258. arg: fn.call(obj, arg)
  259. };
  260. } catch (err) {
  261. return {
  262. type: "throw",
  263. arg: err
  264. };
  265. }
  266. }
  267. var GenStateSuspendedStart = "suspendedStart";
  268. var GenStateSuspendedYield = "suspendedYield";
  269. var GenStateExecuting = "executing";
  270. var GenStateCompleted = "completed";
  271. var ContinueSentinel = {};
  272. function Generator() {}
  273. function GeneratorFunction() {}
  274. function GeneratorFunctionPrototype() {}
  275. var IteratorPrototype = {};
  276. IteratorPrototype[iteratorSymbol] = function () {
  277. return this;
  278. };
  279. var getProto = Object.getPrototypeOf;
  280. var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  281. if (NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
  282. IteratorPrototype = NativeIteratorPrototype;
  283. }
  284. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
  285. GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
  286. GeneratorFunctionPrototype.constructor = GeneratorFunction;
  287. GeneratorFunctionPrototype[toStringTagSymbol] = GeneratorFunction.displayName = "GeneratorFunction";
  288. function defineIteratorMethods(prototype) {
  289. ["next", "throw", "return"].forEach(function (method) {
  290. prototype[method] = function (arg) {
  291. return this._invoke(method, arg);
  292. };
  293. });
  294. }
  295. runtime.isGeneratorFunction = function (genFun) {
  296. var ctor = typeof genFun === "function" && genFun.constructor;
  297. return ctor ? ctor === GeneratorFunction || (ctor.displayName || ctor.name) === "GeneratorFunction" : false;
  298. };
  299. runtime.mark = function (genFun) {
  300. if (Object.setPrototypeOf) {
  301. Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
  302. } else {
  303. genFun.__proto__ = GeneratorFunctionPrototype;
  304. if (!(toStringTagSymbol in genFun)) {
  305. genFun[toStringTagSymbol] = "GeneratorFunction";
  306. }
  307. }
  308. genFun.prototype = Object.create(Gp);
  309. return genFun;
  310. };
  311. runtime.awrap = function (arg) {
  312. return {
  313. __await: arg
  314. };
  315. };
  316. function AsyncIterator(generator) {
  317. function invoke(method, arg, resolve, reject) {
  318. var record = tryCatch(generator[method], generator, arg);
  319. if (record.type === "throw") {
  320. reject(record.arg);
  321. } else {
  322. var result = record.arg;
  323. var value = result.value;
  324. if (value && typeof value === "object" && hasOwn.call(value, "__await")) {
  325. return Promise.resolve(value.__await).then(function (value) {
  326. invoke("next", value, resolve, reject);
  327. }, function (err) {
  328. invoke("throw", err, resolve, reject);
  329. });
  330. }
  331. return Promise.resolve(value).then(function (unwrapped) {
  332. result.value = unwrapped;
  333. resolve(result);
  334. }, reject);
  335. }
  336. }
  337. var previousPromise;
  338. function enqueue(method, arg) {
  339. function callInvokeWithMethodAndArg() {
  340. return new Promise(function (resolve, reject) {
  341. invoke(method, arg, resolve, reject);
  342. });
  343. }
  344. return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
  345. }
  346. this._invoke = enqueue;
  347. }
  348. defineIteratorMethods(AsyncIterator.prototype);
  349. AsyncIterator.prototype[asyncIteratorSymbol] = function () {
  350. return this;
  351. };
  352. runtime.AsyncIterator = AsyncIterator;
  353. runtime.async = function (innerFn, outerFn, self, tryLocsList) {
  354. var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList));
  355. return runtime.isGeneratorFunction(outerFn) ? iter // If outerFn is a generator, return the full iterator.
  356. : iter.next().then(function (result) {
  357. return result.done ? result.value : iter.next();
  358. });
  359. };
  360. function makeInvokeMethod(innerFn, self, context) {
  361. var state = GenStateSuspendedStart;
  362. return function invoke(method, arg) {
  363. if (state === GenStateExecuting) {
  364. throw new Error("Generator is already running");
  365. }
  366. if (state === GenStateCompleted) {
  367. if (method === "throw") {
  368. throw arg;
  369. } // Be forgiving, per 25.3.3.3.3 of the spec:
  370. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
  371. return doneResult();
  372. }
  373. context.method = method;
  374. context.arg = arg;
  375. while (true) {
  376. var delegate = context.delegate;
  377. if (delegate) {
  378. var delegateResult = maybeInvokeDelegate(delegate, context);
  379. if (delegateResult) {
  380. if (delegateResult === ContinueSentinel) continue;
  381. return delegateResult;
  382. }
  383. }
  384. if (context.method === "next") {
  385. context.sent = context._sent = context.arg;
  386. } else if (context.method === "throw") {
  387. if (state === GenStateSuspendedStart) {
  388. state = GenStateCompleted;
  389. throw context.arg;
  390. }
  391. context.dispatchException(context.arg);
  392. } else if (context.method === "return") {
  393. context.abrupt("return", context.arg);
  394. }
  395. state = GenStateExecuting;
  396. var record = tryCatch(innerFn, self, context);
  397. if (record.type === "normal") {
  398. state = context.done ? GenStateCompleted : GenStateSuspendedYield;
  399. if (record.arg === ContinueSentinel) {
  400. continue;
  401. }
  402. return {
  403. value: record.arg,
  404. done: context.done
  405. };
  406. } else if (record.type === "throw") {
  407. state = GenStateCompleted;
  408. context.method = "throw";
  409. context.arg = record.arg;
  410. }
  411. }
  412. };
  413. }
  414. function maybeInvokeDelegate(delegate, context) {
  415. var method = delegate.iterator[context.method];
  416. if (method === undefined) {
  417. context.delegate = null;
  418. if (context.method === "throw") {
  419. if (delegate.iterator.return) {
  420. context.method = "return";
  421. context.arg = undefined;
  422. maybeInvokeDelegate(delegate, context);
  423. if (context.method === "throw") {
  424. return ContinueSentinel;
  425. }
  426. }
  427. context.method = "throw";
  428. context.arg = new TypeError("The iterator does not provide a 'throw' method");
  429. }
  430. return ContinueSentinel;
  431. }
  432. var record = tryCatch(method, delegate.iterator, context.arg);
  433. if (record.type === "throw") {
  434. context.method = "throw";
  435. context.arg = record.arg;
  436. context.delegate = null;
  437. return ContinueSentinel;
  438. }
  439. var info = record.arg;
  440. if (!info) {
  441. context.method = "throw";
  442. context.arg = new TypeError("iterator result is not an object");
  443. context.delegate = null;
  444. return ContinueSentinel;
  445. }
  446. if (info.done) {
  447. context[delegate.resultName] = info.value;
  448. context.next = delegate.nextLoc;
  449. if (context.method !== "return") {
  450. context.method = "next";
  451. context.arg = undefined;
  452. }
  453. } else {
  454. return info;
  455. }
  456. context.delegate = null;
  457. return ContinueSentinel;
  458. }
  459. defineIteratorMethods(Gp);
  460. Gp[toStringTagSymbol] = "Generator";
  461. Gp[iteratorSymbol] = function () {
  462. return this;
  463. };
  464. Gp.toString = function () {
  465. return "[object Generator]";
  466. };
  467. function pushTryEntry(locs) {
  468. var entry = {
  469. tryLoc: locs[0]
  470. };
  471. if (1 in locs) {
  472. entry.catchLoc = locs[1];
  473. }
  474. if (2 in locs) {
  475. entry.finallyLoc = locs[2];
  476. entry.afterLoc = locs[3];
  477. }
  478. this.tryEntries.push(entry);
  479. }
  480. function resetTryEntry(entry) {
  481. var record = entry.completion || {};
  482. record.type = "normal";
  483. delete record.arg;
  484. entry.completion = record;
  485. }
  486. function Context(tryLocsList) {
  487. this.tryEntries = [{
  488. tryLoc: "root"
  489. }];
  490. tryLocsList.forEach(pushTryEntry, this);
  491. this.reset(true);
  492. }
  493. runtime.keys = function (object) {
  494. var keys = [];
  495. for (var key in object) {
  496. keys.push(key);
  497. }
  498. keys.reverse();
  499. return function next() {
  500. while (keys.length) {
  501. var key = keys.pop();
  502. if (key in object) {
  503. next.value = key;
  504. next.done = false;
  505. return next;
  506. }
  507. }
  508. next.done = true;
  509. return next;
  510. };
  511. };
  512. function values(iterable) {
  513. if (iterable) {
  514. var iteratorMethod = iterable[iteratorSymbol];
  515. if (iteratorMethod) {
  516. return iteratorMethod.call(iterable);
  517. }
  518. if (typeof iterable.next === "function") {
  519. return iterable;
  520. }
  521. if (!isNaN(iterable.length)) {
  522. var i = -1,
  523. next = function next() {
  524. while (++i < iterable.length) {
  525. if (hasOwn.call(iterable, i)) {
  526. next.value = iterable[i];
  527. next.done = false;
  528. return next;
  529. }
  530. }
  531. next.value = undefined;
  532. next.done = true;
  533. return next;
  534. };
  535. return next.next = next;
  536. }
  537. } // Return an iterator with no values.
  538. return {
  539. next: doneResult
  540. };
  541. }
  542. runtime.values = values;
  543. function doneResult() {
  544. return {
  545. value: undefined,
  546. done: true
  547. };
  548. }
  549. Context.prototype = {
  550. constructor: Context,
  551. reset: function (skipTempReset) {
  552. this.prev = 0;
  553. this.next = 0; // Resetting context._sent for legacy support of Babel's
  554. // function.sent implementation.
  555. this.sent = this._sent = undefined;
  556. this.done = false;
  557. this.delegate = null;
  558. this.method = "next";
  559. this.arg = undefined;
  560. this.tryEntries.forEach(resetTryEntry);
  561. if (!skipTempReset) {
  562. for (var name in this) {
  563. // Not sure about the optimal order of these conditions:
  564. if (name.charAt(0) === "t" && hasOwn.call(this, name) && !isNaN(+name.slice(1))) {
  565. this[name] = undefined;
  566. }
  567. }
  568. }
  569. },
  570. stop: function () {
  571. this.done = true;
  572. var rootEntry = this.tryEntries[0];
  573. var rootRecord = rootEntry.completion;
  574. if (rootRecord.type === "throw") {
  575. throw rootRecord.arg;
  576. }
  577. return this.rval;
  578. },
  579. dispatchException: function (exception) {
  580. if (this.done) {
  581. throw exception;
  582. }
  583. var context = this;
  584. function handle(loc, caught) {
  585. record.type = "throw";
  586. record.arg = exception;
  587. context.next = loc;
  588. if (caught) {
  589. // If the dispatched exception was caught by a catch block,
  590. // then let that catch block handle the exception normally.
  591. context.method = "next";
  592. context.arg = undefined;
  593. }
  594. return !!caught;
  595. }
  596. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  597. var entry = this.tryEntries[i];
  598. var record = entry.completion;
  599. if (entry.tryLoc === "root") {
  600. // Exception thrown outside of any try block that could handle
  601. // it, so set the completion value of the entire function to
  602. // throw the exception.
  603. return handle("end");
  604. }
  605. if (entry.tryLoc <= this.prev) {
  606. var hasCatch = hasOwn.call(entry, "catchLoc");
  607. var hasFinally = hasOwn.call(entry, "finallyLoc");
  608. if (hasCatch && hasFinally) {
  609. if (this.prev < entry.catchLoc) {
  610. return handle(entry.catchLoc, true);
  611. } else if (this.prev < entry.finallyLoc) {
  612. return handle(entry.finallyLoc);
  613. }
  614. } else if (hasCatch) {
  615. if (this.prev < entry.catchLoc) {
  616. return handle(entry.catchLoc, true);
  617. }
  618. } else if (hasFinally) {
  619. if (this.prev < entry.finallyLoc) {
  620. return handle(entry.finallyLoc);
  621. }
  622. } else {
  623. throw new Error("try statement without catch or finally");
  624. }
  625. }
  626. }
  627. },
  628. abrupt: function (type, arg) {
  629. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  630. var entry = this.tryEntries[i];
  631. if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
  632. var finallyEntry = entry;
  633. break;
  634. }
  635. }
  636. if (finallyEntry && (type === "break" || type === "continue") && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc) {
  637. finallyEntry = null;
  638. }
  639. var record = finallyEntry ? finallyEntry.completion : {};
  640. record.type = type;
  641. record.arg = arg;
  642. if (finallyEntry) {
  643. this.method = "next";
  644. this.next = finallyEntry.finallyLoc;
  645. return ContinueSentinel;
  646. }
  647. return this.complete(record);
  648. },
  649. complete: function (record, afterLoc) {
  650. if (record.type === "throw") {
  651. throw record.arg;
  652. }
  653. if (record.type === "break" || record.type === "continue") {
  654. this.next = record.arg;
  655. } else if (record.type === "return") {
  656. this.rval = this.arg = record.arg;
  657. this.method = "return";
  658. this.next = "end";
  659. } else if (record.type === "normal" && afterLoc) {
  660. this.next = afterLoc;
  661. }
  662. return ContinueSentinel;
  663. },
  664. finish: function (finallyLoc) {
  665. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  666. var entry = this.tryEntries[i];
  667. if (entry.finallyLoc === finallyLoc) {
  668. this.complete(entry.completion, entry.afterLoc);
  669. resetTryEntry(entry);
  670. return ContinueSentinel;
  671. }
  672. }
  673. },
  674. "catch": function (tryLoc) {
  675. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  676. var entry = this.tryEntries[i];
  677. if (entry.tryLoc === tryLoc) {
  678. var record = entry.completion;
  679. if (record.type === "throw") {
  680. var thrown = record.arg;
  681. resetTryEntry(entry);
  682. }
  683. return thrown;
  684. }
  685. } // The context.catch method must only be called with a location
  686. // argument that corresponds to a known catch block.
  687. throw new Error("illegal catch attempt");
  688. },
  689. delegateYield: function (iterable, resultName, nextLoc) {
  690. this.delegate = {
  691. iterator: values(iterable),
  692. resultName: resultName,
  693. nextLoc: nextLoc
  694. };
  695. if (this.method === "next") {
  696. // Deliberately forget the last sent value so that we don't
  697. // accidentally pass it on to the delegate.
  698. this.arg = undefined;
  699. }
  700. return ContinueSentinel;
  701. }
  702. };
  703. }(function () {
  704. return this;
  705. }() || Function("return this")());