wxPromise.js 24 KB

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