JavaScript系列(64)--响应式状态管理实现详解

时间:2025-02-08 14:49:25

JavaScript响应式状态管理实现详解 ????

今天,让我们深入探讨JavaScript的响应式状态管理实现。响应式状态管理是现代前端框架中的核心特性,它能够自动追踪状态变化并更新相关依赖。

响应式系统基础概念 ????

???? 小知识:响应式状态管理的核心是自动依赖收集和更新传播。当状态发生变化时,系统能够自动识别并更新所有依赖于该状态的计算和副作用。

基础响应式系统实现 ????

// 1. 响应式数据包装器
class Reactive {
    constructor(value) {
        this._value = value;
        this.deps = new Set();
    }
    
    get value() {
        // 依赖收集
        if (activeEffect) {
            this.deps.add(activeEffect);
        }
        return this._value;
    }
    
    set value(newValue) {
        this._value = newValue;
        // 触发更新
        this.notify();
    }
    
    notify() {
        // 执行所有依赖
        this.deps.forEach(effect => effect());
    }
}

// 2. 副作用追踪器
let activeEffect = null;

function effect(fn) {
    const wrappedEffect = () => {
        activeEffect = wrappedEffect;
        fn();
        activeEffect = null;
    };
    
    wrappedEffect();
    return wrappedEffect;
}

// 3. 计算属性实现
class Computed {
    constructor(getter) {
        this._getter = getter;
        this._value = undefined;
        this._dirty = true;
        this.effect = effect(() => {
            this._dirty = true;
        });
    }
    
    get value() {
        if (this._dirty) {
            this._value = this._getter();
            this._dirty = false;
        }
        return this._value;
    }
}

高级响应式特性 ????

// 1. 响应式对象代理
class ReactiveProxy {
    static create(target) {
        return new Proxy(target, {
            get(target, key) {
                // 依赖收集
                track(target, key);
                return Reflect.get(target, key);
            },
            
            set(target, key, value) {
                const oldValue = target[key];
                const result = Reflect.set(target, key, value);
                
                if (oldValue !== value) {
                    // 触发更新
                    trigger(target, key);
                }
                
                return result;
            }
        });
    }
}

// 2. 依赖管理器
class DependencyManager {
    constructor() {
        this.targetMap = new WeakMap();
    }
    
    track(target, key) {
        if (!activeEffect) return;
        
        let depsMap = this.targetMap.get(target);
        if (!depsMap) {
            depsMap = new Map();
            this.targetMap.set(target, depsMap);
        }
        
        let dep = depsMap.get(key);
        if (!dep) {
            dep = new Set();
            depsMap.set(key, dep);
        }
        
        dep.add(activeEffect);
    }
    
    trigger(target, key) {
        const depsMap = this.targetMap.get(target);
        if (!depsMap) return;
        
        const dep = depsMap.get(key);
        if (dep) {
            dep.forEach(effect => effect());
        }
    }
}

// 3. 监听器实现
class Watcher {
    constructor(source, callback, options = {}) {
        this.source = source;
        this.callback = callback;
        this.options = options;
        this.cleanup = null;
        
        this.effect = effect(() => {
            const newValue = this.getValue();
            if (this.cleanup) {
                this.cleanup();
            }
            this.cleanup = this.callback(newValue);
        });
    }
    
    getValue() {
        if (typeof this.source === 'function') {
            return this.source();
        }
        return this.source.value;
    }
    
    stop() {
        if (this.cleanup) {
            this.cleanup();
        }
        // 移除effect
        this.effect.deps.forEach(dep => dep.delete(this.effect));
    }
}

性能优化策略 ⚡

// 1. 批量更新处理
class BatchUpdateManager {
    constructor() {
        this.queue = new Set();
        this.isFlushing = false;
        this.isFlushPending = false;
    }
    
    enqueue(job) {
        this.queue.add(job);
        
        if (!this.isFlushing && !this.isFlushPending) {
            this.isFlushPending = true;
            Promise.resolve().then(() => this.flushJobs());
        }
    }
    
    async flushJobs() {
        this.isFlushing = true;
        this.isFlushPending = false;
        
        // 按照优先级排序
        const jobs = Array.from(this.queue)
            .sort((a, b) => a.priority - b.priority);
        
        this.queue.clear();
        
        for (const job of jobs) {
            try {
                await job.execute();
            } catch (error) {
                console.error('Job execution error:', error);
            }
        }
        
        this.isFlushing = false;
    }
}

// 2. 依赖优化
class DependencyOptimizer {
    constructor() {
        this.depCache = new WeakMap();
    }
    
    optimizeDeps(deps) {
        // 移除重复依赖
        const uniqueDeps = new Set(deps);
        
        // 缓存依赖关系
        const cached = this.depCache.get(uniqueDeps);
        if (cached) {
            return cached;
        }
        
        const optimized = this.analyze(uniqueDeps);
        this.depCache.set(uniqueDeps, optimized);
        return optimized;
    }
    
    analyze(deps) {
        // 分析依赖关系,移除无用依赖
        return Array.from(deps).filter(dep => {
            return !this.isRedundant(dep, deps);
        });
    }
    
    isRedundant(dep, allDeps) {
        // 检查是否是冗余依赖
        return false; // 实现具体的冗余检查逻辑
    }
}

// 3. 内存管理
class MemoryManager {
    constructor() {
        this.weakRefs = new WeakMap();
        this.gcThreshold = 1000;
    }
    
    register(target, deps) {
        this.weakRefs.set(target, {
            deps,
            lastAccessed: Date.now()
        });
    }
    
    cleanup() {
        const now = Date.now();
        
        // 清理长时间未访问的依赖
        for (const [target, info] of this.weakRefs) {
            if (now - info.lastAccessed > this.gcThreshold) {
                info.deps.forEach(dep => dep.delete(target));
                this.weakRefs.delete(target);
            }
        }
    }
}

实际应用场景 ????

// 1. 响应式状态存储
class ReactiveStore {
    constructor(initialState = {}) {
        this.state = ReactiveProxy.create(initialState);
        this.watchers = new Set();
    }
    
    watch(path, callback) {
        const watcher = new Watcher(
            () => this.getNestedValue(path),
            callback
        );
        this.watchers.add(watcher);
        return () => {
            watcher.stop();
            this.watchers.delete(watcher);
        };
    }
    
    getNestedValue(path) {
        return path.split('.').reduce((obj, key) => obj[key], this.state);
    }
    
    setState(newState) {
        Object.assign(this.state, newState);
    }
}

// 2. 响应式表单
class ReactiveForm {
    constructor(initialData = {}) {
        this.data = ReactiveProxy.create(initialData);
        this.validators = new Map();
        this.errors = ReactiveProxy.create({});
    }
    
    addField(name, validator) {
        this.validators.set(name, validator);
        this.validate(name);
    }
    
    watch(field, callback) {
        return new Watcher(
            () => this.data[field],
            value => {
                this.validate(field);
                callback(value);
            }
        );
    }
    
    validate(field) {
        const validator = this.validators.get(field);
        if (validator) {
            const value = this.data[field];
            const result = validator(value);
            this.errors[field] = result.valid ? null : result.error;
        }
    }
}

// 3. 响应式组件
class ReactiveComponent {
    constructor(props = {}) {
        this.props = ReactiveProxy.create(props);
        this.state = ReactiveProxy.create({});
        this.effects = new Set();
        this.mounted = false;
    }
    
    setState(newState) {
        Object.assign(this.state, newState);
    }
    
    mount() {
        this.mounted = true;
        this.effects.forEach(effect => effect());
    }
    
    unmount() {
        this.mounted = false;
        this.effects.forEach(effect => effect.stop());
        this.effects.clear();
    }
    
    effect(fn) {
        if (!this.mounted) return;
        
        const watcher = new Watcher(fn, () => {
            if (this.mounted) {
                this.update();
            }
        });
        
        this.effects.add(watcher);
        return () => {
            watcher.stop();
            this.effects.delete(watcher);
        };
    }
    
    update() {
        // 实现组件更新逻辑
    }
}

最佳实践建议 ????

  1. 响应式设计模式
// 1. 单向数据流
class UnidirectionalStore {
    constructor(initialState) {
        this.state = ReactiveProxy.create(initialState);
        this.mutations = new Map();
        this.actions = new Map();
    }
    
    commit(type, payload) {
        const mutation = this.mutations.get(type);
        if (mutation) {
            mutation(this.state, payload);
        }
    }
    
    dispatch(type, payload) {
        const action = this.actions.get(type);
        if (action) {
            action(this, payload);
        }
    }
    
    registerMutation(type, handler) {
        this.mutations.set