Skip to content

Instantly share code, notes, and snippets.

@wilsoncook
Created December 24, 2020 13:20
Show Gist options
  • Save wilsoncook/f8d38ba6cfa9058b66abe32f6229f65c to your computer and use it in GitHub Desktop.
Save wilsoncook/f8d38ba6cfa9058b66abe32f6229f65c to your computer and use it in GitHub Desktop.
全局性的数据流懒缓存,应用场景:部分逻辑代码依赖数据流中的“数据 + 转换逻辑”形成的结果数据,而在使用的时候需要以mutable的形式按需使用(即虽然依赖数据流数据,但该数据变更不会触发对应逻辑重新执行,只有特定的其他依赖会导致其执行),避免频繁转换
import { Store, Unsubscribe } from 'redux';
export class LazyCache<V = any, S = any, M = any> {
/** 最后一次更新并转换后的数据 */
private value: V;
/** 当前selector后的值,用于作变更对比 */
private currentSelected: M | S;
/** 标记对应数据流是否有变更,在使用方使用数据的时候,便可重新生成 */
private needUpdate = true; // 第一次默认生成
private unsubscribe: Unsubscribe;
/**
* 创建一个数据流监听、转换和懒缓存
* @param store 数据流store
* @param translator 数据转换器
* @param selector? 数据流筛选器,为空表示所有数据变更都会更新
*/
constructor(private store: Store<S>, private translator: (snapshot: S) => V, private selector?: (state: S) => M) {
this.unsubscribe = store.subscribe(() => {
const state = store.getState();
const nextSelected = this.selector ? this.selector(state) : state;
if (this.currentSelected !== nextSelected) {
this.currentSelected = nextSelected;
this.needUpdate = true;
}
});
}
/**
* 实时获取转换后的数据
*/
getValue() {
if (this.needUpdate) {
this.needUpdate = false;
this.value = this.translator(this.store.getState());
}
return this.value;
}
destructor() {
this.unsubscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment