|
1 | 1 | /* eslint-disable @typescript-eslint/ban-ts-comment */
|
2 | 2 | import {
|
3 | 3 | Injectable,
|
| 4 | +Injector, |
4 | 5 | NgZone,
|
5 | 6 | PendingTasks,
|
6 | 7 | inject
|
@@ -97,7 +98,7 @@ const zoneWrapFn = (
|
97 | 98 | ) => {
|
98 | 99 | return (...args: any[]) => {
|
99 | 100 | if (taskDone) {
|
100 |
| -setTimeout(taskDone, 10); |
| 101 | +setTimeout(taskDone, 0); |
101 | 102 | }
|
102 | 103 | return run(() => it.apply(this, args));
|
103 | 104 | };
|
@@ -108,12 +109,22 @@ export const ɵzoneWrap = <T= unknown>(it: T, blockUntilFirst: boolean): T => {
|
108 | 109 | return function () {
|
109 | 110 | let taskDone: VoidFunction | undefined;
|
110 | 111 | const _arguments = arguments;
|
| 112 | +let schedulers: ɵAngularFireSchedulers; |
| 113 | +let pendingTasks: PendingTasks; |
| 114 | +let injector: Injector; |
| 115 | +try { |
| 116 | +schedulers = getSchedulers(); |
| 117 | +pendingTasks = inject(PendingTasks); |
| 118 | +injector = inject(Injector); |
| 119 | +} catch(e) { |
| 120 | +return (it as any).apply(this, _arguments); |
| 121 | +} |
111 | 122 | // if this is a callback function, e.g, onSnapshot, we should create a pending task and complete it
|
112 | 123 | // only once one of the callback functions is tripped.
|
113 | 124 | for (let i = 0; i < arguments.length; i++) {
|
114 | 125 | if (typeof _arguments[i] === 'function') {
|
115 | 126 | if (blockUntilFirst) {
|
116 |
| -taskDone ||= run(() => inject(PendingTasks).add()); |
| 127 | +taskDone ||= run(() => pendingTasks.add()); |
117 | 128 | }
|
118 | 129 | // TODO create a microtask to track callback functions
|
119 | 130 | _arguments[i] = zoneWrapFn(_arguments[i], taskDone);
|
@@ -122,7 +133,6 @@ export const ɵzoneWrap = <T= unknown>(it: T, blockUntilFirst: boolean): T => {
|
122 | 133 | const ret = runOutsideAngular(() => (it as any).apply(this, _arguments));
|
123 | 134 | if (!blockUntilFirst) {
|
124 | 135 | if (ret instanceof Observable) {
|
125 |
| -const schedulers = getSchedulers(); |
126 | 136 | return ret.pipe(
|
127 | 137 | subscribeOn(schedulers.outsideAngular),
|
128 | 138 | observeOn(schedulers.insideAngular),
|
@@ -132,18 +142,17 @@ export const ɵzoneWrap = <T= unknown>(it: T, blockUntilFirst: boolean): T => {
|
132 | 142 | }
|
133 | 143 | }
|
134 | 144 | if (ret instanceof Observable) {
|
135 |
| -const schedulers = getSchedulers(); |
136 | 145 | return ret.pipe(
|
137 | 146 | subscribeOn(schedulers.outsideAngular),
|
138 | 147 | observeOn(schedulers.insideAngular),
|
139 |
| -pendingUntilEvent(), |
| 148 | +pendingUntilEvent(injector), |
140 | 149 | );
|
141 | 150 | } else if (ret instanceof Promise) {
|
142 | 151 | // eslint-disable-next-line @typescript-eslint/no-misused-promises
|
143 | 152 | return run(
|
144 | 153 | () =>
|
145 | 154 | new Promise((resolve, reject) => {
|
146 |
| -inject(PendingTasks).run(() => ret).then( |
| 155 | +pendingTasks.run(() => ret).then( |
147 | 156 | (it) => run(() => resolve(it)),
|
148 | 157 | (reason) => run(() => reject(reason))
|
149 | 158 | );
|
@@ -153,7 +162,7 @@ export const ɵzoneWrap = <T= unknown>(it: T, blockUntilFirst: boolean): T => {
|
153 | 162 | // Handle unsubscribe
|
154 | 163 | // function() is needed for the arguments object
|
155 | 164 | return function () {
|
156 |
| -setTimeout(taskDone, 10); |
| 165 | +setTimeout(taskDone, 0); |
157 | 166 | return ret.apply(this, arguments);
|
158 | 167 | };
|
159 | 168 | } else {
|
|
0 commit comments