@mpxjs/core 2.8.61 → 2.8.64-bridgetest

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.
package/@types/index.d.ts CHANGED
@@ -46,7 +46,7 @@ type FullPropType<T> = {
46
46
  }
47
47
 
48
48
  interface Properties {
49
- [key: string]: WechatMiniprogram.Component.AllProperty | PropType<any> | FullPropType<any>
49
+ [key: string]: WechatMiniprogram.Component.AllProperty
50
50
  }
51
51
 
52
52
  interface Methods {
@@ -82,12 +82,12 @@ type PropValueType<Def> = Def extends {
82
82
  }
83
83
  ? T
84
84
  : Def extends (...args: any[]) => infer T
85
- ? T
86
- : Def extends FullPropType<infer T>
87
- ? T
88
- : Def extends PropType<infer T>
89
- ? T
90
- : any;
85
+ ? T
86
+ : Def extends FullPropType<infer T>
87
+ ? T
88
+ : Def extends PropType<infer T>
89
+ ? T
90
+ : any;
91
91
 
92
92
  type GetPropsType<T> = {
93
93
  readonly [K in keyof T]: PropValueType<T[K]>
@@ -240,7 +240,10 @@ export function createApp<T extends WechatMiniprogram.IAnyObject> (opt: WechatMi
240
240
 
241
241
  type MixinType = 'app' | 'page' | 'component'
242
242
 
243
- export function injectMixins (mixins: object | Array<object>, options?: MixinType | MixinType[] | { types?: MixinType | MixinType[], stage?: number }): Mpx
243
+ export function injectMixins (mixins: object | Array<object>, options?: MixinType | MixinType[] | {
244
+ types?: MixinType | MixinType[],
245
+ stage?: number
246
+ }): Mpx
244
247
 
245
248
  // export function watch (expr: string | (() => any), handler: WatchHandler | WatchOptWithHandler, options?: WatchOpt): () => void
246
249
 
@@ -339,7 +342,7 @@ export interface Ref<T = any> {
339
342
  * We need this to be in public d.ts but don't want it to show up in IDE
340
343
  * autocomplete, so we use a private Symbol instead.
341
344
  */
342
- [RefSymbol]: true
345
+ [RefSymbol]: true
343
346
  }
344
347
 
345
348
  type CollectionTypes = IterableCollections | WeakCollections
@@ -448,7 +451,11 @@ export interface WatchOptions extends WatchEffectOptions {
448
451
 
449
452
  interface EffectScope {
450
453
  run<T> (fn: () => T): T | undefined // 如果作用域不活跃就为 undefined
451
- stop (): void
454
+ stop (fromParent?: boolean): void
455
+
456
+ pause (): void
457
+
458
+ resume (ignoreDirty?: boolean): void
452
459
  }
453
460
 
454
461
 
@@ -614,7 +621,7 @@ export function onTabItemTap (callback: WechatMiniprogram.Page.ILifetime['onTabI
614
621
  export function onSaveExitState (callback: () => void): void
615
622
 
616
623
  // get instance
617
- export function getCurrentInstance<T extends ComponentIns<{}, {}, {}>> (): T
624
+ export function getCurrentInstance<T extends ComponentIns<{}, {}, {}>> (): { proxy: T, [x: string]: any }
618
625
 
619
626
  // I18n
620
627
  export function useI18n<Options extends {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@mpxjs/core",
3
- "version": "2.8.61",
3
+ "version": "2.8.64-bridgetest",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
7
7
  "mpx"
8
8
  ],
9
9
  "author": "donghongping",
10
- "license": "Apache",
10
+ "license": "Apache-2.0",
11
11
  "module": "src/index.js",
12
12
  "types": "@types/index.d.ts",
13
13
  "directories": {
@@ -19,7 +19,7 @@
19
19
  ],
20
20
  "main": "src/index.js",
21
21
  "dependencies": {
22
- "@mpxjs/utils": "^2.8.60",
22
+ "@mpxjs/utils": "^2.8.64-bridgetest",
23
23
  "lodash": "^4.1.1",
24
24
  "miniprogram-api-typings": "^3.10.0"
25
25
  },
@@ -47,5 +47,5 @@
47
47
  "url": "https://github.com/didi/mpx/issues"
48
48
  },
49
49
  "sideEffects": false,
50
- "gitHead": "f393ffc439f21b24f5f09a65ef99db20b145e19a"
50
+ "gitHead": "4080623bc3f493d1cfb7a506dffaec0f79084af6"
51
51
  }
@@ -10,6 +10,7 @@ export const ONLOAD = '__onLoad__'
10
10
  export const ONSHOW = '__onShow__'
11
11
  export const ONHIDE = '__onHide__'
12
12
  export const ONRESIZE = '__onResize__'
13
+ export const SERVERPREFETCH = '__serverPrefetch__'
13
14
 
14
15
  export const INNER_LIFECYCLES = [
15
16
  BEFORECREATE,
@@ -19,6 +20,7 @@ export const INNER_LIFECYCLES = [
19
20
  BEFOREUPDATE,
20
21
  UPDATED,
21
22
  BEFOREUNMOUNT,
23
+ SERVERPREFETCH,
22
24
  UNMOUNTED,
23
25
  ONLOAD,
24
26
  ONSHOW,
@@ -314,17 +314,19 @@ export function mergeToArray (parent, child, key) {
314
314
  function composeHooks (target, includes) {
315
315
  Object.keys(target).forEach(key => {
316
316
  if (!includes || includes[key]) {
317
- const hooksArr = target[key]
318
- hooksArr && (target[key] = function (...args) {
319
- let result
320
- for (let i = 0; i < hooksArr.length; i++) {
321
- if (typeof hooksArr[i] === 'function') {
322
- const data = hooksArr[i].apply(this, args)
323
- data !== undefined && (result = data)
317
+ const hooks = target[key]
318
+ if (Array.isArray(hooks)) {
319
+ target[key] = function (...args) {
320
+ let result
321
+ for (let i = 0; i < hooks.length; i++) {
322
+ if (typeof hooks[i] === 'function') {
323
+ const data = hooks[i].apply(this, args)
324
+ data !== undefined && (result = data)
325
+ }
324
326
  }
327
+ return result
325
328
  }
326
- return result
327
- })
329
+ }
328
330
  }
329
331
  })
330
332
  }
@@ -352,7 +354,7 @@ function transformHOOKS (options) {
352
354
  const componentHooksMap = makeMap(convertRule.lifecycle.component)
353
355
  for (const key in options) {
354
356
  // 使用Component创建page实例,页面专属生命周期&自定义方法需写在methods内部
355
- if (typeof options[key] === 'function' && key !== 'dataFn' && key !== 'setup' && !componentHooksMap[key]) {
357
+ if (typeof options[key] === 'function' && key !== 'dataFn' && key !== 'setup' && key !== 'serverPrefetch' && !componentHooksMap[key]) {
356
358
  if (!options.methods) options.methods = {}
357
359
  options.methods[key] = options[key]
358
360
  delete options[key]
package/src/core/proxy.js CHANGED
@@ -37,6 +37,7 @@ import {
37
37
  BEFOREUPDATE,
38
38
  UPDATED,
39
39
  BEFOREUNMOUNT,
40
+ SERVERPREFETCH,
40
41
  UNMOUNTED,
41
42
  ONLOAD,
42
43
  ONSHOW,
@@ -659,6 +660,7 @@ export const onLoad = createHook(ONLOAD)
659
660
  export const onShow = createHook(ONSHOW)
660
661
  export const onHide = createHook(ONHIDE)
661
662
  export const onResize = createHook(ONRESIZE)
663
+ export const onServerPrefetch = createHook(SERVERPREFETCH)
662
664
  export const onPullDownRefresh = createHook('__onPullDownRefresh__')
663
665
  export const onReachBottom = createHook('__onReachBottom__')
664
666
  export const onShareAppMessage = createHook('__onShareAppMessage__')
package/src/index.js CHANGED
@@ -29,6 +29,7 @@ export {
29
29
  UPDATED,
30
30
  BEFOREUNMOUNT,
31
31
  UNMOUNTED,
32
+ SERVERPREFETCH,
32
33
  ONLOAD,
33
34
  ONSHOW,
34
35
  ONHIDE,
@@ -42,6 +43,7 @@ export {
42
43
  onUpdated,
43
44
  onBeforeUnmount,
44
45
  onUnmounted,
46
+ onServerPrefetch,
45
47
  onLoad,
46
48
  onShow,
47
49
  onHide,
@@ -119,7 +119,9 @@ export class ReactiveEffect {
119
119
  }
120
120
 
121
121
  pause () {
122
- this.pausedState = PausedState.paused
122
+ if (this.pausedState !== PausedState.dirty) {
123
+ this.pausedState = PausedState.paused
124
+ }
123
125
  }
124
126
 
125
127
  resume (ignoreDirty = false) {
@@ -63,7 +63,7 @@ export default function pageStatusMixin (mixinType) {
63
63
  },
64
64
  created () {
65
65
  // onLoad应该在用户声明周期CREATED后再执行,故此处使用原生created声明周期来触发onLoad
66
- const query = (global.__mpxRouter && global.__mpxRouter.currentRoute && global.__mpxRouter.currentRoute.query) || {}
66
+ const query = this.$root.$options?.router?.currentRoute?.query || {}
67
67
  this.__mpxProxy.callHook(ONLOAD, [query])
68
68
  }
69
69
  })
@@ -71,25 +71,27 @@ export default function pageStatusMixin (mixinType) {
71
71
 
72
72
  Object.assign(mixin, {
73
73
  [CREATED] () {
74
- const pageInstance = mixinType === 'page' ? this : getCurrentPageInstance()
75
- if (pageInstance) {
76
- this.$watch(() => pageInstance.mpxPageStatus, status => {
77
- if (!status) return
78
- if (status === 'show') this.__mpxProxy.callHook(ONSHOW)
79
- if (status === 'hide') this.__mpxProxy.callHook(ONHIDE)
80
- const pageLifetimes = this.__mpxProxy.options.pageLifetimes
81
- if (pageLifetimes) {
82
- if (/^resize/.test(status) && isFunction(pageLifetimes.resize)) {
83
- // resize
84
- pageLifetimes.resize.call(this, systemInfo)
85
- } else if (isFunction(pageLifetimes[status])) {
86
- // show & hide
87
- pageLifetimes[status].call(this)
74
+ if (isBrowser) {
75
+ const pageInstance = mixinType === 'page' ? this : getCurrentPageInstance()
76
+ if (pageInstance) {
77
+ this.$watch(() => pageInstance.mpxPageStatus, status => {
78
+ if (!status) return
79
+ if (status === 'show') this.__mpxProxy.callHook(ONSHOW)
80
+ if (status === 'hide') this.__mpxProxy.callHook(ONHIDE)
81
+ const pageLifetimes = this.__mpxProxy.options.pageLifetimes
82
+ if (pageLifetimes) {
83
+ if (/^resize/.test(status) && isFunction(pageLifetimes.resize)) {
84
+ // resize
85
+ pageLifetimes.resize.call(this, systemInfo)
86
+ } else if (isFunction(pageLifetimes[status])) {
87
+ // show & hide
88
+ pageLifetimes[status].call(this)
89
+ }
88
90
  }
89
- }
90
- }, {
91
- sync: true
92
- })
91
+ }, {
92
+ sync: true
93
+ })
94
+ }
93
95
  }
94
96
  }
95
97
  })
@@ -1,11 +1,12 @@
1
1
  import transferOptions from '../core/transferOptions'
2
2
  import mergeOptions from '../core/mergeOptions'
3
3
  import builtInKeysMap from './patch/builtInKeysMap'
4
- import { makeMap, spreadProp } from '@mpxjs/utils'
4
+ import { makeMap, spreadProp, isBrowser } from '@mpxjs/utils'
5
+ import { mergeLifecycle } from '../convertor/mergeLifecycle'
5
6
  import * as webLifecycle from '../platform/patch/web/lifecycle'
6
7
  import Mpx from '../index'
7
8
 
8
- const webAppHooksMap = makeMap(webLifecycle.LIFECYCLE.APP_HOOKS)
9
+ const webAppHooksMap = makeMap(mergeLifecycle(webLifecycle.LIFECYCLE).app)
9
10
 
10
11
  function filterOptions (options, appData) {
11
12
  const newOptions = {}
@@ -35,7 +36,7 @@ export default function createApp (option, config = {}) {
35
36
  created () {
36
37
  Object.assign(this, Mpx.prototype)
37
38
  Object.assign(this, appData)
38
- const current = (global.__mpxRouter && global.__mpxRouter.currentRoute) || {}
39
+ const current = this.$root.$options?.router?.currentRoute || {}
39
40
  const options = {
40
41
  path: current.path && current.path.replace(/^\//, ''),
41
42
  query: current.query,
@@ -49,19 +50,24 @@ export default function createApp (option, config = {}) {
49
50
  hide: [],
50
51
  error: []
51
52
  }
52
- if (this.$options.onShow) {
53
- this.$options.onShow.call(this, options)
54
- global.__mpxAppCbs.show.push(this.$options.onShow.bind(this))
55
- }
56
- if (this.$options.onHide) {
57
- global.__mpxAppCbs.hide.push(this.$options.onHide.bind(this))
58
- }
59
- if (this.$options.onError) {
60
- global.__mpxAppCbs.error.push(this.$options.onError.bind(this))
53
+ if (isBrowser) {
54
+ if (this.$options.onShow) {
55
+ this.$options.onShow.call(this, options)
56
+ global.__mpxAppCbs.show.push(this.$options.onShow.bind(this))
57
+ }
58
+ if (this.$options.onHide) {
59
+ global.__mpxAppCbs.hide.push(this.$options.onHide.bind(this))
60
+ }
61
+ if (this.$options.onError) {
62
+ global.__mpxAppCbs.error.push(this.$options.onError.bind(this))
63
+ }
61
64
  }
62
65
  }
63
66
  })
64
67
  } else {
68
+ if (option.onAppInit) {
69
+ option.onAppInit()
70
+ }
65
71
  builtInMixins.push({
66
72
  onLaunch () {
67
73
  Object.assign(this, Mpx.prototype)
@@ -77,6 +83,9 @@ export default function createApp (option, config = {}) {
77
83
  global.__mpxOptionsMap = global.__mpxOptionsMap || {}
78
84
  global.__mpxOptionsMap[global.currentModuleId] = defaultOptions
79
85
  global.getApp = function () {
86
+ if (!isBrowser) {
87
+ console.error('[Mpx runtime error]: Dangerous API! global.getApp method is running in non browser environments')
88
+ }
80
89
  return appData
81
90
  }
82
91
  } else {
@@ -1,8 +1,60 @@
1
- import {
2
- effectScope as vueEffectScope,
3
- getCurrentScope as vueGetCurrentScope,
4
- onScopeDispose
5
- } from 'vue'
1
+ import { EffectScope } from 'vue'
2
+ import { hasOwn } from '@mpxjs/utils'
3
+ import { PausedState } from '../../helper/const'
4
+
5
+ const hackEffectScope = () => {
6
+ EffectScope.prototype.pause = function () {
7
+ if (this.active) {
8
+ let i, l
9
+ for (i = 0, l = this.effects.length; i < l; i++) {
10
+ const effect = this.effects[i]
11
+ // vue2.7中存在对于watcher实例方法的重写(doWatch),因此无法通过修改Watcher.prototype统一实现pause和resume,只能逐个实例修改实现
12
+ if (!hasOwn(effect, 'pausedState')) {
13
+ effect.pausedState = PausedState.resumed
14
+ const rawUpdate = effect.update
15
+ effect.update = function () {
16
+ if (effect.pausedState !== PausedState.resumed) {
17
+ effect.pausedState = PausedState.dirty
18
+ } else {
19
+ rawUpdate.call(effect)
20
+ }
21
+ }
22
+ }
23
+ if (effect.pausedState !== PausedState.dirty) {
24
+ effect.pausedState = PausedState.paused
25
+ }
26
+ }
27
+ if (this.scopes) {
28
+ for (i = 0, l = this.scopes.length; i < l; i++) {
29
+ this.scopes[i].pause()
30
+ }
31
+ }
32
+ }
33
+ }
34
+
35
+ EffectScope.prototype.resume = function (ignoreDirty = false) {
36
+ if (this.active) {
37
+ let i, l
38
+ for (i = 0, l = this.effects.length; i < l; i++) {
39
+ const effect = this.effects[i]
40
+ if (hasOwn(effect, 'pausedState')) {
41
+ const lastPausedState = effect.pausedState
42
+ effect.pausedState = PausedState.resumed
43
+ if (!ignoreDirty && lastPausedState === PausedState.dirty) {
44
+ effect.update()
45
+ }
46
+ }
47
+ }
48
+ if (this.scopes) {
49
+ for (i = 0, l = this.scopes.length; i < l; i++) {
50
+ this.scopes[i].resume(ignoreDirty)
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+
57
+ hackEffectScope()
6
58
 
7
59
  export {
8
60
  // watch
@@ -29,27 +81,12 @@ export {
29
81
  // computed
30
82
  computed,
31
83
  // instance
32
- getCurrentInstance
33
- } from 'vue'
34
-
35
- const noop = () => {
36
- }
37
-
38
- const fixEffectScope = (scope) => {
39
- scope.pause = noop
40
- scope.resume = noop
41
- return scope
42
- }
43
-
44
- const effectScope = (detached) => fixEffectScope(vueEffectScope(detached))
45
- const getCurrentScope = () => fixEffectScope(vueGetCurrentScope())
46
-
47
- export {
84
+ getCurrentInstance,
48
85
  // effectScope
49
86
  effectScope,
50
87
  getCurrentScope,
51
88
  onScopeDispose
52
- }
89
+ } from 'vue'
53
90
 
54
91
  export {
55
92
  // i18n
@@ -3,7 +3,7 @@ import mergeOptions from '../../../core/mergeOptions'
3
3
  import { diffAndCloneA, hasOwn } from '@mpxjs/utils'
4
4
  import { getCurrentInstance as getCurrentVueInstance } from '../../export/index'
5
5
  import MpxProxy, { setCurrentInstance, unsetCurrentInstance } from '../../../core/proxy'
6
- import { BEFORECREATE, BEFOREUPDATE, UPDATED, BEFOREUNMOUNT, UNMOUNTED } from '../../../core/innerLifecycle'
6
+ import { BEFORECREATE, BEFOREUPDATE, UPDATED, BEFOREUNMOUNT, UNMOUNTED, SERVERPREFETCH } from '../../../core/innerLifecycle'
7
7
 
8
8
  function filterOptions (options) {
9
9
  const newOptions = {}
@@ -79,6 +79,9 @@ export function getDefaultOptions (type, { rawOptions = {} }) {
79
79
  },
80
80
  destroyed () {
81
81
  if (this.__mpxProxy) this.__mpxProxy.callHook(UNMOUNTED)
82
+ },
83
+ serverPrefetch () {
84
+ if (this.__mpxProxy) return this.__mpxProxy.callHook(SERVERPREFETCH)
82
85
  }
83
86
  }]
84
87
  // 为了在builtMixin中可以使用某些rootMixin实现的特性(如数据响应等),此处builtInMixin在rootMixin之后执行,但是当builtInMixin使用存在对应内建生命周期的目标平台声明周期写法时,可能会出现用户生命周期比builtInMixin中的生命周期先执行的情况,为了避免这种情况发生,builtInMixin应该尽可能使用内建生命周期来编写
@@ -9,7 +9,8 @@ const COMPONENT_HOOKS = [
9
9
  'deactivated',
10
10
  'beforeDestroy',
11
11
  'destroyed',
12
- 'errorCaptured'
12
+ 'errorCaptured',
13
+ 'serverPrefetch'
13
14
  ]
14
15
 
15
16
  const PAGE_HOOKS = [
@@ -38,7 +39,9 @@ const APP_HOOKS = [
38
39
  'onError',
39
40
  'onPageNotFound',
40
41
  'onUnhandledRejection',
41
- 'onThemeChange'
42
+ 'onThemeChange',
43
+ 'onSSRAppCreated',
44
+ 'onAppInit'
42
45
  ]
43
46
 
44
47
  export const LIFECYCLE = {
@@ -14,7 +14,8 @@ const APP_HOOKS = [
14
14
  'onError',
15
15
  'onPageNotFound',
16
16
  'onUnhandledRejection',
17
- 'onThemeChange'
17
+ 'onThemeChange',
18
+ 'onAppInit'
18
19
  ]
19
20
 
20
21
  const PAGE_HOOKS = [