Created
June 27, 2020 11:12
-
-
Save xingoxu/58f8f7faa83732ec922fcd38299f3283 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Zone = { | |
_currentZone: {}, | |
get current() { | |
return { | |
...this._currentZone, | |
fork: (zone) => { | |
this._currentZone = { | |
...this._currentZone, | |
...zone, | |
}; | |
return this; | |
}, | |
set: (key, value) => { | |
this._currentZone[key] = value; | |
}, | |
}; | |
}, | |
}; | |
(() => { | |
const _setTimeout = global.setTimeout; | |
global.setTimeout = (cb, timeout, ...args) => { | |
const _currentZone = Zone._currentZone; | |
_setTimeout(() => { | |
const __after = Zone._currentZone; | |
Zone._currentZone = _currentZone; | |
cb(...args); | |
Zone._currentZone = __after; | |
}, timeout); | |
}; | |
})(); | |
for (let i = 0; i < 10; i++) { | |
const value = Math.floor(Math.random() * 100); | |
console.log(i, value); | |
Zone.current.fork({ i, value }); | |
setTimeout(() => { | |
console.log(Zone.current.i, Zone.current.value); | |
}, value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment