Skip to content

Commit 13b2ea8

Browse files
authored
feat(zone.js): patch Utils threading functions (#31)
1 parent 5560dfd commit 13b2ea8

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

packages/angular/src/lib/nativescript-ng-zone.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class NativeScriptNgZone implements NgZone {
131131
}
132132

133133
Zone.assertZonePatched();
134-
const self = (this as any) as NgZonePrivate;
134+
const self = this as any as NgZonePrivate;
135135
self._nesting = 0;
136136

137137
self._outer = self._inner = Zone.current;
@@ -149,7 +149,8 @@ export class NativeScriptNgZone implements NgZone {
149149
self.shouldCoalesceRunChangeDetection = shouldCoalesceRunChangeDetection;
150150
self.lastRequestAnimationFrameId = -1;
151151
self.nativeRequestAnimationFrame = function (cb) {
152-
Utils.dispatchToMainThread(cb);
152+
const nativeDispatchToMainThread = Utils[Zone.__symbol__('dispatchToMainThread')] || Utils.dispatchToMainThread;
153+
nativeDispatchToMainThread(cb);
153154
return currentRafId++;
154155
};
155156
forkInnerZoneWithAngularBehavior(self);
@@ -184,7 +185,7 @@ export class NativeScriptNgZone implements NgZone {
184185
* If a synchronous error happens it will be rethrown and not reported via `onError`.
185186
*/
186187
run<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T {
187-
return ((this as any) as NgZonePrivate)._inner.run(fn, applyThis, applyArgs);
188+
return (this as any as NgZonePrivate)._inner.run(fn, applyThis, applyArgs);
188189
}
189190

190191
/**
@@ -200,7 +201,7 @@ export class NativeScriptNgZone implements NgZone {
200201
* If a synchronous error happens it will be rethrown and not reported via `onError`.
201202
*/
202203
runTask<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[], name?: string): T {
203-
const zone = ((this as any) as NgZonePrivate)._inner;
204+
const zone = (this as any as NgZonePrivate)._inner;
204205
const task = zone.scheduleEventTask('NgZoneEvent: ' + name, fn, EMPTY_PAYLOAD, noop, noop);
205206
try {
206207
return zone.runTask(task, applyThis, applyArgs);
@@ -214,7 +215,7 @@ export class NativeScriptNgZone implements NgZone {
214215
* rethrown.
215216
*/
216217
runGuarded<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T {
217-
return ((this as any) as NgZonePrivate)._inner.runGuarded(fn, applyThis, applyArgs);
218+
return (this as any as NgZonePrivate)._inner.runGuarded(fn, applyThis, applyArgs);
218219
}
219220

220221
/**
@@ -231,7 +232,7 @@ export class NativeScriptNgZone implements NgZone {
231232
* Use {@link #run} to reenter the Angular zone and do work that updates the application model.
232233
*/
233234
runOutsideAngular<T>(fn: (...args: any[]) => T): T {
234-
return ((this as any) as NgZonePrivate)._outer.run(fn);
235+
return (this as any as NgZonePrivate)._outer.run(fn);
235236
}
236237
}
237238

packages/zone-js/dist/events.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22
import './core';
3-
import { Observable, View } from '@nativescript/core';
3+
import { Observable, View, Utils } from '@nativescript/core';
44

55
Zone.__load_patch('nativescript_observable_events', (g, z, api: any) => {
66
api.patchNativeScriptEventTarget(g, api, [Observable, Observable.prototype, View, View.prototype]);
@@ -9,3 +9,42 @@ Zone.__load_patch('nativescript_observable_events', (g, z, api: any) => {
99
Zone.__load_patch('nativescript_xhr_events', (g, z, api: any) => {
1010
api.patchNativeScriptEventTarget(g, api, [XMLHttpRequest.prototype]);
1111
});
12+
13+
// We're patching the Utils object instead of the actual js module
14+
Zone.__load_patch('nativescript_mainThreadify', (global, zone, api) => {
15+
api.patchMethod(
16+
Utils,
17+
'mainThreadify',
18+
(delegate, delegateName, name) =>
19+
function (self, args) {
20+
const callback = args[0];
21+
return delegate.apply(self, [Zone.current.wrap(callback, 'NS mainThreadify patch')]);
22+
}
23+
);
24+
});
25+
26+
Zone.__load_patch('nativescript_executeOnMainThread', (global, zone, api) => {
27+
api.patchMethod(
28+
Utils,
29+
'executeOnMainThread',
30+
(delegate, delegateName, name) =>
31+
function (self, args) {
32+
const callback = args[0];
33+
return delegate.apply(self, [Zone.current.wrap(callback, 'NS executeOnMainThread patch')]);
34+
}
35+
);
36+
});
37+
38+
Zone.__load_patch('nativescript_dispatchToMainThread', (global, zone, api) => {
39+
api.patchMethod(
40+
Utils,
41+
'dispatchToMainThread',
42+
(delegate, delegateName, name) =>
43+
function (self, args) {
44+
const callback = args[0];
45+
return delegate.apply(self, [Zone.current.wrap(callback, 'NS dispatchToMainThread patch')]);
46+
}
47+
);
48+
});
49+
50+
//! queueMacroTask should never be patched! We should consider it as a low level API to queue macroTasks which will be patched separately by other patches.

0 commit comments

Comments
 (0)