@tarojs/shared 3.5.0-beta.0 → 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.
@@ -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
1
  export * from './components';
2
+ export * from './event-emitter';
2
3
  export * from './is';
3
4
  export * from './native-apis';
5
+ export * from './runtime-hooks';
4
6
  export { Shortcuts } from './shortcuts';
5
7
  export * from './utils';
package/dist/index.js CHANGED
@@ -338,6 +338,87 @@ const nestElements = new Map([
338
338
  ['swiper-item', 4]
339
339
  ]);
340
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
+
341
422
  function isString(o) {
342
423
  return typeof o === 'string';
343
424
  }
@@ -364,10 +445,161 @@ function isBooleanStringLiteral(o) {
364
445
  }
365
446
  const isArray = Array.isArray;
366
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
+
367
600
  const EMPTY_OBJ = {};
368
601
  const EMPTY_ARR = [];
369
602
  const noop = (..._) => { };
370
- const defaultReconciler = Object.create(null);
371
603
  /**
372
604
  * box creates a boxed value.
373
605
  *
@@ -491,15 +723,11 @@ function mergeInternalComponents(components) {
491
723
  }
492
724
  });
493
725
  }
494
- function mergeReconciler(hostConfig) {
495
- Object.keys(hostConfig).forEach(key => {
496
- const value = hostConfig[key];
497
- const raw = defaultReconciler[key];
498
- defaultReconciler[key] = !raw
499
- ? value
500
- : (isArray(raw)
501
- ? raw.concat(value)
502
- : [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]);
503
731
  });
504
732
  }
505
733
  function unsupport(api) {
@@ -874,6 +1102,9 @@ exports.Shortcuts = void 0;
874
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;