Skip to content

Commit 8c172b8

Browse files
committed
Move identifier to options object for getImmediate()
1 parent 7b8439d commit 8c172b8

File tree

5 files changed

+52
-30
lines changed

5 files changed

+52
-30
lines changed

packages/component/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
export { Component } from './src/component';
1919
export { ComponentContainer } from './src/component_container';
2020
export { Provider } from './src/provider';
21-
export { ComponentType, InstanceFactory } from './src/types';
21+
export { ComponentType, InstanceFactory, InstantiationMode } from './src/types';

packages/component/package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222
"prepare": "yarn build"
2323
},
2424
"dependencies": {
25-
"@firebase/util": "0.2.29",
25+
"@firebase/util": "0.2.31",
2626
"tslib": "1.10.0"
2727
},
2828
"license": "Apache-2.0",
2929
"devDependencies": {
30-
"@types/chai": "4.2.3",
30+
"@types/chai": "4.2.4",
3131
"@types/mocha": "5.2.7",
3232
"@types/sinon": "7.5.0",
3333
"chai": "4.2.0",
3434
"chai-as-promised": "7.1.1",
35-
"karma": "4.3.0",
35+
"karma": "4.4.1",
3636
"sinon": "7.5.0",
3737
"sinon-chai": "3.3.0",
3838
"karma-chrome-launcher": "3.1.0",
@@ -43,19 +43,19 @@
4343
"karma-sauce-launcher": "1.2.0",
4444
"karma-spec-reporter": "0.0.32",
4545
"karma-webpack": "4.0.2",
46-
"mocha": "6.2.1",
46+
"mocha": "6.2.2",
4747
"npm-run-all": "4.1.5",
4848
"nyc": "14.1.1",
49-
"rollup": "1.23.1",
49+
"rollup": "1.25.2",
5050
"rollup-plugin-typescript2": "0.24.3",
51-
"ts-loader": "6.2.0",
51+
"ts-loader": "6.2.1",
5252
"ts-node": "8.4.1",
5353
"typescript": "3.6.4",
54-
"webpack": "4.41.1",
54+
"webpack": "4.41.2",
5555
"eslint": "6.5.1",
56-
"@typescript-eslint/parser": "2.4.0",
57-
"@typescript-eslint/eslint-plugin": "2.4.0",
58-
"@typescript-eslint/eslint-plugin-tslint": "2.4.0",
56+
"@typescript-eslint/parser": "2.5.0",
57+
"@typescript-eslint/eslint-plugin": "2.5.0",
58+
"@typescript-eslint/eslint-plugin-tslint": "2.5.0",
5959
"eslint-plugin-import": "2.18.2"
6060
},
6161
"repository": {

packages/component/src/provider.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Provider', () => {
4949
});
5050

5151
it('returns null if the service is not available with optional flag', () => {
52-
expect(provider.getImmediate(undefined, { optional: true })).to.equal(
52+
expect(provider.getImmediate({ optional: true })).to.equal(
5353
null
5454
);
5555
});
@@ -69,8 +69,8 @@ describe('Provider', () => {
6969
it('ignores parameter identifier and return the default service', () => {
7070
provider.setComponent(getFakeComponent('test', () => ({ test: true })));
7171
const defaultService = provider.getImmediate();
72-
expect(provider.getImmediate('spider1')).to.equal(defaultService);
73-
expect(provider.getImmediate('spider2')).to.equal(defaultService);
72+
expect(provider.getImmediate({identifier: 'spider1'})).to.equal(defaultService);
73+
expect(provider.getImmediate({identifier: 'spider2'})).to.equal(defaultService);
7474
});
7575
});
7676

@@ -187,11 +187,11 @@ describe('Provider', () => {
187187
describe('Provider (multipleInstances = true)', () => {
188188
describe('getImmediate(identifier)', () => {
189189
it('throws if the service is not available', () => {
190-
expect(provider.getImmediate.bind(provider, 'guardian')).to.throw();
190+
expect(provider.getImmediate.bind(provider, {identifier: 'guardian'})).to.throw();
191191
});
192192

193193
it('returns null if the service is not available with optional flag', () => {
194-
expect(provider.getImmediate('guardian', { optional: true })).to.equal(
194+
expect(provider.getImmediate({ identifier: 'guardian', optional: true })).to.equal(
195195
null
196196
);
197197
});
@@ -201,8 +201,8 @@ describe('Provider', () => {
201201
getFakeComponent('test', () => ({ test: true }), true)
202202
);
203203
const defaultService = provider.getImmediate();
204-
const service1 = provider.getImmediate('guardian');
205-
const service2 = provider.getImmediate('servant');
204+
const service1 = provider.getImmediate({identifier: 'guardian'});
205+
const service2 = provider.getImmediate({identifier: 'servant'});
206206

207207
expect(defaultService).to.deep.equal({ test: true });
208208
expect(service1).to.deep.equal({ test: true });
@@ -296,8 +296,8 @@ describe('Provider', () => {
296296
provider.setComponent(getFakeComponent('test', getService, true));
297297

298298
// create 2 service instances with different names
299-
provider.getImmediate('instance1');
300-
provider.getImmediate('instance2');
299+
provider.getImmediate({identifier: 'instance1'});
300+
provider.getImmediate({identifier: 'instance2'});
301301

302302
// eslint-disable-next-line @typescript-eslint/no-floating-promises
303303
provider.delete();
@@ -313,7 +313,7 @@ describe('Provider', () => {
313313
provider.setComponent(getFakeComponent('test', () => ({}), true));
314314
// create serviec instances with different identifiers
315315
const defaultInstance = provider.getImmediate();
316-
const instance1 = provider.getImmediate('instance1');
316+
const instance1 = provider.getImmediate({identifier: 'instance1'});
317317

318318
expect((provider as any).instances.size).to.equal(2);
319319

@@ -327,7 +327,7 @@ describe('Provider', () => {
327327
// remove the named instance from cache and create a new instance with the same identifier
328328
provider.clearInstance('instance1');
329329
expect((provider as any).instances.size).to.equal(1);
330-
const newInstance1 = provider.getImmediate('instance1');
330+
const newInstance1 = provider.getImmediate({identifier: 'instance1'});
331331
expect(newInstance1).to.not.eq(instance1);
332332
expect((provider as any).instances.size).to.equal(2);
333333
});

packages/component/src/provider.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ export class Provider<T = unknown> {
3232
constructor(
3333
private readonly name: string,
3434
private readonly container: ComponentContainer
35-
) {}
35+
) { }
3636

37+
/**
38+
* @param identifier A provider can provide mulitple instances of a service
39+
* if this.component.multipleInstances is true.
40+
*/
3741
get(identifier: string = DEFAULT_ENTRY_NAME): Promise<T> {
3842
// if multipleInstances is not supported, use the default name
3943
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
@@ -51,22 +55,40 @@ export class Provider<T = unknown> {
5155
return this.instancesDeferred.get(normalizedIdentifier)!.promise;
5256
}
5357

58+
/**
59+
*
60+
* @param options.identifier A provider can provide mulitple instances of a service
61+
* if this.component.multipleInstances is true.
62+
* @param options.optional If optional is false or not provided, the method throws an error when
63+
* the service is not immediately available.
64+
* If optional is true, the method returns null if the service is not immediately available.
65+
*/
5466
getImmediate(
55-
identifier: string | undefined,
56-
options: { optional: true }
67+
options: {
68+
identifier?: string
69+
optional: true
70+
}
5771
): T | null;
58-
getImmediate(identifier?: string, options?: { optional: false }): T;
5972
getImmediate(
60-
identifier: string = DEFAULT_ENTRY_NAME,
61-
options?: { optional: boolean }
73+
options?: {
74+
identifier?: string,
75+
optional?: false
76+
}): T;
77+
getImmediate(
78+
options?: {
79+
identifier?: string,
80+
optional?: boolean
81+
}
6282
): T | null {
83+
84+
const { identifier, optional } = { identifier: DEFAULT_ENTRY_NAME, optional: false, ...options };
6385
// if multipleInstances is not supported, use the default name
6486
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
6587

6688
const instance = this.getOrInitializeService(normalizedIdentifier);
6789

6890
if (!instance) {
69-
if (options && options.optional) {
91+
if (optional) {
7092
return null;
7193
}
7294
throw Error(`Service ${this.name} is not available`);

packages/component/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ export type InstanceFactory<T = unknown> = (
4848
instanceIdentifier?: string
4949
) => T;
5050

51-
export type Dictionary = {
51+
export interface Dictionary {
5252
[key: string]: unknown;
5353
};

0 commit comments

Comments
 (0)