4. Zone.jsとは
1. Brian Ford さんが2013年DartのZoneのIdeaから作成したライブラリ
a. Provide an execution Context
b. Provide async task lifecycle hook
c. Provide async error handler
5. Sample Code: Execution Context
Zone.current.fork({
name: 'zone',
properties: {
key: 'value'
}
}).run(() => {
setTimeout(() => {
console.log('in zone', Zone.current.name, Zone.current.get('key')); // will print `in zone zone value
});
})
11. 過去一年のZone.js
1. Performance
2. Better Promise support
3. More API support
4. Better Rxjs Support
5. Better Error handling
6. Module system
7. Closure Build Support
12. Performance
● GC is the top bottleneck
● Zone.js
○ DOM
■ addEventListener/removeEventListener/onXXX(onClick etc..)
■ BlackListEvent Support (scroll, mousemove…) , __zone_symbol__BLACK_LISTED_EVENTS
○ NodeJS
■ EventEmitter
Reduce objects, especially short live objects. Remove all closures below.
const wrappedCallback = () => {zone.run(eventListener)};
※ Angular DomEvent use more aggressive way to improve performance. It is under testing.
13. Promise
● Promise A+ Spec Passed
● ZoneAwarePromise load order improvment
○ can load es6/core-js/other 3rdParty promise before/after zone.js without reporting
Zone.js has detected that ZoneAwarePromise `(window|global).Promise` has been
overwritten
’
14. More API support
1. Nw/Electron
2. MediaQuery
3. Notification
4. RTCPeerConnction
5. ShadyDom
6. Cordova
15. Better Rxjs zone support
// constructor zone.
zoneA.run(() => { observable = new Observable((_subscribe) => subscribe = _subscribe;
console.log('constructing:', Zone.current.name);});});
// operator zone
zoneB.run(() => {observable.map(value => {console.log('mapping:',
Zone.current.name);return value;});});
// subscription zone
zoneC.run(() => {observable.subscribe(value => {console.log('subscription:',
Zone.current.name);});});
// emitter zone
zoneD.run(() => {subscribe.next(1);subscribe.complete();});
16. Better Error Handling
1. By default, ZoneAware Error will not loaded ( for Chrome Debugger issue)
2. DevMode, import `zone-error` in polyfill.
a. Every stack frame will have zone information
b. Zone.js stack traces will be removed
at eval (app.component.ts:24)
at ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (core.js:4747)
at ZoneDelegate.invokeTask (zone.js:424)
at Zone.runTask (zone.js:192)
at ZoneTask.invokeTask (zone.js:499)
at ZoneTask.invoke (zone.js:488)
at timer (zone.js:2040)
at eval (app.component.ts:24) [angular]
at Object.onInvokeTask (core.js:4747) [angular]
at timer (zone.js:2040) [<root>]
19. これからのZone.js
● Fix bugs of Angular NgNoopZone
● Better document
○ Module enable/disable flags
○ Built in ZoneSpec(AsyncTest/SyncTest/Proxy/LongStackTrace/WTF…) usage
○ Tips, such as target[Zone.__symbol__(method)] to access non patched method.
● Unify Dom Event Listener behavior
○ <button (click) = “handler();”></button>
○ btn.addEventListener(‘click’, handler);
○ btn.onclick = handler;
○ <button onclick=”handler();”></button>
● Read only zone implementation with Nodejs asynchooks instead of monkey patch
● Native async/await support with asynchooks + PromiseHook
const p1 = new Promise(() => {}); // This will never be resolved
const p2 = new Promise((resolve) => setTimeout(resolve, 1000, p1)); // resolve p2 with p1 after 1s
● Zone TC39 proposal implementation?
● More API support
○ Electron native API
○ Bluebird