Skip to content

Commit 19d3a34

Browse files
committed
format zones.ts
1 parent 3639e41 commit 19d3a34

File tree

1 file changed

+78
-40
lines changed

1 file changed

+78
-40
lines changed

src/zones.ts

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
2-
import { Injectable, NgZone } from '@angular/core';
2+
import { Injectable, NgZone } from "@angular/core";
33
import {
44
Observable,
55
Operator,
@@ -9,30 +9,32 @@ import {
99
Subscription,
1010
TeardownLogic,
1111
asyncScheduler,
12-
queueScheduler
13-
} from 'rxjs';
14-
import { observeOn, subscribeOn, tap } from 'rxjs/operators';
12+
queueScheduler,
13+
} from "rxjs";
14+
import { observeOn, subscribeOn, tap } from "rxjs/operators";
1515

1616
// eslint-disable-next-line @typescript-eslint/no-empty-function
17-
function noop() {
18-
}
17+
function noop() {}
1918

2019
/**
2120
* Schedules tasks so that they are invoked inside the Zone that is passed in the constructor.
2221
*/
2322
export class ɵZoneScheduler implements SchedulerLike {
24-
constructor(private zone: any, private delegate: any = queueScheduler) {
25-
}
23+
constructor(private zone: any, private delegate: any = queueScheduler) {}
2624

2725
now() {
2826
return this.delegate.now();
2927
}
3028

31-
schedule(work: (this: SchedulerAction<any>, state?: any) => void, delay?: number, state?: any): Subscription {
29+
schedule(
30+
work: (this: SchedulerAction<any>, state?: any) => void,
31+
delay?: number,
32+
state?: any
33+
): Subscription {
3234
const targetZone = this.zone;
3335
// Wrap the specified work function to make sure that if nested scheduling takes place the
3436
// work is executed in the correct zone
35-
const workInZone = function(this: SchedulerAction<any>, state: any) {
37+
const workInZone = function (this: SchedulerAction<any>, state: any) {
3638
targetZone.runGuarded(() => {
3739
work.apply(this, [state]);
3840
});
@@ -49,24 +51,32 @@ class BlockUntilFirstOperator<T> implements Operator<T, T> {
4951
// @ts-ignore
5052
private task: MacroTask | null = null;
5153

52-
constructor(private zone: any) {
53-
}
54+
constructor(private zone: any) {}
5455

5556
call(subscriber: Subscriber<T>, source: Observable<T>): TeardownLogic {
5657
const unscheduleTask = this.unscheduleTask.bind(this);
5758
// @ts-ignore
58-
this.task = this.zone.run(() => Zone.current.scheduleMacroTask('firebaseZoneBlock', noop, {}, noop, noop));
59+
this.task = this.zone.run(() =>
60+
Zone.current.scheduleMacroTask("firebaseZoneBlock", noop, {}, noop, noop)
61+
);
5962

60-
return source.pipe(
61-
tap({ next: unscheduleTask, complete: unscheduleTask, error: unscheduleTask })
62-
).subscribe(subscriber).add(unscheduleTask);
63+
return source
64+
.pipe(
65+
tap({
66+
next: unscheduleTask,
67+
complete: unscheduleTask,
68+
error: unscheduleTask,
69+
})
70+
)
71+
.subscribe(subscriber)
72+
.add(unscheduleTask);
6373
}
6474

6575
private unscheduleTask() {
6676
// maybe this is a race condition, invoke in a timeout
6777
// hold for 10ms while I try to figure out what is going on
6878
setTimeout(() => {
69-
if (this.task != null && this.task.state === 'scheduled') {
79+
if (this.task != null && this.task.state === "scheduled") {
7080
this.task.invoke();
7181
this.task = null;
7282
}
@@ -75,27 +85,34 @@ class BlockUntilFirstOperator<T> implements Operator<T, T> {
7585
}
7686

7787
@Injectable({
78-
providedIn: 'root',
88+
providedIn: "root",
7989
})
8090
export class ɵAngularFireSchedulers {
8191
public readonly outsideAngular: ɵZoneScheduler;
8292
public readonly insideAngular: ɵZoneScheduler;
8393

8494
constructor(public ngZone: NgZone) {
8595
// @ts-ignore
86-
this.outsideAngular = ngZone.runOutsideAngular(() => new ɵZoneScheduler(Zone.current));
96+
this.outsideAngular = ngZone.runOutsideAngular(
97+
() => new ɵZoneScheduler(Zone.current)
98+
);
8799
// @ts-ignore
88-
this.insideAngular = ngZone.run(() => new ɵZoneScheduler(Zone.current, asyncScheduler));
100+
this.insideAngular = ngZone.run(
101+
() => new ɵZoneScheduler(Zone.current, asyncScheduler)
102+
);
89103
globalThis.ɵAngularFireScheduler ||= this;
90104
}
91105
}
92106

93107
function getSchedulers() {
94-
const schedulers = globalThis.ɵAngularFireScheduler as ɵAngularFireSchedulers|undefined;
108+
const schedulers = globalThis.ɵAngularFireScheduler as
109+
| ɵAngularFireSchedulers
110+
| undefined;
95111
if (!schedulers) {
96112
throw new Error(
97-
`Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using
98-
provideFirebaseApp) or you're calling an AngularFire method outside of an NgModule (which is not supported).`);
113+
`Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using
114+
provideFirebaseApp) or you're calling an AngularFire method outside of an NgModule (which is not supported).`
115+
);
99116
}
100117
return schedulers;
101118
}
@@ -126,11 +143,13 @@ export function keepUnstableUntilFirst<T>(obs$: Observable<T>): Observable<T> {
126143
* value from firebase but doesn't block the zone forever since the firebase subscription
127144
* is still alive.
128145
*/
129-
export function ɵkeepUnstableUntilFirstFactory(schedulers: ɵAngularFireSchedulers) {
130-
return function keepUnstableUntilFirst<T>(obs$: Observable<T>): Observable<T> {
131-
obs$ = obs$.lift(
132-
new BlockUntilFirstOperator(schedulers.ngZone)
133-
);
146+
export function ɵkeepUnstableUntilFirstFactory(
147+
schedulers: ɵAngularFireSchedulers
148+
) {
149+
return function keepUnstableUntilFirst<T>(
150+
obs$: Observable<T>
151+
): Observable<T> {
152+
obs$ = obs$.lift(new BlockUntilFirstOperator(schedulers.ngZone));
134153

135154
return obs$.pipe(
136155
// Run the subscribe body outside of Angular (e.g. calling Firebase SDK to add a listener to a change event)
@@ -144,15 +163,18 @@ export function ɵkeepUnstableUntilFirstFactory(schedulers: ɵAngularFireSchedul
144163
}
145164

146165
// @ts-ignore
147-
const zoneWrapFn = (it: (...args: any[]) => any, macrotask: MacroTask|undefined) => {
166+
const zoneWrapFn = (
167+
it: (...args: any[]) => any,
168+
macrotask: MacroTask | undefined
169+
) => {
148170
// eslint-disable-next-line @typescript-eslint/no-this-alias
149171
const _this = this;
150172
// function() is needed for the arguments object
151-
return function() {
173+
return function () {
152174
const _arguments = arguments;
153175
if (macrotask) {
154176
setTimeout(() => {
155-
if (macrotask.state === 'scheduled') {
177+
if (macrotask.state === "scheduled") {
156178
macrotask.invoke();
157179
}
158180
}, 10);
@@ -161,19 +183,27 @@ const zoneWrapFn = (it: (...args: any[]) => any, macrotask: MacroTask|undefined)
161183
};
162184
};
163185

164-
export const ɵzoneWrap = <T= unknown>(it: T, blockUntilFirst: boolean): T => {
186+
export const ɵzoneWrap = <T = unknown>(it: T, blockUntilFirst: boolean): T => {
165187
// function() is needed for the arguments object
166-
return function() {
188+
return function () {
167189
// @ts-ignore
168190
let macrotask: MacroTask | undefined;
169191
const _arguments = arguments;
170192
// if this is a callback function, e.g, onSnapshot, we should create a microtask and invoke it
171193
// only once one of the callback functions is tripped.
172194
for (let i = 0; i < arguments.length; i++) {
173-
if (typeof _arguments[i] === 'function') {
195+
if (typeof _arguments[i] === "function") {
174196
if (blockUntilFirst) {
175197
// @ts-ignore
176-
macrotask ||= run(() => Zone.current.scheduleMacroTask('firebaseZoneBlock', noop, {}, noop, noop));
198+
macrotask ||= run(() =>
199+
Zone.current.scheduleMacroTask(
200+
"firebaseZoneBlock",
201+
noop,
202+
{},
203+
noop,
204+
noop
205+
)
206+
);
177207
}
178208
// TODO create a microtask to track callback functions
179209
_arguments[i] = zoneWrapFn(_arguments[i], macrotask);
@@ -185,7 +215,7 @@ export const ɵzoneWrap = <T= unknown>(it: T, blockUntilFirst: boolean): T => {
185215
const schedulers = getSchedulers();
186216
return ret.pipe(
187217
subscribeOn(schedulers.outsideAngular),
188-
observeOn(schedulers.insideAngular),
218+
observeOn(schedulers.insideAngular)
189219
);
190220
} else {
191221
return run(() => ret);
@@ -195,13 +225,21 @@ export const ɵzoneWrap = <T= unknown>(it: T, blockUntilFirst: boolean): T => {
195225
return ret.pipe(keepUnstableUntilFirst) as any;
196226
} else if (ret instanceof Promise) {
197227
// eslint-disable-next-line @typescript-eslint/no-misused-promises
198-
return run(() => new Promise((resolve, reject) => ret.then(it => run(() => resolve(it)), reason => run(() => reject(reason)))));
199-
} else if (typeof ret === 'function' && macrotask) {
228+
return run(
229+
() =>
230+
new Promise((resolve, reject) =>
231+
ret.then(
232+
(it) => run(() => resolve(it)),
233+
(reason) => run(() => reject(reason))
234+
)
235+
)
236+
);
237+
} else if (typeof ret === "function" && macrotask) {
200238
// Handle unsubscribe
201239
// function() is needed for the arguments object
202-
return function() {
240+
return function () {
203241
setTimeout(() => {
204-
if (macrotask && macrotask.state === 'scheduled') {
242+
if (macrotask && macrotask.state === "scheduled") {
205243
macrotask.invoke();
206244
}
207245
}, 10);

0 commit comments

Comments
 (0)