feat(watch): 实现watch功能并完善响应式系统
- 新增apiWatch.ts实现watch功能,支持immediate和deep选项 - 扩展ReactiveEffect类添加stop方法用于停止监听 - 导出ReactiveEffect和isReactive函数供外部使用 - 添加ReactiveFlags枚举和IS_REACTIVE标识符 - 在reactive对象上添加__v_isReactive标识 - 导出EMPTY_OBJ常量用于默认参数 - 添加watch功能到Vue入口文件 - 创建watch.html示例验证监听功能正常工作
This commit is contained in:
@@ -41,6 +41,7 @@ export class ReactiveEffect<T = any> {
|
||||
activeEffect = this
|
||||
return this.fn() //完成第一次getter行为的触发
|
||||
}
|
||||
stop() {}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export { reactive } from './reactive'
|
||||
export { effect } from './effect'
|
||||
export { reactive, isReactive } from './reactive'
|
||||
export { effect, ReactiveEffect } from './effect'
|
||||
export { ref } from './ref'
|
||||
export { computed } from './computed'
|
||||
|
||||
@@ -8,6 +8,10 @@ import { isObject } from '@vue/shared'
|
||||
*/
|
||||
export const reactiveMap = new WeakMap<object, any>()
|
||||
|
||||
export const enum ReactiveFlags {
|
||||
IS_REACTIVE = '__v_isReactive'
|
||||
}
|
||||
|
||||
/**
|
||||
* 为复杂数据类型, 创建响应式对象
|
||||
* @param target 被代理的对象
|
||||
@@ -35,7 +39,8 @@ function createReactiveObject(
|
||||
}
|
||||
//未被代理则生成proxy实例
|
||||
const proxy = new Proxy(target, baseHandlers)
|
||||
|
||||
//添加reactive标识
|
||||
proxy[ReactiveFlags.IS_REACTIVE] = true
|
||||
//缓存代理对象
|
||||
proxyMap.set(target, proxy)
|
||||
return proxy
|
||||
@@ -43,3 +48,7 @@ function createReactiveObject(
|
||||
|
||||
export const toReactive = <T extends unknown>(value: T): T =>
|
||||
isObject(value) ? reactive(value as object) : value
|
||||
|
||||
export function isReactive(value): boolean {
|
||||
return !!(value && value[ReactiveFlags.IS_REACTIVE])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user