UNPKG

43.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var lib = {};
6
7Object.defineProperty(lib, '__esModule', { value: true });
8
9/******************************************************************************
10Copyright (c) Microsoft Corporation.
11
12Permission to use, copy, modify, and/or distribute this software for any
13purpose with or without fee is hereby granted.
14
15THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
16REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
18INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21PERFORMANCE OF THIS SOFTWARE.
22***************************************************************************** */
23/* global Reflect, Promise */
24
25var extendStatics$1 = function(d, b) {
26 extendStatics$1 = Object.setPrototypeOf ||
27 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
28 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
29 return extendStatics$1(d, b);
30};
31
32function __extends$1(d, b) {
33 if (typeof b !== "function" && b !== null)
34 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
35 extendStatics$1(d, b);
36 function __() { this.constructor = d; }
37 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
38}
39
40var __assign = function() {
41 __assign = Object.assign || function __assign(t) {
42 for (var s, i = 1, n = arguments.length; i < n; i++) {
43 s = arguments[i];
44 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
45 }
46 return t;
47 };
48 return __assign.apply(this, arguments);
49};
50
51function __rest(s, e) {
52 var t = {};
53 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
54 t[p] = s[p];
55 if (s != null && typeof Object.getOwnPropertySymbols === "function")
56 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
57 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
58 t[p[i]] = s[p[i]];
59 }
60 return t;
61}
62
63function __awaiter$1(thisArg, _arguments, P, generator) {
64 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
65 return new (P || (P = Promise))(function (resolve, reject) {
66 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
67 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
68 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
69 step((generator = generator.apply(thisArg, _arguments || [])).next());
70 });
71}
72
73function __generator$1(thisArg, body) {
74 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
75 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
76 function verb(n) { return function (v) { return step([n, v]); }; }
77 function step(op) {
78 if (f) throw new TypeError("Generator is already executing.");
79 while (_) try {
80 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
81 if (y = 0, t) op = [op[0] & 2, t.value];
82 switch (op[0]) {
83 case 0: case 1: t = op; break;
84 case 4: _.label++; return { value: op[1], done: false };
85 case 5: _.label++; y = op[1]; op = [0]; continue;
86 case 7: op = _.ops.pop(); _.trys.pop(); continue;
87 default:
88 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
89 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
90 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
91 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
92 if (t[2]) _.ops.pop();
93 _.trys.pop(); continue;
94 }
95 op = body.call(thisArg, _);
96 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
97 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
98 }
99}
100
101var PROVIDER$1 = "lc_weapp";
102var PLATFORM$1 = "weixin";
103function getLoginCode() {
104 return new Promise(function (resolve, reject) {
105 wx.login({
106 success: function (res) {
107 return res.code ? resolve(res.code) : reject(new Error(res.errMsg));
108 },
109 fail: function (_a) {
110 var errMsg = _a.errMsg;
111 return reject(new Error(errMsg));
112 },
113 });
114 });
115}
116var getAuthInfo$1 = function (_a) {
117 var _b = _a === void 0 ? {} : _a, _c = _b.platform, platform = _c === void 0 ? PLATFORM$1 : _c, _d = _b.preferUnionId, preferUnionId = _d === void 0 ? false : _d, _e = _b.asMainAccount, asMainAccount = _e === void 0 ? false : _e;
118 return __awaiter$1(this, void 0, void 0, function () {
119 var code, authData;
120 return __generator$1(this, function (_f) {
121 switch (_f.label) {
122 case 0: return [4 /*yield*/, getLoginCode()];
123 case 1:
124 code = _f.sent();
125 authData = { code: code };
126 if (preferUnionId) {
127 authData.platform = platform;
128 authData.main_account = asMainAccount;
129 }
130 return [2 /*return*/, {
131 authData: authData,
132 platform: platform,
133 provider: PROVIDER$1,
134 }];
135 }
136 });
137 });
138};
139
140var storage = {
141 getItem: function (key) {
142 return wx.getStorageSync(key);
143 },
144 setItem: function (key, value) {
145 return wx.setStorageSync(key, value);
146 },
147 removeItem: function (key) {
148 return wx.removeStorageSync(key);
149 },
150 clear: function () {
151 return wx.clearStorageSync();
152 },
153};
154
155/******************************************************************************
156Copyright (c) Microsoft Corporation.
157
158Permission to use, copy, modify, and/or distribute this software for any
159purpose with or without fee is hereby granted.
160
161THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
162REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
163AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
164INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
165LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
166OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
167PERFORMANCE OF THIS SOFTWARE.
168***************************************************************************** */
169/* global Reflect, Promise */
170
171var extendStatics = function(d, b) {
172 extendStatics = Object.setPrototypeOf ||
173 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
174 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
175 return extendStatics(d, b);
176};
177
178function __extends(d, b) {
179 if (typeof b !== "function" && b !== null)
180 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
181 extendStatics(d, b);
182 function __() { this.constructor = d; }
183 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
184}
185
186var AbortError = /** @class */ (function (_super) {
187 __extends(AbortError, _super);
188 function AbortError() {
189 var _this = _super !== null && _super.apply(this, arguments) || this;
190 _this.name = "AbortError";
191 return _this;
192 }
193 return AbortError;
194}(Error));
195
196var request = function (url, options) {
197 if (options === void 0) { options = {}; }
198 var method = options.method, data = options.data, headers = options.headers, signal = options.signal;
199 if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
200 return Promise.reject(new AbortError("Request aborted"));
201 }
202 return new Promise(function (resolve, reject) {
203 var task = wx.request({
204 url: url,
205 method: method,
206 data: data,
207 header: headers,
208 complete: function (res) {
209 signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", abortListener);
210 if (!res.statusCode) {
211 reject(new Error(res.errMsg));
212 return;
213 }
214 resolve({
215 ok: !(res.statusCode >= 400),
216 status: res.statusCode,
217 headers: res.header,
218 data: res.data,
219 });
220 },
221 });
222 var abortListener = function () {
223 reject(new AbortError("Request aborted"));
224 task.abort();
225 };
226 signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", abortListener);
227 });
228};
229var upload = function (url, file, options) {
230 if (options === void 0) { options = {}; }
231 var headers = options.headers, data = options.data, onprogress = options.onprogress, signal = options.signal;
232 if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
233 return Promise.reject(new AbortError("Request aborted"));
234 }
235 if (!(file && file.data && file.data.uri)) {
236 return Promise.reject(new TypeError("File data must be an object like { uri: localPath }."));
237 }
238 return new Promise(function (resolve, reject) {
239 var task = wx.uploadFile({
240 url: url,
241 header: headers,
242 filePath: file.data.uri,
243 name: file.field,
244 formData: data,
245 success: function (response) {
246 var status = response.statusCode, data = response.data, rest = __rest(response, ["statusCode", "data"]);
247 resolve(__assign(__assign({}, rest), { data: typeof data === "string" ? JSON.parse(data) : data, status: status, ok: !(status >= 400) }));
248 },
249 fail: function (response) {
250 reject(new Error(response.errMsg));
251 },
252 complete: function () {
253 signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", abortListener);
254 },
255 });
256 var abortListener = function () {
257 reject(new AbortError("Request aborted"));
258 task.abort();
259 };
260 signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", abortListener);
261 if (onprogress) {
262 task.onProgressUpdate(function (event) {
263 return onprogress({
264 loaded: event.totalBytesSent,
265 total: event.totalBytesExpectedToSend,
266 percent: event.progress,
267 });
268 });
269 }
270 });
271};
272
273/**
274 * @author Toru Nagashima <https://github.com/mysticatea>
275 * @copyright 2015 Toru Nagashima. All rights reserved.
276 * See LICENSE file in root directory for full license.
277 */
278/**
279 * @typedef {object} PrivateData
280 * @property {EventTarget} eventTarget The event target.
281 * @property {{type:string}} event The original event object.
282 * @property {number} eventPhase The current event phase.
283 * @property {EventTarget|null} currentTarget The current event target.
284 * @property {boolean} canceled The flag to prevent default.
285 * @property {boolean} stopped The flag to stop propagation.
286 * @property {boolean} immediateStopped The flag to stop propagation immediately.
287 * @property {Function|null} passiveListener The listener if the current listener is passive. Otherwise this is null.
288 * @property {number} timeStamp The unix time.
289 * @private
290 */
291
292/**
293 * Private data for event wrappers.
294 * @type {WeakMap<Event, PrivateData>}
295 * @private
296 */
297const privateData = new WeakMap();
298
299/**
300 * Cache for wrapper classes.
301 * @type {WeakMap<Object, Function>}
302 * @private
303 */
304const wrappers = new WeakMap();
305
306/**
307 * Get private data.
308 * @param {Event} event The event object to get private data.
309 * @returns {PrivateData} The private data of the event.
310 * @private
311 */
312function pd(event) {
313 const retv = privateData.get(event);
314 return retv
315}
316
317/**
318 * https://dom.spec.whatwg.org/#set-the-canceled-flag
319 * @param data {PrivateData} private data.
320 */
321function setCancelFlag(data) {
322 if (data.passiveListener != null) {
323 if (
324 typeof console !== "undefined" &&
325 typeof console.error === "function"
326 ) {
327 console.error(
328 "Unable to preventDefault inside passive event listener invocation.",
329 data.passiveListener
330 );
331 }
332 return
333 }
334 if (!data.event.cancelable) {
335 return
336 }
337
338 data.canceled = true;
339 if (typeof data.event.preventDefault === "function") {
340 data.event.preventDefault();
341 }
342}
343
344/**
345 * @see https://dom.spec.whatwg.org/#interface-event
346 * @private
347 */
348/**
349 * The event wrapper.
350 * @constructor
351 * @param {EventTarget} eventTarget The event target of this dispatching.
352 * @param {Event|{type:string}} event The original event to wrap.
353 */
354function Event(eventTarget, event) {
355 privateData.set(this, {
356 eventTarget,
357 event,
358 eventPhase: 2,
359 currentTarget: eventTarget,
360 canceled: false,
361 stopped: false,
362 immediateStopped: false,
363 passiveListener: null,
364 timeStamp: event.timeStamp || Date.now(),
365 });
366
367 // https://heycam.github.io/webidl/#Unforgeable
368 Object.defineProperty(this, "isTrusted", { value: false, enumerable: true });
369
370 // Define accessors
371 const keys = Object.keys(event);
372 for (let i = 0; i < keys.length; ++i) {
373 const key = keys[i];
374 if (!(key in this)) {
375 Object.defineProperty(this, key, defineRedirectDescriptor(key));
376 }
377 }
378}
379
380// Should be enumerable, but class methods are not enumerable.
381Event.prototype = {
382 /**
383 * The type of this event.
384 * @type {string}
385 */
386 get type() {
387 return pd(this).event.type
388 },
389
390 /**
391 * The target of this event.
392 * @type {EventTarget}
393 */
394 get target() {
395 return pd(this).eventTarget
396 },
397
398 /**
399 * The target of this event.
400 * @type {EventTarget}
401 */
402 get currentTarget() {
403 return pd(this).currentTarget
404 },
405
406 /**
407 * @returns {EventTarget[]} The composed path of this event.
408 */
409 composedPath() {
410 const currentTarget = pd(this).currentTarget;
411 if (currentTarget == null) {
412 return []
413 }
414 return [currentTarget]
415 },
416
417 /**
418 * Constant of NONE.
419 * @type {number}
420 */
421 get NONE() {
422 return 0
423 },
424
425 /**
426 * Constant of CAPTURING_PHASE.
427 * @type {number}
428 */
429 get CAPTURING_PHASE() {
430 return 1
431 },
432
433 /**
434 * Constant of AT_TARGET.
435 * @type {number}
436 */
437 get AT_TARGET() {
438 return 2
439 },
440
441 /**
442 * Constant of BUBBLING_PHASE.
443 * @type {number}
444 */
445 get BUBBLING_PHASE() {
446 return 3
447 },
448
449 /**
450 * The target of this event.
451 * @type {number}
452 */
453 get eventPhase() {
454 return pd(this).eventPhase
455 },
456
457 /**
458 * Stop event bubbling.
459 * @returns {void}
460 */
461 stopPropagation() {
462 const data = pd(this);
463
464 data.stopped = true;
465 if (typeof data.event.stopPropagation === "function") {
466 data.event.stopPropagation();
467 }
468 },
469
470 /**
471 * Stop event bubbling.
472 * @returns {void}
473 */
474 stopImmediatePropagation() {
475 const data = pd(this);
476
477 data.stopped = true;
478 data.immediateStopped = true;
479 if (typeof data.event.stopImmediatePropagation === "function") {
480 data.event.stopImmediatePropagation();
481 }
482 },
483
484 /**
485 * The flag to be bubbling.
486 * @type {boolean}
487 */
488 get bubbles() {
489 return Boolean(pd(this).event.bubbles)
490 },
491
492 /**
493 * The flag to be cancelable.
494 * @type {boolean}
495 */
496 get cancelable() {
497 return Boolean(pd(this).event.cancelable)
498 },
499
500 /**
501 * Cancel this event.
502 * @returns {void}
503 */
504 preventDefault() {
505 setCancelFlag(pd(this));
506 },
507
508 /**
509 * The flag to indicate cancellation state.
510 * @type {boolean}
511 */
512 get defaultPrevented() {
513 return pd(this).canceled
514 },
515
516 /**
517 * The flag to be composed.
518 * @type {boolean}
519 */
520 get composed() {
521 return Boolean(pd(this).event.composed)
522 },
523
524 /**
525 * The unix time of this event.
526 * @type {number}
527 */
528 get timeStamp() {
529 return pd(this).timeStamp
530 },
531
532 /**
533 * The target of this event.
534 * @type {EventTarget}
535 * @deprecated
536 */
537 get srcElement() {
538 return pd(this).eventTarget
539 },
540
541 /**
542 * The flag to stop event bubbling.
543 * @type {boolean}
544 * @deprecated
545 */
546 get cancelBubble() {
547 return pd(this).stopped
548 },
549 set cancelBubble(value) {
550 if (!value) {
551 return
552 }
553 const data = pd(this);
554
555 data.stopped = true;
556 if (typeof data.event.cancelBubble === "boolean") {
557 data.event.cancelBubble = true;
558 }
559 },
560
561 /**
562 * The flag to indicate cancellation state.
563 * @type {boolean}
564 * @deprecated
565 */
566 get returnValue() {
567 return !pd(this).canceled
568 },
569 set returnValue(value) {
570 if (!value) {
571 setCancelFlag(pd(this));
572 }
573 },
574
575 /**
576 * Initialize this event object. But do nothing under event dispatching.
577 * @param {string} type The event type.
578 * @param {boolean} [bubbles=false] The flag to be possible to bubble up.
579 * @param {boolean} [cancelable=false] The flag to be possible to cancel.
580 * @deprecated
581 */
582 initEvent() {
583 // Do nothing.
584 },
585};
586
587// `constructor` is not enumerable.
588Object.defineProperty(Event.prototype, "constructor", {
589 value: Event,
590 configurable: true,
591 writable: true,
592});
593
594// Ensure `event instanceof window.Event` is `true`.
595if (typeof window !== "undefined" && typeof window.Event !== "undefined") {
596 Object.setPrototypeOf(Event.prototype, window.Event.prototype);
597
598 // Make association for wrappers.
599 wrappers.set(window.Event.prototype, Event);
600}
601
602/**
603 * Get the property descriptor to redirect a given property.
604 * @param {string} key Property name to define property descriptor.
605 * @returns {PropertyDescriptor} The property descriptor to redirect the property.
606 * @private
607 */
608function defineRedirectDescriptor(key) {
609 return {
610 get() {
611 return pd(this).event[key]
612 },
613 set(value) {
614 pd(this).event[key] = value;
615 },
616 configurable: true,
617 enumerable: true,
618 }
619}
620
621/**
622 * Get the property descriptor to call a given method property.
623 * @param {string} key Property name to define property descriptor.
624 * @returns {PropertyDescriptor} The property descriptor to call the method property.
625 * @private
626 */
627function defineCallDescriptor(key) {
628 return {
629 value() {
630 const event = pd(this).event;
631 return event[key].apply(event, arguments)
632 },
633 configurable: true,
634 enumerable: true,
635 }
636}
637
638/**
639 * Define new wrapper class.
640 * @param {Function} BaseEvent The base wrapper class.
641 * @param {Object} proto The prototype of the original event.
642 * @returns {Function} The defined wrapper class.
643 * @private
644 */
645function defineWrapper(BaseEvent, proto) {
646 const keys = Object.keys(proto);
647 if (keys.length === 0) {
648 return BaseEvent
649 }
650
651 /** CustomEvent */
652 function CustomEvent(eventTarget, event) {
653 BaseEvent.call(this, eventTarget, event);
654 }
655
656 CustomEvent.prototype = Object.create(BaseEvent.prototype, {
657 constructor: { value: CustomEvent, configurable: true, writable: true },
658 });
659
660 // Define accessors.
661 for (let i = 0; i < keys.length; ++i) {
662 const key = keys[i];
663 if (!(key in BaseEvent.prototype)) {
664 const descriptor = Object.getOwnPropertyDescriptor(proto, key);
665 const isFunc = typeof descriptor.value === "function";
666 Object.defineProperty(
667 CustomEvent.prototype,
668 key,
669 isFunc
670 ? defineCallDescriptor(key)
671 : defineRedirectDescriptor(key)
672 );
673 }
674 }
675
676 return CustomEvent
677}
678
679/**
680 * Get the wrapper class of a given prototype.
681 * @param {Object} proto The prototype of the original event to get its wrapper.
682 * @returns {Function} The wrapper class.
683 * @private
684 */
685function getWrapper(proto) {
686 if (proto == null || proto === Object.prototype) {
687 return Event
688 }
689
690 let wrapper = wrappers.get(proto);
691 if (wrapper == null) {
692 wrapper = defineWrapper(getWrapper(Object.getPrototypeOf(proto)), proto);
693 wrappers.set(proto, wrapper);
694 }
695 return wrapper
696}
697
698/**
699 * Wrap a given event to management a dispatching.
700 * @param {EventTarget} eventTarget The event target of this dispatching.
701 * @param {Object} event The event to wrap.
702 * @returns {Event} The wrapper instance.
703 * @private
704 */
705function wrapEvent(eventTarget, event) {
706 const Wrapper = getWrapper(Object.getPrototypeOf(event));
707 return new Wrapper(eventTarget, event)
708}
709
710/**
711 * Get the immediateStopped flag of a given event.
712 * @param {Event} event The event to get.
713 * @returns {boolean} The flag to stop propagation immediately.
714 * @private
715 */
716function isStopped(event) {
717 return pd(event).immediateStopped
718}
719
720/**
721 * Set the current event phase of a given event.
722 * @param {Event} event The event to set current target.
723 * @param {number} eventPhase New event phase.
724 * @returns {void}
725 * @private
726 */
727function setEventPhase(event, eventPhase) {
728 pd(event).eventPhase = eventPhase;
729}
730
731/**
732 * Set the current target of a given event.
733 * @param {Event} event The event to set current target.
734 * @param {EventTarget|null} currentTarget New current target.
735 * @returns {void}
736 * @private
737 */
738function setCurrentTarget(event, currentTarget) {
739 pd(event).currentTarget = currentTarget;
740}
741
742/**
743 * Set a passive listener of a given event.
744 * @param {Event} event The event to set current target.
745 * @param {Function|null} passiveListener New passive listener.
746 * @returns {void}
747 * @private
748 */
749function setPassiveListener(event, passiveListener) {
750 pd(event).passiveListener = passiveListener;
751}
752
753/**
754 * @typedef {object} ListenerNode
755 * @property {Function} listener
756 * @property {1|2|3} listenerType
757 * @property {boolean} passive
758 * @property {boolean} once
759 * @property {ListenerNode|null} next
760 * @private
761 */
762
763/**
764 * @type {WeakMap<object, Map<string, ListenerNode>>}
765 * @private
766 */
767const listenersMap = new WeakMap();
768
769// Listener types
770const CAPTURE = 1;
771const BUBBLE = 2;
772const ATTRIBUTE = 3;
773
774/**
775 * Check whether a given value is an object or not.
776 * @param {any} x The value to check.
777 * @returns {boolean} `true` if the value is an object.
778 */
779function isObject(x) {
780 return x !== null && typeof x === "object" //eslint-disable-line no-restricted-syntax
781}
782
783/**
784 * Get listeners.
785 * @param {EventTarget} eventTarget The event target to get.
786 * @returns {Map<string, ListenerNode>} The listeners.
787 * @private
788 */
789function getListeners(eventTarget) {
790 const listeners = listenersMap.get(eventTarget);
791 if (listeners == null) {
792 throw new TypeError(
793 "'this' is expected an EventTarget object, but got another value."
794 )
795 }
796 return listeners
797}
798
799/**
800 * Get the property descriptor for the event attribute of a given event.
801 * @param {string} eventName The event name to get property descriptor.
802 * @returns {PropertyDescriptor} The property descriptor.
803 * @private
804 */
805function defineEventAttributeDescriptor(eventName) {
806 return {
807 get() {
808 const listeners = getListeners(this);
809 let node = listeners.get(eventName);
810 while (node != null) {
811 if (node.listenerType === ATTRIBUTE) {
812 return node.listener
813 }
814 node = node.next;
815 }
816 return null
817 },
818
819 set(listener) {
820 if (typeof listener !== "function" && !isObject(listener)) {
821 listener = null; // eslint-disable-line no-param-reassign
822 }
823 const listeners = getListeners(this);
824
825 // Traverse to the tail while removing old value.
826 let prev = null;
827 let node = listeners.get(eventName);
828 while (node != null) {
829 if (node.listenerType === ATTRIBUTE) {
830 // Remove old value.
831 if (prev !== null) {
832 prev.next = node.next;
833 } else if (node.next !== null) {
834 listeners.set(eventName, node.next);
835 } else {
836 listeners.delete(eventName);
837 }
838 } else {
839 prev = node;
840 }
841
842 node = node.next;
843 }
844
845 // Add new value.
846 if (listener !== null) {
847 const newNode = {
848 listener,
849 listenerType: ATTRIBUTE,
850 passive: false,
851 once: false,
852 next: null,
853 };
854 if (prev === null) {
855 listeners.set(eventName, newNode);
856 } else {
857 prev.next = newNode;
858 }
859 }
860 },
861 configurable: true,
862 enumerable: true,
863 }
864}
865
866/**
867 * Define an event attribute (e.g. `eventTarget.onclick`).
868 * @param {Object} eventTargetPrototype The event target prototype to define an event attrbite.
869 * @param {string} eventName The event name to define.
870 * @returns {void}
871 */
872function defineEventAttribute(eventTargetPrototype, eventName) {
873 Object.defineProperty(
874 eventTargetPrototype,
875 `on${eventName}`,
876 defineEventAttributeDescriptor(eventName)
877 );
878}
879
880/**
881 * Define a custom EventTarget with event attributes.
882 * @param {string[]} eventNames Event names for event attributes.
883 * @returns {EventTarget} The custom EventTarget.
884 * @private
885 */
886function defineCustomEventTarget(eventNames) {
887 /** CustomEventTarget */
888 function CustomEventTarget() {
889 EventTarget.call(this);
890 }
891
892 CustomEventTarget.prototype = Object.create(EventTarget.prototype, {
893 constructor: {
894 value: CustomEventTarget,
895 configurable: true,
896 writable: true,
897 },
898 });
899
900 for (let i = 0; i < eventNames.length; ++i) {
901 defineEventAttribute(CustomEventTarget.prototype, eventNames[i]);
902 }
903
904 return CustomEventTarget
905}
906
907/**
908 * EventTarget.
909 *
910 * - This is constructor if no arguments.
911 * - This is a function which returns a CustomEventTarget constructor if there are arguments.
912 *
913 * For example:
914 *
915 * class A extends EventTarget {}
916 * class B extends EventTarget("message") {}
917 * class C extends EventTarget("message", "error") {}
918 * class D extends EventTarget(["message", "error"]) {}
919 */
920function EventTarget() {
921 /*eslint-disable consistent-return */
922 if (this instanceof EventTarget) {
923 listenersMap.set(this, new Map());
924 return
925 }
926 if (arguments.length === 1 && Array.isArray(arguments[0])) {
927 return defineCustomEventTarget(arguments[0])
928 }
929 if (arguments.length > 0) {
930 const types = new Array(arguments.length);
931 for (let i = 0; i < arguments.length; ++i) {
932 types[i] = arguments[i];
933 }
934 return defineCustomEventTarget(types)
935 }
936 throw new TypeError("Cannot call a class as a function")
937 /*eslint-enable consistent-return */
938}
939
940// Should be enumerable, but class methods are not enumerable.
941EventTarget.prototype = {
942 /**
943 * Add a given listener to this event target.
944 * @param {string} eventName The event name to add.
945 * @param {Function} listener The listener to add.
946 * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.
947 * @returns {void}
948 */
949 addEventListener(eventName, listener, options) {
950 if (listener == null) {
951 return
952 }
953 if (typeof listener !== "function" && !isObject(listener)) {
954 throw new TypeError("'listener' should be a function or an object.")
955 }
956
957 const listeners = getListeners(this);
958 const optionsIsObj = isObject(options);
959 const capture = optionsIsObj
960 ? Boolean(options.capture)
961 : Boolean(options);
962 const listenerType = capture ? CAPTURE : BUBBLE;
963 const newNode = {
964 listener,
965 listenerType,
966 passive: optionsIsObj && Boolean(options.passive),
967 once: optionsIsObj && Boolean(options.once),
968 next: null,
969 };
970
971 // Set it as the first node if the first node is null.
972 let node = listeners.get(eventName);
973 if (node === undefined) {
974 listeners.set(eventName, newNode);
975 return
976 }
977
978 // Traverse to the tail while checking duplication..
979 let prev = null;
980 while (node != null) {
981 if (
982 node.listener === listener &&
983 node.listenerType === listenerType
984 ) {
985 // Should ignore duplication.
986 return
987 }
988 prev = node;
989 node = node.next;
990 }
991
992 // Add it.
993 prev.next = newNode;
994 },
995
996 /**
997 * Remove a given listener from this event target.
998 * @param {string} eventName The event name to remove.
999 * @param {Function} listener The listener to remove.
1000 * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.
1001 * @returns {void}
1002 */
1003 removeEventListener(eventName, listener, options) {
1004 if (listener == null) {
1005 return
1006 }
1007
1008 const listeners = getListeners(this);
1009 const capture = isObject(options)
1010 ? Boolean(options.capture)
1011 : Boolean(options);
1012 const listenerType = capture ? CAPTURE : BUBBLE;
1013
1014 let prev = null;
1015 let node = listeners.get(eventName);
1016 while (node != null) {
1017 if (
1018 node.listener === listener &&
1019 node.listenerType === listenerType
1020 ) {
1021 if (prev !== null) {
1022 prev.next = node.next;
1023 } else if (node.next !== null) {
1024 listeners.set(eventName, node.next);
1025 } else {
1026 listeners.delete(eventName);
1027 }
1028 return
1029 }
1030
1031 prev = node;
1032 node = node.next;
1033 }
1034 },
1035
1036 /**
1037 * Dispatch a given event.
1038 * @param {Event|{type:string}} event The event to dispatch.
1039 * @returns {boolean} `false` if canceled.
1040 */
1041 dispatchEvent(event) {
1042 if (event == null || typeof event.type !== "string") {
1043 throw new TypeError('"event.type" should be a string.')
1044 }
1045
1046 // If listeners aren't registered, terminate.
1047 const listeners = getListeners(this);
1048 const eventName = event.type;
1049 let node = listeners.get(eventName);
1050 if (node == null) {
1051 return true
1052 }
1053
1054 // Since we cannot rewrite several properties, so wrap object.
1055 const wrappedEvent = wrapEvent(this, event);
1056
1057 // This doesn't process capturing phase and bubbling phase.
1058 // This isn't participating in a tree.
1059 let prev = null;
1060 while (node != null) {
1061 // Remove this listener if it's once
1062 if (node.once) {
1063 if (prev !== null) {
1064 prev.next = node.next;
1065 } else if (node.next !== null) {
1066 listeners.set(eventName, node.next);
1067 } else {
1068 listeners.delete(eventName);
1069 }
1070 } else {
1071 prev = node;
1072 }
1073
1074 // Call this listener
1075 setPassiveListener(
1076 wrappedEvent,
1077 node.passive ? node.listener : null
1078 );
1079 if (typeof node.listener === "function") {
1080 try {
1081 node.listener.call(this, wrappedEvent);
1082 } catch (err) {
1083 if (
1084 typeof console !== "undefined" &&
1085 typeof console.error === "function"
1086 ) {
1087 console.error(err);
1088 }
1089 }
1090 } else if (
1091 node.listenerType !== ATTRIBUTE &&
1092 typeof node.listener.handleEvent === "function"
1093 ) {
1094 node.listener.handleEvent(wrappedEvent);
1095 }
1096
1097 // Break if `event.stopImmediatePropagation` was called.
1098 if (isStopped(wrappedEvent)) {
1099 break
1100 }
1101
1102 node = node.next;
1103 }
1104 setPassiveListener(wrappedEvent, null);
1105 setEventPhase(wrappedEvent, 0);
1106 setCurrentTarget(wrappedEvent, null);
1107
1108 return !wrappedEvent.defaultPrevented
1109 },
1110};
1111
1112// `constructor` is not enumerable.
1113Object.defineProperty(EventTarget.prototype, "constructor", {
1114 value: EventTarget,
1115 configurable: true,
1116 writable: true,
1117});
1118
1119// Ensure `eventTarget instanceof window.EventTarget` is `true`.
1120if (
1121 typeof window !== "undefined" &&
1122 typeof window.EventTarget !== "undefined"
1123) {
1124 Object.setPrototypeOf(EventTarget.prototype, window.EventTarget.prototype);
1125}
1126
1127var WS = /** @class */ (function (_super) {
1128 __extends$1(WS, _super);
1129 function WS(url, protocol) {
1130 var _this = _super.call(this) || this;
1131 _this._readyState = WS.CLOSED;
1132 if (!url) {
1133 throw new TypeError("Failed to construct 'WebSocket': url required");
1134 }
1135 _this._url = url;
1136 _this._protocol = protocol;
1137 return _this;
1138 }
1139 Object.defineProperty(WS.prototype, "url", {
1140 get: function () {
1141 return this._url;
1142 },
1143 enumerable: false,
1144 configurable: true
1145 });
1146 Object.defineProperty(WS.prototype, "protocol", {
1147 get: function () {
1148 return this._protocol;
1149 },
1150 enumerable: false,
1151 configurable: true
1152 });
1153 Object.defineProperty(WS.prototype, "readyState", {
1154 get: function () {
1155 return this._readyState;
1156 },
1157 enumerable: false,
1158 configurable: true
1159 });
1160 WS.CONNECTING = 0;
1161 WS.OPEN = 1;
1162 WS.CLOSING = 2;
1163 WS.CLOSED = 3;
1164 return WS;
1165}(EventTarget("open", "error", "message", "close")));
1166var WechatWS = /** @class */ (function (_super) {
1167 __extends$1(WechatWS, _super);
1168 function WechatWS(url, protocol) {
1169 var _this = _super.call(this, url, protocol) || this;
1170 if (protocol &&
1171 !(wx.canIUse && wx.canIUse("connectSocket.object.protocols"))) {
1172 throw new Error("subprotocol not supported in weapp");
1173 }
1174 _this._readyState = WS.CONNECTING;
1175 var errorHandler = function (event) {
1176 _this._readyState = WS.CLOSED;
1177 _this.dispatchEvent({
1178 type: "error",
1179 message: event.errMsg,
1180 });
1181 };
1182 var socketTask = wx.connectSocket({
1183 url: url,
1184 protocols: _this._protocol === undefined || Array.isArray(_this._protocol)
1185 ? _this._protocol
1186 : [_this._protocol],
1187 fail: function (error) { return setTimeout(function () { return errorHandler(error); }, 0); },
1188 });
1189 _this._socketTask = socketTask;
1190 socketTask.onOpen(function () {
1191 _this._readyState = WS.OPEN;
1192 _this.dispatchEvent({
1193 type: "open",
1194 });
1195 });
1196 socketTask.onError(errorHandler);
1197 socketTask.onMessage(function (event) {
1198 var data = event.data;
1199 _this.dispatchEvent({
1200 data: data,
1201 type: "message",
1202 });
1203 });
1204 socketTask.onClose(function (event) {
1205 _this._readyState = WS.CLOSED;
1206 var code = event.code, reason = event.reason;
1207 _this.dispatchEvent({
1208 code: code,
1209 reason: reason,
1210 type: "close",
1211 });
1212 });
1213 return _this;
1214 }
1215 WechatWS.prototype.close = function () {
1216 if (this.readyState === WS.CLOSED)
1217 return;
1218 if (this.readyState === WS.CONNECTING) {
1219 console.warn("close WebSocket which is connecting might not work");
1220 }
1221 this._socketTask.close({});
1222 };
1223 WechatWS.prototype.send = function (data) {
1224 if (this.readyState !== WS.OPEN) {
1225 throw new Error("INVALID_STATE_ERR");
1226 }
1227 if (!(typeof data === "string" || data instanceof ArrayBuffer)) {
1228 throw new TypeError("only String/ArrayBuffer supported");
1229 }
1230 this._socketTask.send({
1231 data: data,
1232 });
1233 };
1234 return WechatWS;
1235}(WS));
1236var WebSocket = WechatWS;
1237
1238var platformInfo$1 = {
1239 name: "Weapp",
1240};
1241
1242var WebSocket_1 = lib.WebSocket = WebSocket;
1243var getAuthInfo_1 = lib.getAuthInfo = getAuthInfo$1;
1244lib.platformInfo = platformInfo$1;
1245var request_1 = lib.request = request;
1246var storage_1 = lib.storage = storage;
1247var upload_1 = lib.upload = upload;
1248
1249/******************************************************************************
1250Copyright (c) Microsoft Corporation.
1251
1252Permission to use, copy, modify, and/or distribute this software for any
1253purpose with or without fee is hereby granted.
1254
1255THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
1256REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1257AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
1258INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1259LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1260OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1261PERFORMANCE OF THIS SOFTWARE.
1262***************************************************************************** */
1263
1264function __awaiter(thisArg, _arguments, P, generator) {
1265 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1266 return new (P || (P = Promise))(function (resolve, reject) {
1267 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1268 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1269 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1270 step((generator = generator.apply(thisArg, _arguments || [])).next());
1271 });
1272}
1273
1274function __generator(thisArg, body) {
1275 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
1276 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
1277 function verb(n) { return function (v) { return step([n, v]); }; }
1278 function step(op) {
1279 if (f) throw new TypeError("Generator is already executing.");
1280 while (_) try {
1281 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
1282 if (y = 0, t) op = [op[0] & 2, t.value];
1283 switch (op[0]) {
1284 case 0: case 1: t = op; break;
1285 case 4: _.label++; return { value: op[1], done: false };
1286 case 5: _.label++; y = op[1]; op = [0]; continue;
1287 case 7: op = _.ops.pop(); _.trys.pop(); continue;
1288 default:
1289 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
1290 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
1291 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
1292 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
1293 if (t[2]) _.ops.pop();
1294 _.trys.pop(); continue;
1295 }
1296 op = body.call(thisArg, _);
1297 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
1298 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
1299 }
1300}
1301
1302var PROVIDER = "lc_qqapp";
1303var PLATFORM = "qq";
1304var getAuthInfo = function (_a) {
1305 var _b = _a === void 0 ? {} : _a, _c = _b.platform, platform = _c === void 0 ? PLATFORM : _c, _d = _b.preferUnionId, preferUnionId = _d === void 0 ? false : _d, _e = _b.asMainAccount, asMainAccount = _e === void 0 ? false : _e;
1306 return __awaiter(this, void 0, void 0, function () {
1307 var authInfo;
1308 return __generator(this, function (_f) {
1309 switch (_f.label) {
1310 case 0: return [4 /*yield*/, getAuthInfo_1({
1311 platform: platform,
1312 preferUnionId: preferUnionId,
1313 asMainAccount: asMainAccount,
1314 })];
1315 case 1:
1316 authInfo = _f.sent();
1317 authInfo.provider = PROVIDER;
1318 return [2 /*return*/, authInfo];
1319 }
1320 });
1321 });
1322};
1323
1324var platformInfo = {
1325 name: "QQApp",
1326};
1327
1328exports.WebSocket = WebSocket_1;
1329exports.getAuthInfo = getAuthInfo;
1330exports.platformInfo = platformInfo;
1331exports.request = request_1;
1332exports.storage = storage_1;
1333exports.upload = upload_1;
1334//# sourceMappingURL=index.js.map