Skip to content

feat(zone.js): patch Utils threading functions #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/angular/src/lib/nativescript-ng-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class NativeScriptNgZone implements NgZone {
}

Zone.assertZonePatched();
const self = (this as any) as NgZonePrivate;
const self = this as any as NgZonePrivate;
self._nesting = 0;

self._outer = self._inner = Zone.current;
Expand All @@ -149,7 +149,8 @@ export class NativeScriptNgZone implements NgZone {
self.shouldCoalesceRunChangeDetection = shouldCoalesceRunChangeDetection;
self.lastRequestAnimationFrameId = -1;
self.nativeRequestAnimationFrame = function (cb) {
Utils.dispatchToMainThread(cb);
const nativeDispatchToMainThread = Utils[Zone.__symbol__('dispatchToMainThread')] || Utils.dispatchToMainThread;
nativeDispatchToMainThread(cb);
return currentRafId++;
};
forkInnerZoneWithAngularBehavior(self);
Expand Down Expand Up @@ -184,7 +185,7 @@ export class NativeScriptNgZone implements NgZone {
* If a synchronous error happens it will be rethrown and not reported via `onError`.
*/
run<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T {
return ((this as any) as NgZonePrivate)._inner.run(fn, applyThis, applyArgs);
return (this as any as NgZonePrivate)._inner.run(fn, applyThis, applyArgs);
}

/**
Expand All @@ -200,7 +201,7 @@ export class NativeScriptNgZone implements NgZone {
* If a synchronous error happens it will be rethrown and not reported via `onError`.
*/
runTask<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[], name?: string): T {
const zone = ((this as any) as NgZonePrivate)._inner;
const zone = (this as any as NgZonePrivate)._inner;
const task = zone.scheduleEventTask('NgZoneEvent: ' + name, fn, EMPTY_PAYLOAD, noop, noop);
try {
return zone.runTask(task, applyThis, applyArgs);
Expand All @@ -214,7 +215,7 @@ export class NativeScriptNgZone implements NgZone {
* rethrown.
*/
runGuarded<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T {
return ((this as any) as NgZonePrivate)._inner.runGuarded(fn, applyThis, applyArgs);
return (this as any as NgZonePrivate)._inner.runGuarded(fn, applyThis, applyArgs);
}

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

Expand Down
41 changes: 40 additions & 1 deletion packages/zone-js/dist/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */
import './core';
import { Observable, View } from '@nativescript/core';
import { Observable, View, Utils } from '@nativescript/core';

Zone.__load_patch('nativescript_observable_events', (g, z, api: any) => {
api.patchNativeScriptEventTarget(g, api, [Observable, Observable.prototype, View, View.prototype]);
Expand All @@ -9,3 +9,42 @@ Zone.__load_patch('nativescript_observable_events', (g, z, api: any) => {
Zone.__load_patch('nativescript_xhr_events', (g, z, api: any) => {
api.patchNativeScriptEventTarget(g, api, [XMLHttpRequest.prototype]);
});

// We're patching the Utils object instead of the actual js module
Zone.__load_patch('nativescript_mainThreadify', (global, zone, api) => {
api.patchMethod(
Utils,
'mainThreadify',
(delegate, delegateName, name) =>
function (self, args) {
const callback = args[0];
return delegate.apply(self, [Zone.current.wrap(callback, 'NS mainThreadify patch')]);
}
);
});

Zone.__load_patch('nativescript_executeOnMainThread', (global, zone, api) => {
api.patchMethod(
Utils,
'executeOnMainThread',
(delegate, delegateName, name) =>
function (self, args) {
const callback = args[0];
return delegate.apply(self, [Zone.current.wrap(callback, 'NS executeOnMainThread patch')]);
}
);
});

Zone.__load_patch('nativescript_dispatchToMainThread', (global, zone, api) => {
api.patchMethod(
Utils,
'dispatchToMainThread',
(delegate, delegateName, name) =>
function (self, args) {
const callback = args[0];
return delegate.apply(self, [Zone.current.wrap(callback, 'NS dispatchToMainThread patch')]);
}
);
});

//! 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.