Skip to content

Commit 03335d2

Browse files
committed
test: 添加effect debug日志信息
1 parent aa27efc commit 03335d2

File tree

7 files changed

+55
-17
lines changed

7 files changed

+55
-17
lines changed

packages/core/src/observer/array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const arrayMethods = Object.create(arrayProto)
3232
}
3333
if (inserted) ob.observeArray(inserted)
3434
// notify change
35-
ob.dep.notify()
35+
ob.dep.notify(method, 'this', new Error().stack)
3636
}
3737
return result
3838
})

packages/core/src/observer/computed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Dep from './dep'
33
import { createRef } from './ref'
44
import { ReactiveEffect } from './effect'
55

6-
export function computed (getterOrOptions) {
6+
export function computed (getterOrOptions, options = {}) {
77
let getter, setter
88
if (isFunction(getterOrOptions)) {
99
getter = getterOrOptions
@@ -17,7 +17,7 @@ export function computed (getterOrOptions) {
1717
let value
1818
const effect = new ReactiveEffect(getter, () => {
1919
dirty = true
20-
})
20+
}, options.debug || 0, options.name || 'anonymous computed')
2121

2222
return createRef({
2323
get: () => {

packages/core/src/observer/dep.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export default class Dep {
2626
}
2727
}
2828

29-
notify () {
29+
notify (key, value, stack) {
3030
// stabilize the subscriber list first
3131
const subs = this.subs.slice()
3232
for (let i = 0, l = subs.length; i < l; i++) {
33-
subs[i].update()
33+
subs[i].update(key, value, stack)
3434
}
3535
}
3636
}

packages/core/src/observer/effect.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,54 @@ export function resetTracking () {
1717
shouldTrack = last === undefined ? true : last
1818
}
1919

20+
global.__setMpxReactiveEffect = (value) => {}
21+
2022
export class ReactiveEffect {
2123
active = true
2224
deps = []
2325
newDeps = []
2426
depIds = new Set()
2527
newDepIds = new Set()
2628
allowRecurse = false
29+
debug = 0
30+
name = 'no-name'
2731

2832
constructor (
2933
fn,
3034
scheduler,
31-
scope
35+
scope,
36+
debug,
37+
name
3238
) {
39+
this.debug = debug || 0
40+
this.name = name || 'no-name'
3341
this.id = ++uid
34-
this.fn = fn
35-
this.scheduler = scheduler
42+
this.fn = (...args) => {
43+
this.debug > 0 && console.log(`[Mpx Effect] id:${this.id} name: ${this.name} will run`)
44+
this.debug > 1 && console.log(new Error().stack)
45+
const t = global.__setMpxReactiveEffect
46+
global.__setMpxReactiveEffect = (option) => {
47+
Object.entries(option).forEach(([key, value]) => {
48+
this[key] = value
49+
})
50+
}
51+
const result = fn(...args)
52+
global.__setMpxReactiveEffect = t
53+
return result
54+
}
55+
this.scheduler = (...args) => {
56+
this.debug > 0 && console.log(`[Mpx Effect] id:${this.id} name: ${this.name} will scheduled`)
57+
this.debug > 1 && console.log(new Error().stack)
58+
const t = global.__setMpxReactiveEffect
59+
global.__setMpxReactiveEffect = (option) => {
60+
Object.entries(option).forEach(([key, value]) => {
61+
this[key] = value
62+
})
63+
}
64+
const result = scheduler(...args)
65+
global.__setMpxReactiveEffect = t
66+
return result
67+
}
3668
this.pausedState = PausedState.resumed
3769
recordEffectScope(this, scope)
3870
}
@@ -85,12 +117,16 @@ export class ReactiveEffect {
85117
}
86118

87119
// same as trigger
88-
update () {
120+
update (key, value, stack) {
89121
// avoid dead cycle
90122
if (Dep.target !== this || this.allowRecurse) {
91123
if (this.pausedState !== PausedState.resumed) {
124+
this.debug > 0 && console.log(`[Mpx Effect] id:${this.id} name: ${this.name} pausedState will set to dirty with, from ${key}: ${value}`)
125+
this.debug > 1 && console.log(stack)
92126
this.pausedState = PausedState.dirty
93127
} else {
128+
this.debug > 0 && console.log(`[Mpx Effect] id:${this.id} name: ${this.name} will updated, from ${key}: ${value}`)
129+
this.debug > 1 && console.log(stack)
94130
this.scheduler ? this.scheduler() : this.run()
95131
}
96132
}

packages/core/src/observer/reactive.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ export function defineReactive (obj, key, val, shallow) {
121121
const getter = property && property.get
122122
const setter = property && property.set
123123

124+
const stack = new Error().stack
125+
124126
let childOb = shallow ? getObserver(val) : observe(val)
125127
Object.defineProperty(obj, key, {
126128
enumerable: true,
@@ -151,7 +153,7 @@ export function defineReactive (obj, key, val, shallow) {
151153
val = newVal
152154
}
153155
childOb = shallow ? getObserver(newVal) : observe(newVal)
154-
dep.notify()
156+
dep.notify(key, newVal, stack)
155157
}
156158
})
157159
}
@@ -177,7 +179,7 @@ export function set (target, key, val) {
177179
return val
178180
}
179181
defineReactive(ob.value, key, val, ob.shallow)
180-
ob.dep.notify()
182+
ob.dep.notify(key, val, new Error().stack)
181183
return val
182184
}
183185

@@ -197,7 +199,7 @@ export function del (target, key) {
197199
if (!ob) {
198200
return
199201
}
200-
ob.dep.notify()
202+
ob.dep.notify(key, undefined, new Error().stack)
201203
}
202204

203205
/**

packages/core/src/observer/watch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export function watch (source, cb, options = {}) {
137137

138138
job.allowRecurse = !!cb
139139

140-
const effect = new ReactiveEffect(getter, scheduler)
140+
const effect = new ReactiveEffect(getter, scheduler, options.debug || 0, options.name || 'anonymous watch')
141141

142142
if (cb) {
143143
if (immediate) {

packages/core/src/platform/patch/getDefaultOptions.ios.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function getSystemInfo () {
3333
}
3434
}
3535

36-
function createEffect (proxy, componentsMap) {
36+
function createEffect (proxy, componentsMap, rawOptions) {
3737
const update = proxy.update = () => {
3838
// react update props in child render(async), do not need exec pre render
3939
// if (proxy.propsUpdatedFlag) {
@@ -65,7 +65,7 @@ function createEffect (proxy, componentsMap) {
6565
// reset instance
6666
proxy.target.__resetInstance()
6767
return callWithErrorHandling(proxy.target.__injectedRender.bind(proxy.target), proxy, 'render function', [innerCreateElement, getComponent])
68-
}, () => queueJob(update), proxy.scope)
68+
}, () => { return queueJob(update) }, proxy.scope, 1, `MpxRenderEffect-${rawOptions.mpxFileResource || 'unknown'}`)
6969
// render effect允许自触发
7070
proxy.toggleRecurse(true)
7171
}
@@ -332,7 +332,7 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps
332332
stateVersion: Symbol(),
333333
subscribe: (onStoreChange) => {
334334
if (!proxy.effect) {
335-
createEffect(proxy, componentsMap)
335+
createEffect(proxy, componentsMap, rawOptions)
336336
proxy.stateVersion = Symbol()
337337
}
338338
proxy.onStoreChange = onStoreChange
@@ -348,7 +348,7 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps
348348
})
349349
// react数据响应组件更新管理器
350350
if (!proxy.effect) {
351-
createEffect(proxy, componentsMap)
351+
createEffect(proxy, componentsMap, rawOptions)
352352
}
353353

354354
return instance

0 commit comments

Comments
 (0)