JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<script src="https://unpkg.com/[email protected]/bundles/zone.umd.js"></script>
</html>
JavaScript
const rootZone = Zone.current;
// Create two zones (thread contexts, basically)
const zone1 = rootZone.fork({
name: 'zone1',
properties: { pizzaInfo: { topping: 'pepperoni' } },
});
const zone2 = rootZone.fork({
name: 'zone2',
properties: { pizzaInfo: { topping: 'pineapple' } },
});
// A function which will trigger some asynchronous work
// Doesn't know anything about zones, but magically the
// work will run in whatever zone launched it.
function someWork() {
setTimeout(someAsyncWork, 1000);
}
// Some asynchronous work
function someAsyncWork() {
const initialZone = Zone.current;
const pizzaInfo = initialZone.get('pizzaInfo');
console.log(`someAsyncWork: in zone ${initialZone.name}`);
console.log(
`someAsyncWork: best pizza topping for this zone is ${pizzaInfo.topping}`
);
// Have some async fun with promises and timers
delay(500).then(yetMoreWork);
}
function yetMoreWork() {
const zone = Zone.current;
console.log(`yetMoreWork: in zone ${zone.name}`);
}
function delay(millis) {
return new Promise((resolve) => setTimeout(resolve, millis));
}
zone1.run(someWork);
zone2.run(someWork);