|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: A little bit of Inject refactor |
| 4 | +author: [gallayl] |
| 5 | +tags: ['inject'] |
| 6 | +image: img/009-inject-refactor.jpg |
| 7 | +date: '2022-08-12T18:00:00.257Z' |
| 8 | +draft: false |
| 9 | +excerpt: Emitting decorator type data is doomed :( |
| 10 | +--- |
| 11 | + |
| 12 | +## Why |
| 13 | + |
| 14 | +As you can see, in FuryStack I've tried to take some steps to use only standardized APIs to maintain the supportability. As I started to work with new tools and framework, I've found some bottlenecks. The first big bad was the hacky [extension method support](/008-byebye-extension-methods/) that I've introduced in the beginning of the project, but I've found an another black sheep in the heart of the Typescript ecosystem - Decorator support. |
| 15 | + |
| 16 | +In short: "[The emitDecoratorMetadata flag is intentionally not supported.](https://github.com/evanw/esbuild/issues/257#issuecomment-658053616)" |
| 17 | + |
| 18 | +## Emm ok, what now? 😕 |
| 19 | + |
| 20 | +We had a feature in `Inject` that *was* build on a top of emitting type data and that was *constructor injection*. The syntax was like: |
| 21 | + |
| 22 | +```ts |
| 23 | +const injector = new Injector() |
| 24 | +@Injectable() |
| 25 | +class Service1 { |
| 26 | + constructor(public service2: Service2, public service3: Service3) {} |
| 27 | +} |
| 28 | +@Injectable() |
| 29 | +class Service2 { |
| 30 | + public value = 'foo' |
| 31 | +} |
| 32 | +@Injectable() |
| 33 | +class Service3 { |
| 34 | + public value = 'bar' |
| 35 | +} |
| 36 | + |
| 37 | +expect(injector.getInstance(Service1).service2.value).toBe('foo') |
| 38 | +expect(injector.getInstance(Service1).service2.value).toBe('bar') |
| 39 | +``` |
| 40 | + |
| 41 | +That's clear that we can't use this if we loose type data at runtime, so the idea was to pass down the constructor object at runtime - and try to maintain the simplicity of the old API. |
| 42 | + |
| 43 | +## The new `Injected()` properties ✨ |
| 44 | + |
| 45 | +...so a new property-level decorator called `Injected()` was born. |
| 46 | +The very same behavior with the new API looks like this: |
| 47 | + |
| 48 | +```ts |
| 49 | +const injector = new Injector() |
| 50 | +@Injectable() |
| 51 | +class Service1 { |
| 52 | + @Injected(Service2) |
| 53 | + public service2!: Service2 |
| 54 | + |
| 55 | + @Injected(Service3) |
| 56 | + public service2!: Service3 |
| 57 | +} |
| 58 | +@Injectable() |
| 59 | +class Service2 { |
| 60 | + public value = 'foo' |
| 61 | +} |
| 62 | +@Injectable() |
| 63 | +class Service3 { |
| 64 | + public value = 'bar' |
| 65 | +} |
| 66 | + |
| 67 | +expect(injector.getInstance(Service1).service2.value).toBe('foo') |
| 68 | +expect(injector.getInstance(Service1).service2.value).toBe('bar') |
| 69 | +``` |
| 70 | + |
| 71 | +### Some pros 👌 |
| 72 | + - The consrtuctor instance is passed down as a variable - without emitting non-standard metadata and other black magic |
| 73 | + - The main behavior (lifetime, recursive resolution, etc...) remains the same |
| 74 | + |
| 75 | +### ...and a few drawbacks 😿 |
| 76 | + - Properties will be injected **after** constructing the instance - it means that you cannot use them in the constructor |
| 77 | + - A breaking change - again... |
| 78 | + - See the `!` operator? Yeah, it's a kind of "shortcut" for now... |
0 commit comments