-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from peopledoc/modernise-service-embedded
Modernise Service `embedded` + accept a generic type parameter
- Loading branch information
Showing
3 changed files
with
88 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
import ObjectProxy from '@ember/object/proxy' | ||
import { getOwner } from '@ember/application' | ||
import { assert } from '@ember/debug' | ||
|
||
// eslint-disable-next-line ember/no-classic-classes | ||
const configService = ObjectProxy.extend({ | ||
init(...args: Array<Record<string, unknown>>): void { | ||
this.content = getOwner(this).factoryFor('config:embedded').class | ||
this._super(...args) | ||
}, | ||
}) | ||
type AnyObject = Record<string, unknown> | ||
|
||
configService.reopenClass({ | ||
isServiceFactory: true, | ||
}) | ||
export default class EmbeddedService< | ||
EmbeddedOptions extends AnyObject = AnyObject | ||
> extends ObjectProxy<EmbeddedOptions> { | ||
|
||
export default configService | ||
constructor() { | ||
super(...arguments) // eslint-disable-line prefer-rest-params | ||
|
||
const factoryName = 'config:embedded' | ||
const factory: { class: EmbeddedOptions } | undefined = getOwner(this).factoryFor(factoryName) | ||
|
||
assert( | ||
`The factory "${factoryName}" could not be found.`, | ||
typeof factory === 'object' | ||
) | ||
|
||
this.content = factory.class | ||
} | ||
|
||
} | ||
|
||
// DO NOT DELETE: this is how TypeScript knows how to look up your services. | ||
declare module '@ember/service' { | ||
interface Registry { | ||
embedded: EmbeddedService; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters