Skip to content

Commit b897751

Browse files
committed
Revert "Revert "delete services that implement the next FirebaseService interface in @firebase/component correctly""
This reverts commit 410b926.
1 parent 410b926 commit b897751

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

packages-exp/app-exp/src/api.test.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import {
2929
onLog
3030
} from './api';
3131
import { DEFAULT_ENTRY_NAME } from './constants';
32-
import { _FirebaseAppInternal } from '@firebase/app-types-exp';
32+
import {
33+
_FirebaseAppInternal,
34+
_FirebaseService
35+
} from '@firebase/app-types-exp';
3336
import {
3437
_clearComponents,
3538
_components,
@@ -175,6 +178,33 @@ describe('API tests', () => {
175178
deleteApp(app).catch(() => {});
176179
expect(getApps().length).to.equal(0);
177180
});
181+
182+
it('waits for all services being deleted', async () => {
183+
_clearComponents();
184+
let count = 0;
185+
const comp1 = new Component(
186+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
187+
'test1' as any,
188+
_container =>
189+
({
190+
delete: async () => {
191+
await Promise.resolve();
192+
expect(count).to.equal(0);
193+
count++;
194+
}
195+
} as _FirebaseService),
196+
ComponentType.PUBLIC
197+
);
198+
_registerComponent(comp1);
199+
200+
const app = initializeApp({});
201+
// create service instance
202+
const test1Provider = _getProvider(app, 'test1' as any);
203+
test1Provider.getImmediate();
204+
205+
await deleteApp(app);
206+
expect(count).to.equal(1);
207+
});
178208
});
179209

180210
describe('registerVersion', () => {

packages/component/src/provider.test.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { expect } from 'chai';
1919
import { fake, SinonSpy } from 'sinon';
2020
import { ComponentContainer } from './component_container';
2121
import { FirebaseService } from '@firebase/app-types/private';
22+
import { _FirebaseService } from '@firebase/app-types-exp';
2223
import { Provider } from './provider';
2324
import { getFakeApp, getFakeComponent } from '../test/util';
2425
import '../test/setup';
@@ -202,7 +203,7 @@ describe('Provider', () => {
202203
});
203204

204205
describe('delete()', () => {
205-
it('calls delete() on the service instance that implements FirebaseService', () => {
206+
it('calls delete() on the service instance that implements legacy FirebaseService', () => {
206207
const deleteFake = fake();
207208
const myService: FirebaseService = {
208209
app: getFakeApp(),
@@ -226,6 +227,29 @@ describe('Provider', () => {
226227

227228
expect(deleteFake).to.have.been.called;
228229
});
230+
231+
it('calls delete() on the service instance that implements next FirebaseService', () => {
232+
const deleteFake = fake();
233+
const myService: _FirebaseService = {
234+
app: getFakeApp(),
235+
delete: deleteFake
236+
};
237+
238+
// provide factory and create a service instance
239+
provider.setComponent(
240+
getFakeComponent(
241+
'test',
242+
() => myService,
243+
false,
244+
InstantiationMode.EAGER
245+
)
246+
);
247+
248+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
249+
provider.delete();
250+
251+
expect(deleteFake).to.have.been.called;
252+
});
229253
});
230254

231255
describe('clearCache()', () => {

packages/component/src/provider.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,16 @@ export class Provider<T extends Name> {
170170
async delete(): Promise<void> {
171171
const services = Array.from(this.instances.values());
172172

173-
await Promise.all(
174-
services
175-
.filter(service => 'INTERNAL' in service)
173+
await Promise.all([
174+
...services
175+
.filter(service => 'INTERNAL' in service) // legacy services
176176
// eslint-disable-next-line @typescript-eslint/no-explicit-any
177-
.map(service => (service as any).INTERNAL!.delete())
178-
);
177+
.map(service => (service as any).INTERNAL!.delete()),
178+
...services
179+
.filter(service => 'delete' in service) // next services
180+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
181+
.map(service => (service as any).delete())
182+
]);
179183
}
180184

181185
isComponentSet(): boolean {

0 commit comments

Comments
 (0)