好吧,事实证明它很简单,但并不简单,所以我将在这里发布我找到的解决方案。
我想要做的是在Zone.JS 从浏览器的本机对象中修补地狱之前document.addEventListener
在全局对象中保存一个可用的对象。
这必须在加载 polyfills 之前完成,因为 Zone 是在其中加载的。并且,它不能在 中以纯代码完成polyfills.ts
,因为import
语句在任何其他代码之前处理。
所以我需要zone-config.ts
在同一文件夹中的文件polyfills.ts
。在后者中,需要额外的导入:
import './zone-config'; // <- adding this line to the file, before...
// Zone JS is required by default for Angular itself.
import 'zone.js/dist/zone'; // Included with Angular CLI.
在zone-config.ts
我做花招:
(function(global) {
// Save native handlers in the window.unpatched object.
if (global.unpatched) return;
if (global.Zone) {
throw Error('Zone already running: cannot access native listeners');
}
global.unpatched = {
windowAddEventListener: window.addEventListener.bind(window),
windowRemoveEventListener: window.removeEventListener.bind(window),
documentAddEventListener: document.addEventListener.bind(document),
documentRemoveEventListener: document.removeEventListener.bind(document)
};
// Disable Zone.JS patching of WebSocket -- UNRELATED TO THIS QUESTION
const propsArray = global.__Zone_ignore_on_properties || (global.__Zone_ignore_on_properties = []);
propsArray.push({ target: WebSocket.prototype, ignoreProperties: ['close', 'error', 'open', 'message'] });
// disable addEventListener
const evtsArray = global.__zone_symbol__BLACK_LISTED_EVENTS || (global.__zone_symbol__BLACK_LISTED_EVENTS = []);
evtsArray.push('message');
})(<any>window);
现在我有一个window.unpatched
可用的对象,它允许我完全退出 Zone.JS,用于非常具体的任务,比如IdleService
我正在处理的任务:
import { Injectable, NgZone } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';
/* This is the object initialized in zone-config.ts */
const unpatched: {
windowAddEventListener: typeof window.addEventListener;
windowRemoveEventListener: typeof window.removeEventListener;
documentAddEventListener: typeof document.addEventListener;
documentRemoveEventListener: typeof document.removeEventListener;
} = window['unpatched'];
/** Manages event handlers used to detect User activity on the page,
* either via mouse or keyboard events. */
@Injectable({
providedIn: 'root'
})
export class IdleService {
private _idleForSecond$ = new BehaviorSubject<number>(0);
constructor(zone: NgZone) {
const timerCallback = (idleFromSeconds: number): void => this._idleForSecond$.next(idleFromSeconds);
this.registerIdleCallback(timerCallback);
}
private registerIdleCallback(callback: (idleFromSeconds: number) => void) {
let idleFromSeconds = 0;
const resetCounter = () => (idleFromSeconds = -1);
// runs entirely out of Zone.JS
unpatched.documentAddEventListener('mousemove', resetCounter);
unpatched.documentAddEventListener('keypress', resetCounter);
unpatched.documentAddEventListener('touchstart', resetCounter);
// runs in the Zone normally
window.setInterval(() => callback(++idleFromSeconds), 1000);
}
/** Returns an observable that emits when the user's inactivity time
* surpasses a certain threshold.
*
* @param seconds The minimum inactivity time in seconds.
*/
byAtLeast(seconds: number): Observable<number> {
return this._idleForSecond$.pipe(filter(idleTime => idleTime >= seconds));
}
/** Returns an observable that emits every second the number of seconds since the
* ladt user's activity.
*/
by(): Observable<number> {
return this._idleForSecond$.asObservable();
}
}
现在,回调中的 stackTrace(idleFromSeconds = -1)
根据需要为空。
希望这可以对其他人有所帮助:这并不复杂,但是将所有部分都准备好有点麻烦。