Skip to content

Commit

Permalink
chore: move zone wait function to utils folder
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Jan 29, 2023
1 parent 09ba6e7 commit fa9faf8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
16 changes: 5 additions & 11 deletions packages/content/src/lib/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Observable, of } from 'rxjs';

import { ContentFile } from './content-file';
import { CONTENT_FILES_TOKEN } from './content-files-token';
import { waitFor } from './utils/zone-wait-for';

/**
* Retrieves the static content using the provided param
Expand Down Expand Up @@ -38,21 +39,14 @@ export function injectContent<
}

return new Promise<string>((resolve) => {
const contentResolver = contentFile();

if (import.meta.env.SSR === true) {
const macroTask = (globalThis as any)[
'Zone'
].current.scheduleMacroTask(
`AnalogResolveContent-${Math.random()}`,
() => {},
{},
() => {}
);
contentFile().then((content) => {
macroTask.invoke();
waitFor(contentResolver).then((content) => {
resolve(content);
});
} else {
contentFile().then((content) => {
contentResolver.then((content) => {
resolve(content);
});
}
Expand Down
14 changes: 2 additions & 12 deletions packages/content/src/lib/markdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { catchError, map, mergeMap, switchMap } from 'rxjs/operators';

import { ContentRenderer } from './content-renderer';
import { AnchorNavigationDirective } from './anchor-navigation.directive';
import { waitFor } from './utils/zone-wait-for';

@Component({
selector: 'analog-markdown',
Expand Down Expand Up @@ -53,18 +54,7 @@ export default class AnalogMarkdownComponent
return of(this.content);
} else {
if (import.meta.env.SSR === true) {
const macroTask = (globalThis as any)[
'Zone'
].current.scheduleMacroTask(
`AnalogResolveMarkdown-${Math.random()}`,
() => {},
{},
() => {}
);
return contentResolver().then((content) => {
macroTask.invoke();
return content;
});
return waitFor(contentResolver());
} else {
return contentResolver();
}
Expand Down
19 changes: 19 additions & 0 deletions packages/content/src/lib/utils/zone-wait-for.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { firstValueFrom, isObservable, Observable } from 'rxjs';

declare const Zone: any;

export async function waitFor<T>(prom: Promise<T> | Observable<T>): Promise<T> {
if (isObservable(prom)) {
prom = firstValueFrom(prom);
}
const macroTask = Zone.current.scheduleMacroTask(
`AnalogContentResolve-${Math.random()}`,
() => {},
{},
() => {}
);
return prom.then((p: T) => {
macroTask.invoke();
return p;
});
}

0 comments on commit fa9faf8

Please sign in to comment.