@mpxjs/core 2.7.11 → 2.7.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/core",
3
- "version": "2.7.11",
3
+ "version": "2.7.16",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
@@ -41,5 +41,5 @@
41
41
  "url": "https://github.com/didi/mpx/issues"
42
42
  },
43
43
  "sideEffects": false,
44
- "gitHead": "484feb97fc5741ce3984fd235833fe0ab83593b9"
44
+ "gitHead": "7a2b5ae385cbd035172ec11bc0bfba84eff10243"
45
45
  }
package/src/core/proxy.js CHANGED
@@ -47,6 +47,7 @@ export default class MPXProxy {
47
47
  this.ignoreProxyMap = makeMap(EXPORT_MPX.config.ignoreProxyWhiteList)
48
48
  if (__mpx_mode__ !== 'web') {
49
49
  this._watchers = []
50
+ this._namedWatchers = {}
50
51
  this._watcher = null
51
52
  this.localKeysMap = {} // 非props key
52
53
  this.renderData = {} // 渲染函数中收集的数据
@@ -142,6 +143,12 @@ export default class MPXProxy {
142
143
  // 强制执行render
143
144
  this.target.$forceUpdate = (...rest) => this.forceUpdate(...rest)
144
145
  this.target.$nextTick = fn => this.nextTick(fn)
146
+ this.target.$getPausableWatchers = () => this._watchers.filter(item => item.pausable)
147
+ this.target.$getWatcherByName = (name) => {
148
+ if (!this._namedWatchers) return null
149
+ return this._namedWatchers[name] || null
150
+ }
151
+ this.target.$getRenderWatcher = () => this._watcher
145
152
  }
146
153
  }
147
154
 
@@ -416,11 +423,11 @@ export default class MPXProxy {
416
423
  warn(`Failed to execute render function, degrade to full-set-data mode.`, this.options.mpxFileResource, e)
417
424
  this.render()
418
425
  }
419
- }, noop)
426
+ }, noop, { pausable: true })
420
427
  } else {
421
428
  renderWatcher = new Watcher(this, () => {
422
429
  this.render()
423
- }, noop)
430
+ }, noop, { pausable: true })
424
431
  }
425
432
  this._watcher = renderWatcher
426
433
  }
@@ -1,4 +1,5 @@
1
1
  import { isObject, noop } from '../helper/utils'
2
+ import { error } from '../helper/log'
2
3
  import Watcher from './watcher'
3
4
  import { queueWatcher } from './scheduler'
4
5
 
@@ -20,6 +21,12 @@ export function watch (vm, expOrFn, cb, options) {
20
21
  options = options || {}
21
22
  options.user = true
22
23
  const watcher = new Watcher(vm, expOrFn, cb, options)
24
+ if (!vm._namedWatchers) vm._namedWatchers = {}
25
+ const name = options.name
26
+ if (name) {
27
+ if (vm._namedWatchers[name]) error(`已存在name=${name} 的 watcher,当存在多个 name 相同 watcher 时仅保留当次创建的 watcher,如需都保留请使用不同的 name!`)
28
+ vm._namedWatchers[name] = watcher
29
+ }
23
30
  if (options.immediate) {
24
31
  cb.call(vm.target, watcher.value)
25
32
  } else if (options.immediateAsync) {
@@ -24,6 +24,7 @@ export default class Watcher {
24
24
  this.deep = !!options.deep
25
25
  this.lazy = !!options.lazy
26
26
  this.sync = !!options.sync
27
+ this.name = options.name
27
28
  } else {
28
29
  this.deep = this.lazy = this.sync = false
29
30
  }
@@ -31,6 +32,10 @@ export default class Watcher {
31
32
  this.id = ++uid // uid for batching
32
33
  this.active = true
33
34
  this.immediateAsync = false
35
+ // 是否暂停,默认为否
36
+ this.paused = false
37
+ // 是否可被暂停,默认为否,不可被暂停
38
+ this.pausable = !!(options && options.pausable) || false
34
39
  this.dirty = this.lazy // for lazy watchers
35
40
  this.deps = []
36
41
  this.newDeps = []
@@ -115,7 +120,7 @@ export default class Watcher {
115
120
  // 支持临时将某个异步watcher修改为sync执行,在模拟setData时使用
116
121
  update (sync) {
117
122
  /* istanbul ignore else */
118
- if (this.lazy) {
123
+ if (this.lazy || this.paused) {
119
124
  this.dirty = true
120
125
  } else if (this.sync || sync) {
121
126
  if (sync) dequeueWatcher(this)
@@ -125,6 +130,21 @@ export default class Watcher {
125
130
  }
126
131
  }
127
132
 
133
+ pause () {
134
+ // pausable=false 不可暂停
135
+ if (!this.pausable) return
136
+ this.paused = true
137
+ }
138
+ resume () {
139
+ // pausable=false 不可恢复
140
+ if (!this.pausable) return
141
+ // paused 阶段被触发,则 resume 后执行一次run
142
+ this.paused = false
143
+ if (this.dirty) {
144
+ this.dirty = false
145
+ this.run()
146
+ }
147
+ }
128
148
  /**
129
149
  * Scheduler job interface.
130
150
  * Will be called by the scheduler.