Skip to content

Instantly share code, notes, and snippets.

@xingoxu
Created June 27, 2020 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xingoxu/58f8f7faa83732ec922fcd38299f3283 to your computer and use it in GitHub Desktop.
Save xingoxu/58f8f7faa83732ec922fcd38299f3283 to your computer and use it in GitHub Desktop.
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