@tarojs/shared 3.5.0-alpha.8 → 3.5.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,29 +1,3 @@
1
- function isString(o) {
2
- return typeof o === 'string';
3
- }
4
- function isUndefined(o) {
5
- return typeof o === 'undefined';
6
- }
7
- function isNull(o) {
8
- return o === null;
9
- }
10
- function isObject(o) {
11
- return o !== null && typeof o === 'object';
12
- }
13
- function isBoolean(o) {
14
- return o === true || o === false;
15
- }
16
- function isFunction(o) {
17
- return typeof o === 'function';
18
- }
19
- function isNumber(o) {
20
- return typeof o === 'number';
21
- }
22
- function isBooleanStringLiteral(o) {
23
- return o === 'true' || o === 'false';
24
- }
25
- const isArray = Array.isArray;
26
-
27
1
  const DEFAULT_EMPTY_ARRAY = '[]';
28
2
  const NO_DEFAULT_VALUE = '';
29
3
  const DEFAULT_TRUE = 'true';
@@ -360,10 +334,268 @@ const nestElements = new Map([
360
334
  ['swiper-item', 4]
361
335
  ]);
362
336
 
337
+ class Events {
338
+ constructor(opts) {
339
+ var _a;
340
+ this.callbacks = (_a = opts === null || opts === void 0 ? void 0 : opts.callbacks) !== null && _a !== void 0 ? _a : {};
341
+ }
342
+ on(eventName, callback, context) {
343
+ let event, node, tail, list;
344
+ if (!callback) {
345
+ return this;
346
+ }
347
+ eventName = eventName.split(Events.eventSplitter);
348
+ this.callbacks || (this.callbacks = {});
349
+ const calls = this.callbacks;
350
+ while ((event = eventName.shift())) {
351
+ list = calls[event];
352
+ node = list ? list.tail : {};
353
+ node.next = tail = {};
354
+ node.context = context;
355
+ node.callback = callback;
356
+ calls[event] = {
357
+ tail,
358
+ next: list ? list.next : node
359
+ };
360
+ }
361
+ return this;
362
+ }
363
+ once(events, callback, context) {
364
+ const wrapper = (...args) => {
365
+ callback.apply(this, args);
366
+ this.off(events, wrapper, context);
367
+ };
368
+ this.on(events, wrapper, context);
369
+ return this;
370
+ }
371
+ off(events, callback, context) {
372
+ let event, calls, node, tail, cb, ctx;
373
+ if (!(calls = this.callbacks)) {
374
+ return this;
375
+ }
376
+ if (!(events || callback || context)) {
377
+ delete this.callbacks;
378
+ return this;
379
+ }
380
+ events = events ? events.split(Events.eventSplitter) : Object.keys(calls);
381
+ while ((event = events.shift())) {
382
+ node = calls[event];
383
+ delete calls[event];
384
+ if (!node || !(callback || context)) {
385
+ continue;
386
+ }
387
+ tail = node.tail;
388
+ while ((node = node.next) !== tail) {
389
+ cb = node.callback;
390
+ ctx = node.context;
391
+ if ((callback && cb !== callback) || (context && ctx !== context)) {
392
+ this.on(event, cb, ctx);
393
+ }
394
+ }
395
+ }
396
+ return this;
397
+ }
398
+ trigger(events) {
399
+ let event, node, calls, tail;
400
+ if (!(calls = this.callbacks)) {
401
+ return this;
402
+ }
403
+ events = events.split(Events.eventSplitter);
404
+ const rest = [].slice.call(arguments, 1);
405
+ while ((event = events.shift())) {
406
+ if ((node = calls[event])) {
407
+ tail = node.tail;
408
+ while ((node = node.next) !== tail) {
409
+ node.callback.apply(node.context || this, rest);
410
+ }
411
+ }
412
+ }
413
+ return this;
414
+ }
415
+ }
416
+ Events.eventSplitter = /\s+/;
417
+
418
+ function isString(o) {
419
+ return typeof o === 'string';
420
+ }
421
+ function isUndefined(o) {
422
+ return typeof o === 'undefined';
423
+ }
424
+ function isNull(o) {
425
+ return o === null;
426
+ }
427
+ function isObject(o) {
428
+ return o !== null && typeof o === 'object';
429
+ }
430
+ function isBoolean(o) {
431
+ return o === true || o === false;
432
+ }
433
+ function isFunction(o) {
434
+ return typeof o === 'function';
435
+ }
436
+ function isNumber(o) {
437
+ return typeof o === 'number';
438
+ }
439
+ function isBooleanStringLiteral(o) {
440
+ return o === 'true' || o === 'false';
441
+ }
442
+ const isArray = Array.isArray;
443
+
444
+ var HOOK_TYPE;
445
+ (function (HOOK_TYPE) {
446
+ HOOK_TYPE[HOOK_TYPE["SINGLE"] = 0] = "SINGLE";
447
+ HOOK_TYPE[HOOK_TYPE["MULTI"] = 1] = "MULTI";
448
+ HOOK_TYPE[HOOK_TYPE["WATERFALL"] = 2] = "WATERFALL";
449
+ })(HOOK_TYPE || (HOOK_TYPE = {}));
450
+ const defaultMiniLifecycle = {
451
+ app: [
452
+ 'onLaunch',
453
+ 'onShow',
454
+ 'onHide'
455
+ ],
456
+ page: [
457
+ 'onLoad',
458
+ 'onUnload',
459
+ 'onReady',
460
+ 'onShow',
461
+ 'onHide',
462
+ [
463
+ 'onPullDownRefresh',
464
+ 'onReachBottom',
465
+ 'onPageScroll',
466
+ 'onResize',
467
+ 'onTabItemTap',
468
+ 'onTitleClick',
469
+ 'onOptionMenuClick',
470
+ 'onPopMenuClick',
471
+ 'onPullIntercept',
472
+ 'onAddToFavorites'
473
+ ]
474
+ ]
475
+ };
476
+ function TaroHook(type, initial) {
477
+ return {
478
+ type,
479
+ initial: initial || null
480
+ };
481
+ }
482
+ class TaroHooks extends Events {
483
+ constructor(hooks, opts) {
484
+ super(opts);
485
+ this.hooks = hooks;
486
+ for (const hookName in hooks) {
487
+ const { initial } = hooks[hookName];
488
+ if (isFunction(initial)) {
489
+ this.on(hookName, initial);
490
+ }
491
+ }
492
+ }
493
+ tapOneOrMany(hookName, callback) {
494
+ const list = isFunction(callback) ? [callback] : callback;
495
+ list.forEach(cb => this.on(hookName, cb));
496
+ }
497
+ tap(hookName, callback) {
498
+ const hooks = this.hooks;
499
+ const { type, initial } = hooks[hookName];
500
+ if (type === HOOK_TYPE.SINGLE) {
501
+ this.off(hookName);
502
+ this.on(hookName, isFunction(callback) ? callback : callback[callback.length - 1]);
503
+ }
504
+ else {
505
+ initial && this.off(hookName, initial);
506
+ this.tapOneOrMany(hookName, callback);
507
+ }
508
+ }
509
+ call(hookName, ...rest) {
510
+ var _a;
511
+ const hook = this.hooks[hookName];
512
+ if (!hook)
513
+ return;
514
+ const { type } = hook;
515
+ const calls = this.callbacks;
516
+ if (!calls)
517
+ return;
518
+ const list = calls[hookName];
519
+ if (list) {
520
+ const tail = list.tail;
521
+ let node = list.next;
522
+ let args = rest;
523
+ let res;
524
+ while (node !== tail) {
525
+ res = (_a = node.callback) === null || _a === void 0 ? void 0 : _a.apply(node.context || this, args);
526
+ if (type === HOOK_TYPE.WATERFALL) {
527
+ const params = [res];
528
+ args = params;
529
+ }
530
+ node = node.next;
531
+ }
532
+ return res;
533
+ }
534
+ }
535
+ isExist(hookName) {
536
+ var _a;
537
+ return Boolean((_a = this.callbacks) === null || _a === void 0 ? void 0 : _a[hookName]);
538
+ }
539
+ }
540
+ const hooks = new TaroHooks({
541
+ getMiniLifecycle: TaroHook(HOOK_TYPE.SINGLE, defaultConfig => defaultConfig),
542
+ getMiniLifecycleImpl: TaroHook(HOOK_TYPE.SINGLE, function () {
543
+ return this.call('getMiniLifecycle', defaultMiniLifecycle);
544
+ }),
545
+ getLifecycle: TaroHook(HOOK_TYPE.SINGLE, (instance, lifecycle) => instance[lifecycle]),
546
+ getPathIndex: TaroHook(HOOK_TYPE.SINGLE, indexOfNode => `[${indexOfNode}]`),
547
+ getEventCenter: TaroHook(HOOK_TYPE.SINGLE, Events => new Events()),
548
+ isBubbleEvents: TaroHook(HOOK_TYPE.SINGLE, eventName => {
549
+ /**
550
+ * 支持冒泡的事件, 除 支付宝小程序外,其余的可冒泡事件都和微信保持一致
551
+ * 详见 见 https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/event.html
552
+ */
553
+ const BUBBLE_EVENTS = new Set([
554
+ 'touchstart',
555
+ 'touchmove',
556
+ 'touchcancel',
557
+ 'touchend',
558
+ 'touchforcechange',
559
+ 'tap',
560
+ 'longpress',
561
+ 'longtap',
562
+ 'transitionend',
563
+ 'animationstart',
564
+ 'animationiteration',
565
+ 'animationend'
566
+ ]);
567
+ return BUBBLE_EVENTS.has(eventName);
568
+ }),
569
+ getSpecialNodes: TaroHook(HOOK_TYPE.SINGLE, () => ['view', 'text', 'image']),
570
+ onRemoveAttribute: TaroHook(HOOK_TYPE.SINGLE),
571
+ batchedEventUpdates: TaroHook(HOOK_TYPE.SINGLE),
572
+ mergePageInstance: TaroHook(HOOK_TYPE.SINGLE),
573
+ modifyPageObject: TaroHook(HOOK_TYPE.SINGLE),
574
+ createPullDownComponent: TaroHook(HOOK_TYPE.SINGLE),
575
+ getDOMNode: TaroHook(HOOK_TYPE.SINGLE),
576
+ modifyHydrateData: TaroHook(HOOK_TYPE.SINGLE),
577
+ modifySetAttrPayload: TaroHook(HOOK_TYPE.SINGLE),
578
+ modifyRmAttrPayload: TaroHook(HOOK_TYPE.SINGLE),
579
+ onAddEvent: TaroHook(HOOK_TYPE.SINGLE),
580
+ modifyMpEvent: TaroHook(HOOK_TYPE.MULTI),
581
+ modifyMpEventImpl: TaroHook(HOOK_TYPE.SINGLE, function (e) {
582
+ try {
583
+ // 有些小程序的事件对象的某些属性只读
584
+ this.call('modifyMpEvent', e);
585
+ }
586
+ catch (error) {
587
+ console.warn('[Taro modifyMpEvent hook Error]: ', error);
588
+ }
589
+ }),
590
+ modifyTaroEvent: TaroHook(HOOK_TYPE.MULTI),
591
+ modifyDispatchEvent: TaroHook(HOOK_TYPE.MULTI),
592
+ initNativeApi: TaroHook(HOOK_TYPE.MULTI),
593
+ patchElement: TaroHook(HOOK_TYPE.MULTI)
594
+ });
595
+
363
596
  const EMPTY_OBJ = {};
364
597
  const EMPTY_ARR = [];
365
598
  const noop = (..._) => { };
366
- const defaultReconciler = Object.create(null);
367
599
  /**
368
600
  * box creates a boxed value.
369
601
  *
@@ -487,15 +719,11 @@ function mergeInternalComponents(components) {
487
719
  }
488
720
  });
489
721
  }
490
- function mergeReconciler(hostConfig) {
491
- Object.keys(hostConfig).forEach(key => {
492
- const value = hostConfig[key];
493
- const raw = defaultReconciler[key];
494
- defaultReconciler[key] = !raw
495
- ? value
496
- : (isArray(raw)
497
- ? raw.concat(value)
498
- : [raw, value]);
722
+ function mergeReconciler(hostConfig, hooksForTest) {
723
+ const obj = hooksForTest || hooks;
724
+ const keys = Object.keys(hostConfig);
725
+ keys.forEach(key => {
726
+ obj.tap(key, hostConfig[key]);
499
727
  });
500
728
  }
501
729
  function unsupport(api) {
@@ -778,8 +1006,9 @@ function processApis(taro, global, config = {}) {
778
1006
  }
779
1007
  });
780
1008
  // 给 promise 对象挂载属性
781
- if (key === 'uploadFile' || key === 'downloadFile') {
782
- task && ['abort', 'offHeadersReceived', 'offProgressUpdate', 'onHeadersReceived', 'onProgressUpdate'].forEach(method => {
1009
+ if (['request', 'uploadFile', 'downloadFile'].includes(key)) {
1010
+ const taskMethods = ['abort', 'onHeadersReceived', 'offHeadersReceived', 'onProgressUpdate', 'offProgressUpdate', 'onChunkReceived', 'offChunkReceived'];
1011
+ task && taskMethods.forEach(method => {
783
1012
  if (method in task) {
784
1013
  p[method] = task[method].bind(task);
785
1014
  }
@@ -853,5 +1082,5 @@ function equipCommonApis(taro, global, apis = {}) {
853
1082
  taro.miniGlobal = taro.options.miniGlobal = global;
854
1083
  }
855
1084
 
856
- export { EMPTY_ARR, EMPTY_OBJ, animation, box, cacheDataGet, cacheDataHas, cacheDataSet, capitalize, controlledComponent, defaultReconciler, ensure, focusComponents, getUniqueKey, hasOwn, indent, internalComponents, isArray, isBoolean, isBooleanStringLiteral, isFunction, isNull, isNumber, isObject, isString, isUndefined, mergeInternalComponents, mergeReconciler, nestElements, noop, processApis, queryToJson, setUniqueKeyToRoute, singleQuote, toCamelCase, toDashed, toKebabCase, touchEvents, unbox, unsupport, voidElements, warn };
1085
+ export { EMPTY_ARR, EMPTY_OBJ, Events, HOOK_TYPE, TaroHook, TaroHooks, animation, box, cacheDataGet, cacheDataHas, cacheDataSet, capitalize, controlledComponent, ensure, focusComponents, getUniqueKey, hasOwn, hooks, indent, internalComponents, isArray, isBoolean, isBooleanStringLiteral, isFunction, isNull, isNumber, isObject, isString, isUndefined, mergeInternalComponents, mergeReconciler, nestElements, noop, processApis, queryToJson, setUniqueKeyToRoute, singleQuote, toCamelCase, toDashed, toKebabCase, touchEvents, unbox, unsupport, voidElements, warn };
857
1086
  //# sourceMappingURL=shared.esm.js.map