Skip to content

Instantly share code, notes, and snippets.

@runspired
Created November 24, 2019 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runspired/4a8e600c93dc70a58e9f79e9c2e5970f to your computer and use it in GitHub Desktop.
Save runspired/4a8e600c93dc70a58e9f79e9c2e5970f to your computer and use it in GitHub Desktop.
I trimmed from 1974 characters down to these 1883. I was unable to post until below ~1895.
This would simplify native class use in Ember
https://github.com/emberjs/ember.js/blob/v3.14.1/packages/%40ember/-internals/metal/lib/injected_property.ts#L73
Currently, due to the nature of `getOwner` things like inject can only be used on framework objects
what are your thoughts on a thing for enabling DI on non framework objects?
basically, with nice getters I don't feel objects need to be instantiated via the container anymore to use DI
and this would make just using native classes a lot of places pretty nice
alternatively: an api specifically for this for getting the current application
```ts
type Owner {
lookup() {}
register() {}
factoryFor() {}
}
function getApplicationOwner(): Owner {}
...
return getApplicationOwner().lookup('service:foo');
```
This enables building libraries that are less Ember specific then exposing a very thin set of ember utils or extending with ember things. A generic library could:
```ts
import { service } from 'not-ember/service';
export default class MessageService {
@service fetchService;
}
```
Then for Ember
```ts
// services/message.ts
export { default } from "message-service";
```
Or
```ts
// services/message.ts
import MessageService from "message-service";
import { inject as service } from "@ember/service";
export default class CachingService extends MessageService {
@service store;
async fetchMessages(url, ...args) {
let collection = this.store.peekCollection(url);
if (!collection && collection.expires < Date.now()) {
let data = await super.fetchMessages(url, ...args);
collection = this.store.pushCollection(data);
}
return collection;
}
static create() {
return new this();
}
}
```
In this case ember's service decorator is taking advantage of us having re-exported as a service which gives us an owner, but you could just as easily imagine provider classes that aren't services taking advantage of this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment