From c7342bec243e73088462bdb4e00b3d9dd5f04424 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 24 Oct 2024 08:33:04 +0200 Subject: [PATCH] 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 --- packages/core/rxjs-interop/src/rx_resource.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/core/rxjs-interop/src/rx_resource.ts b/packages/core/rxjs-interop/src/rx_resource.ts index 3e307d34ef9..da509c3accf 100644 --- a/packages/core/rxjs-interop/src/rx_resource.ts +++ b/packages/core/rxjs-interop/src/rx_resource.ts @@ -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(opts: RxResourceOptions): ResourceRef loader: (params) => { const cancelled = new Subject(); 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((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')), + }); + }); }, }); }