@tarojs/shared 3.5.0-alpha.9 → 3.5.0-beta.2

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.
@@ -0,0 +1,27 @@
1
+ declare type Callback1<T1> = (arg1: T1) => any;
2
+ declare type Callback2<T1, T2> = (arg1: T1, arg2: T2) => any;
3
+ declare type Callback3<T1, T2, T3> = (arg1: T1, arg2: T2, arg3: T3) => any;
4
+ declare type Callback4<T1, T2, T3, T4> = (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => any;
5
+ declare type Callback5<T1, T2, T3, T4, T5> = (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => any;
6
+ declare type Callback6Rest<T1, T2, T3, T4, T5, T6> = (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, ...rest: any[]) => any;
7
+ export declare class Events {
8
+ protected callbacks?: Record<string, unknown>;
9
+ static eventSplitter: RegExp;
10
+ constructor(opts?: any);
11
+ on<T>(event: string, callback: Callback1<T>, context?: any): this;
12
+ on<T1, T2>(event: string, callback: Callback2<T1, T2>, context?: any): this;
13
+ on<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>, context?: any): this;
14
+ on<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>, comtext: any): this;
15
+ on<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>, context?: any): this;
16
+ on<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>, context?: any): this;
17
+ once(events: any, callback: any, context: any): this;
18
+ off(events: any, callback?: any, context?: any): this;
19
+ trigger(event: string): any;
20
+ trigger<T1>(event: string, arg: T1): any;
21
+ trigger<T1, T2>(event: string, arg1: T1, arg2: T2): any;
22
+ trigger<T1, T2, T3>(event: string, arg1: T1, arg2: T2, arg3: T3): any;
23
+ trigger<T1, T2, T3, T4>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4): any;
24
+ trigger<T1, T2, T3, T4, T5>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5): any;
25
+ trigger<T1, T2, T3, T4, T5, T6>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, ...rest: any[]): any;
26
+ }
27
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
+ export * from './components';
2
+ export * from './event-emitter';
1
3
  export * from './is';
4
+ export * from './native-apis';
5
+ export * from './runtime-hooks';
2
6
  export { Shortcuts } from './shortcuts';
3
- export * from './components';
4
7
  export * from './utils';
5
- export * from './native-apis';
package/dist/index.js CHANGED
@@ -2,46 +2,6 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- function isString(o) {
6
- return typeof o === 'string';
7
- }
8
- function isUndefined(o) {
9
- return typeof o === 'undefined';
10
- }
11
- function isNull(o) {
12
- return o === null;
13
- }
14
- function isObject(o) {
15
- return o !== null && typeof o === 'object';
16
- }
17
- function isBoolean(o) {
18
- return o === true || o === false;
19
- }
20
- function isFunction(o) {
21
- return typeof o === 'function';
22
- }
23
- function isNumber(o) {
24
- return typeof o === 'number';
25
- }
26
- function isBooleanStringLiteral(o) {
27
- return o === 'true' || o === 'false';
28
- }
29
- const isArray = Array.isArray;
30
-
31
- // 字符串简写
32
- exports.Shortcuts = void 0;
33
- (function (Shortcuts) {
34
- Shortcuts["Container"] = "container";
35
- Shortcuts["Childnodes"] = "cn";
36
- Shortcuts["Text"] = "v";
37
- Shortcuts["NodeType"] = "nt";
38
- Shortcuts["NodeName"] = "nn";
39
- // Attrtibutes
40
- Shortcuts["Style"] = "st";
41
- Shortcuts["Class"] = "cl";
42
- Shortcuts["Src"] = "src";
43
- })(exports.Shortcuts || (exports.Shortcuts = {}));
44
-
45
5
  const DEFAULT_EMPTY_ARRAY = '[]';
46
6
  const NO_DEFAULT_VALUE = '';
47
7
  const DEFAULT_TRUE = 'true';
@@ -378,10 +338,268 @@ const nestElements = new Map([
378
338
  ['swiper-item', 4]
379
339
  ]);
380
340
 
341
+ class Events {
342
+ constructor(opts) {
343
+ var _a;
344
+ this.callbacks = (_a = opts === null || opts === void 0 ? void 0 : opts.callbacks) !== null && _a !== void 0 ? _a : {};
345
+ }
346
+ on(eventName, callback, context) {
347
+ let event, node, tail, list;
348
+ if (!callback) {
349
+ return this;
350
+ }
351
+ eventName = eventName.split(Events.eventSplitter);
352
+ this.callbacks || (this.callbacks = {});
353
+ const calls = this.callbacks;
354
+ while ((event = eventName.shift())) {
355
+ list = calls[event];
356
+ node = list ? list.tail : {};
357
+ node.next = tail = {};
358
+ node.context = context;
359
+ node.callback = callback;
360
+ calls[event] = {
361
+ tail,
362
+ next: list ? list.next : node
363
+ };
364
+ }
365
+ return this;
366
+ }
367
+ once(events, callback, context) {
368
+ const wrapper = (...args) => {
369
+ callback.apply(this, args);
370
+ this.off(events, wrapper, context);
371
+ };
372
+ this.on(events, wrapper, context);
373
+ return this;
374
+ }
375
+ off(events, callback, context) {
376
+ let event, calls, node, tail, cb, ctx;
377
+ if (!(calls = this.callbacks)) {
378
+ return this;
379
+ }
380
+ if (!(events || callback || context)) {
381
+ delete this.callbacks;
382
+ return this;
383
+ }
384
+ events = events ? events.split(Events.eventSplitter) : Object.keys(calls);
385
+ while ((event = events.shift())) {
386
+ node = calls[event];
387
+ delete calls[event];
388
+ if (!node || !(callback || context)) {
389
+ continue;
390
+ }
391
+ tail = node.tail;
392
+ while ((node = node.next) !== tail) {
393
+ cb = node.callback;
394
+ ctx = node.context;
395
+ if ((callback && cb !== callback) || (context && ctx !== context)) {
396
+ this.on(event, cb, ctx);
397
+ }
398
+ }
399
+ }
400
+ return this;
401
+ }
402
+ trigger(events) {
403
+ let event, node, calls, tail;
404
+ if (!(calls = this.callbacks)) {
405
+ return this;
406
+ }
407
+ events = events.split(Events.eventSplitter);
408
+ const rest = [].slice.call(arguments, 1);
409
+ while ((event = events.shift())) {
410
+ if ((node = calls[event])) {
411
+ tail = node.tail;
412
+ while ((node = node.next) !== tail) {
413
+ node.callback.apply(node.context || this, rest);
414
+ }
415
+ }
416
+ }
417
+ return this;
418
+ }
419
+ }
420
+ Events.eventSplitter = /\s+/;
421
+
422
+ function isString(o) {
423
+ return typeof o === 'string';
424
+ }
425
+ function isUndefined(o) {
426
+ return typeof o === 'undefined';
427
+ }
428
+ function isNull(o) {
429
+ return o === null;
430
+ }
431
+ function isObject(o) {
432
+ return o !== null && typeof o === 'object';
433
+ }
434
+ function isBoolean(o) {
435
+ return o === true || o === false;
436
+ }
437
+ function isFunction(o) {
438
+ return typeof o === 'function';
439
+ }
440
+ function isNumber(o) {
441
+ return typeof o === 'number';
442
+ }
443
+ function isBooleanStringLiteral(o) {
444
+ return o === 'true' || o === 'false';
445
+ }
446
+ const isArray = Array.isArray;
447
+
448
+ exports.HOOK_TYPE = void 0;
449
+ (function (HOOK_TYPE) {
450
+ HOOK_TYPE[HOOK_TYPE["SINGLE"] = 0] = "SINGLE";
451
+ HOOK_TYPE[HOOK_TYPE["MULTI"] = 1] = "MULTI";
452
+ HOOK_TYPE[HOOK_TYPE["WATERFALL"] = 2] = "WATERFALL";
453
+ })(exports.HOOK_TYPE || (exports.HOOK_TYPE = {}));
454
+ const defaultMiniLifecycle = {
455
+ app: [
456
+ 'onLaunch',
457
+ 'onShow',
458
+ 'onHide'
459
+ ],
460
+ page: [
461
+ 'onLoad',
462
+ 'onUnload',
463
+ 'onReady',
464
+ 'onShow',
465
+ 'onHide',
466
+ [
467
+ 'onPullDownRefresh',
468
+ 'onReachBottom',
469
+ 'onPageScroll',
470
+ 'onResize',
471
+ 'onTabItemTap',
472
+ 'onTitleClick',
473
+ 'onOptionMenuClick',
474
+ 'onPopMenuClick',
475
+ 'onPullIntercept',
476
+ 'onAddToFavorites'
477
+ ]
478
+ ]
479
+ };
480
+ function TaroHook(type, initial) {
481
+ return {
482
+ type,
483
+ initial: initial || null
484
+ };
485
+ }
486
+ class TaroHooks extends Events {
487
+ constructor(hooks, opts) {
488
+ super(opts);
489
+ this.hooks = hooks;
490
+ for (const hookName in hooks) {
491
+ const { initial } = hooks[hookName];
492
+ if (isFunction(initial)) {
493
+ this.on(hookName, initial);
494
+ }
495
+ }
496
+ }
497
+ tapOneOrMany(hookName, callback) {
498
+ const list = isFunction(callback) ? [callback] : callback;
499
+ list.forEach(cb => this.on(hookName, cb));
500
+ }
501
+ tap(hookName, callback) {
502
+ const hooks = this.hooks;
503
+ const { type, initial } = hooks[hookName];
504
+ if (type === exports.HOOK_TYPE.SINGLE) {
505
+ this.off(hookName);
506
+ this.on(hookName, isFunction(callback) ? callback : callback[callback.length - 1]);
507
+ }
508
+ else {
509
+ initial && this.off(hookName, initial);
510
+ this.tapOneOrMany(hookName, callback);
511
+ }
512
+ }
513
+ call(hookName, ...rest) {
514
+ var _a;
515
+ const hook = this.hooks[hookName];
516
+ if (!hook)
517
+ return;
518
+ const { type } = hook;
519
+ const calls = this.callbacks;
520
+ if (!calls)
521
+ return;
522
+ const list = calls[hookName];
523
+ if (list) {
524
+ const tail = list.tail;
525
+ let node = list.next;
526
+ let args = rest;
527
+ let res;
528
+ while (node !== tail) {
529
+ res = (_a = node.callback) === null || _a === void 0 ? void 0 : _a.apply(node.context || this, args);
530
+ if (type === exports.HOOK_TYPE.WATERFALL) {
531
+ const params = [res];
532
+ args = params;
533
+ }
534
+ node = node.next;
535
+ }
536
+ return res;
537
+ }
538
+ }
539
+ isExist(hookName) {
540
+ var _a;
541
+ return Boolean((_a = this.callbacks) === null || _a === void 0 ? void 0 : _a[hookName]);
542
+ }
543
+ }
544
+ const hooks = new TaroHooks({
545
+ getMiniLifecycle: TaroHook(exports.HOOK_TYPE.SINGLE, defaultConfig => defaultConfig),
546
+ getMiniLifecycleImpl: TaroHook(exports.HOOK_TYPE.SINGLE, function () {
547
+ return this.call('getMiniLifecycle', defaultMiniLifecycle);
548
+ }),
549
+ getLifecycle: TaroHook(exports.HOOK_TYPE.SINGLE, (instance, lifecycle) => instance[lifecycle]),
550
+ getPathIndex: TaroHook(exports.HOOK_TYPE.SINGLE, indexOfNode => `[${indexOfNode}]`),
551
+ getEventCenter: TaroHook(exports.HOOK_TYPE.SINGLE, Events => new Events()),
552
+ isBubbleEvents: TaroHook(exports.HOOK_TYPE.SINGLE, eventName => {
553
+ /**
554
+ * 支持冒泡的事件, 除 支付宝小程序外,其余的可冒泡事件都和微信保持一致
555
+ * 详见 见 https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/event.html
556
+ */
557
+ const BUBBLE_EVENTS = new Set([
558
+ 'touchstart',
559
+ 'touchmove',
560
+ 'touchcancel',
561
+ 'touchend',
562
+ 'touchforcechange',
563
+ 'tap',
564
+ 'longpress',
565
+ 'longtap',
566
+ 'transitionend',
567
+ 'animationstart',
568
+ 'animationiteration',
569
+ 'animationend'
570
+ ]);
571
+ return BUBBLE_EVENTS.has(eventName);
572
+ }),
573
+ getSpecialNodes: TaroHook(exports.HOOK_TYPE.SINGLE, () => ['view', 'text', 'image']),
574
+ onRemoveAttribute: TaroHook(exports.HOOK_TYPE.SINGLE),
575
+ batchedEventUpdates: TaroHook(exports.HOOK_TYPE.SINGLE),
576
+ mergePageInstance: TaroHook(exports.HOOK_TYPE.SINGLE),
577
+ modifyPageObject: TaroHook(exports.HOOK_TYPE.SINGLE),
578
+ createPullDownComponent: TaroHook(exports.HOOK_TYPE.SINGLE),
579
+ getDOMNode: TaroHook(exports.HOOK_TYPE.SINGLE),
580
+ modifyHydrateData: TaroHook(exports.HOOK_TYPE.SINGLE),
581
+ modifySetAttrPayload: TaroHook(exports.HOOK_TYPE.SINGLE),
582
+ modifyRmAttrPayload: TaroHook(exports.HOOK_TYPE.SINGLE),
583
+ onAddEvent: TaroHook(exports.HOOK_TYPE.SINGLE),
584
+ modifyMpEvent: TaroHook(exports.HOOK_TYPE.MULTI),
585
+ modifyMpEventImpl: TaroHook(exports.HOOK_TYPE.SINGLE, function (e) {
586
+ try {
587
+ // 有些小程序的事件对象的某些属性只读
588
+ this.call('modifyMpEvent', e);
589
+ }
590
+ catch (error) {
591
+ console.warn('[Taro modifyMpEvent hook Error]: ', error);
592
+ }
593
+ }),
594
+ modifyTaroEvent: TaroHook(exports.HOOK_TYPE.MULTI),
595
+ modifyDispatchEvent: TaroHook(exports.HOOK_TYPE.MULTI),
596
+ initNativeApi: TaroHook(exports.HOOK_TYPE.MULTI),
597
+ patchElement: TaroHook(exports.HOOK_TYPE.MULTI)
598
+ });
599
+
381
600
  const EMPTY_OBJ = {};
382
601
  const EMPTY_ARR = [];
383
602
  const noop = (..._) => { };
384
- const defaultReconciler = Object.create(null);
385
603
  /**
386
604
  * box creates a boxed value.
387
605
  *
@@ -505,15 +723,11 @@ function mergeInternalComponents(components) {
505
723
  }
506
724
  });
507
725
  }
508
- function mergeReconciler(hostConfig) {
509
- Object.keys(hostConfig).forEach(key => {
510
- const value = hostConfig[key];
511
- const raw = defaultReconciler[key];
512
- defaultReconciler[key] = !raw
513
- ? value
514
- : (isArray(raw)
515
- ? raw.concat(value)
516
- : [raw, value]);
726
+ function mergeReconciler(hostConfig, hooksForTest) {
727
+ const obj = hooksForTest || hooks;
728
+ const keys = Object.keys(hostConfig);
729
+ keys.forEach(key => {
730
+ obj.tap(key, hostConfig[key]);
517
731
  });
518
732
  }
519
733
  function unsupport(api) {
@@ -872,8 +1086,25 @@ function equipCommonApis(taro, global, apis = {}) {
872
1086
  taro.miniGlobal = taro.options.miniGlobal = global;
873
1087
  }
874
1088
 
1089
+ // 字符串简写
1090
+ exports.Shortcuts = void 0;
1091
+ (function (Shortcuts) {
1092
+ Shortcuts["Container"] = "container";
1093
+ Shortcuts["Childnodes"] = "cn";
1094
+ Shortcuts["Text"] = "v";
1095
+ Shortcuts["NodeType"] = "nt";
1096
+ Shortcuts["NodeName"] = "nn";
1097
+ // Attrtibutes
1098
+ Shortcuts["Style"] = "st";
1099
+ Shortcuts["Class"] = "cl";
1100
+ Shortcuts["Src"] = "src";
1101
+ })(exports.Shortcuts || (exports.Shortcuts = {}));
1102
+
875
1103
  exports.EMPTY_ARR = EMPTY_ARR;
876
1104
  exports.EMPTY_OBJ = EMPTY_OBJ;
1105
+ exports.Events = Events;
1106
+ exports.TaroHook = TaroHook;
1107
+ exports.TaroHooks = TaroHooks;
877
1108
  exports.animation = animation;
878
1109
  exports.box = box;
879
1110
  exports.cacheDataGet = cacheDataGet;
@@ -881,11 +1112,11 @@ exports.cacheDataHas = cacheDataHas;
881
1112
  exports.cacheDataSet = cacheDataSet;
882
1113
  exports.capitalize = capitalize;
883
1114
  exports.controlledComponent = controlledComponent;
884
- exports.defaultReconciler = defaultReconciler;
885
1115
  exports.ensure = ensure;
886
1116
  exports.focusComponents = focusComponents;
887
1117
  exports.getUniqueKey = getUniqueKey;
888
1118
  exports.hasOwn = hasOwn;
1119
+ exports.hooks = hooks;
889
1120
  exports.indent = indent;
890
1121
  exports.internalComponents = internalComponents;
891
1122
  exports.isArray = isArray;