Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 6x 6x 6x 21x 21x 21x 21x 21x 21x 21x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 41x 28x 41x 3x 3x 33x 33x 33x 6x 15x 1x 14x 24x 14x 14x 6x |
/*
* @Author: lijianzhang
* @Date: 2018-08-29 22:41:33
* @Last Modified by: lijianzhang
* @Last Modified time: 2018-08-30 01:45:07
*/
import store from "./store";
let id = 0;
export default class Observer {
constructor(cb: Function, name?: string) {
id += 1;
this.id = id;
this.callback = cb;
this.name = name;
store.observers.set(this.id, this);
}
name?: string;
public readonly id: number;
public callback: Function | null;
public bindObservers: Set<Set<number>> = new Set();
private _change = true;
publEic get change() {
return this._change;
}
collectDep(fn: Function) {
this.beginCollectDep();
Econst res = fn();
this.endCollectDep();
return res;
}
beginCollectDep() {
if (this._change) {
this.clearBinds();
store.currentObservers.push(this);
}
}
endCollectDep() {
if (this._change) {
const index = store.currentObservers.indexOf(this);
store.currentObservers.splice(index, 1);
}
this._change = false;
}E
clearBinds() {
this.bindObservers.forEach((observers) => {
observers.delete(this.id);
});
this.bindObservers.clear();
}
unSubscribe() {
this.clearBinds();
this.callback = null;
}
public run() {
this._change = true;
if (this.callback) this.callback(this);
}
}
export function observe(fn: Function, name: string = 'observe') {
if (!fn) throw new Error('you need set callBack');
const ob = new Observer((observer) => {
observer.collectDep(fn);
}, name);
ob.run();
return ob;
}
|