fix(core): avoid breaking change with apps using rxjs 6.x (#58341)

The `rxResource` was using `firstValueFrom` which isn't supported in rxjs 6.x. `@angular/core` currently supports rxjs 6 so we need this to be backwards compatible. This came up when trying to deploy the Material docs site which is still on rxjs 6 ([see](https://github.com/angular/components/actions/runs/11487971079/job/31973721563)).

PR Close #58341
This commit is contained in:
Kristiyan Kostadinov 2024-10-24 08:33:04 +02:00 committed by Alex Rickabaugh
parent 8af71c05b2
commit c7342bec24

View file

@ -13,8 +13,8 @@ import {
ResourceLoaderParams,
ResourceRef,
} from '@angular/core';
import {firstValueFrom, Observable, Subject} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {Observable, Subject} from 'rxjs';
import {take, takeUntil} from 'rxjs/operators';
/**
* Like `ResourceOptions` but uses an RxJS-based `loader`.
@ -38,7 +38,19 @@ export function rxResource<T, R>(opts: RxResourceOptions<T, R>): ResourceRef<T>
loader: (params) => {
const cancelled = new Subject<void>();
params.abortSignal.addEventListener('abort', () => cancelled.next());
return firstValueFrom(opts.loader(params).pipe(takeUntil(cancelled)));
// Note: this is identical to `firstValueFrom` which we can't use,
// because at the time of writing, `core` still supports rxjs 6.x.
return new Promise<T>((resolve, reject) => {
opts
.loader(params)
.pipe(take(1), takeUntil(cancelled))
.subscribe({
next: resolve,
error: reject,
complete: () => reject(new Error('Resource completed before producing a value')),
});
});
},
});
}