Skip to content

Commit

Permalink
add 5.js
Browse files Browse the repository at this point in the history
  • Loading branch information
answershuto committed Jan 24, 2018
1 parent d927707 commit 5bdba10
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions 5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
let uid = 0;

class Watcher {
constructor () {
this.id = ++uid;
}

update () {
console.log('watch' + this.id + ' update');
queueWatcher(this);
}

run () {
console.log('watch' + this.id + '视图更新啦~');
}
}

let callbacks = [];
let pending = false;

function flushCallbacks () {
pending = false;
const copies = callbacks.slice(0);
callbacks.length = 0;
for (let i = 0; i < copies.length; i++) {
copies[i]();
}
}

function nextTick (cb) {
callbacks.push(cb);

if (!pending) {
pending = true;
setTimeout(flushCallbacks, 0);
}
}

let has = {};
let queue = [];
let waiting = false;

function flushSchedulerQueue () {
let watcher, id;

for (index = 0; index < queue.length; index++) {
watcher = queue[index]
id = watcher.id;
has[id] = null;
watcher.run();
}

waiting = false;
}

function queueWatcher(watcher) {
const id = watcher.id;
if (has[id] == null) {
has[id] = true;
queue.push(watcher);

if (!waiting) {
waiting = true;
nextTick(flushSchedulerQueue);
}
}
}

(function () {
let watch1 = new Watcher();
let watch2 = new Watcher();

watch1.update();
watch1.update();
watch2.update();
})();

0 comments on commit 5bdba10

Please sign in to comment.