Skip to content

Commit

Permalink
remove transform utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandotv committed Jul 1, 2024
1 parent 634db4a commit 867d760
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 378 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-windows-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pumpit": major
---

remove `transform` utility function. Another anti-pattern.
50 changes: 0 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ It supports different injection scopes, child containers, hooks etc...
- [Optional injections](#optional-injections)
- [~~Circular dependencies~~](#circular-dependencies)
- [~~Injecting arrays~~](#injecting-arrays)
* [Transforming injected dependencies](#transforming-injected-dependencies)
* [Post construct method](#post-construct-method)
- [Removing values from the container](#removing-values-from-the-container)
* [Calling the dispose method](#calling-the-dispose-method)
Expand Down Expand Up @@ -315,9 +314,6 @@ const container = new PumpIt()
const resolveCtx = { foo: 'bar' }
container.resolve('some_key', resolveCtx)
```

> Read about [transforming dependencies](#transforming-dependencies) to see how context is passed to the callbacks.
## Injection tokens

Injection tokens are the values by which the injection container knows how to resolve registered data. They can be `string`, `Symbol`, or any object.
Expand Down Expand Up @@ -536,52 +532,6 @@ instanceA.b // undefined
> NOTE: Injecting array as a dependency has been removed in version 6.
> If you want to use this feature you can use [version 5](https://github.com/ivandotv/pumpit/tree/v5.0.0)
### Transforming injected dependencies

Injected dependencies can also be manipulated just before they are injected. For this, we use the `transform()` helper function.

`transform` function wraps the injected dependencies, and accepts a callback which will receive all the resolved dependencies that need to be injected, and it should return an array of dependencies. whatever is returned from the callback, will be injected.

```ts
import { transform, PumpIt } from 'pumpit'

const container = new PumpIt()

const keyA = Symbol()
const keyB = Symbol()
const keyC = Symbol()

const valueA = { name: 'a' }
const valueB = { name: 'b' }
const valueC = { name: 'c' }

const resolveCtx = { hello: 'world' }

class TestA {
static inject = transform(
[keyA, keyB, keyC],
(
{ container, ctx },
a: typeof valueA,
b: typeof valueB,
c: typeof valueC
) => {
container === pumpIt // instance of PumpIt
ctx === resolveCtx // context data

a === valueA
b === valueB
c === valueC

//default implementation, return the same dependencies in the same order
return [a, b, c]
}
)

constructor(a: typeof valueA, b: typeof valueB, c: typeof valueC) {}
}
```

### Post construct method

If the class that is being constructed (resolved) has a "postConstruct" method defined it will be called automatically when the class instance is created, in the case of singleton instances it will be called only once. One more important thing about `postConstruct` method is that it will be called in the reverse order of the resolution chain. [Please refer to this test for a concrete example](src/__tests__/instance/post-construct.test.ts#L22)
Expand Down
290 changes: 0 additions & 290 deletions src/__tests__/transform/dependency-transform.test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export * from "./pumpit.js"
export * from "./types.js"
export * from "./pumpit-error.js"

import { INJECT_KEY, get, registerInjections, transform } from "./utils.js"
import { INJECT_KEY, get, registerInjections } from "./utils.js"

export { get, transform, INJECT_KEY, registerInjections }
export { get, INJECT_KEY, registerInjections }
12 changes: 1 addition & 11 deletions src/pumpit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,7 @@ export class PumpIt {
let resolvedDeps: any[] = []

if (injectionData) {
if (Array.isArray(injectionData)) {
resolvedDeps = this.resolveDeps(injectionData, ctx)
} else {
resolvedDeps = injectionData.fn(
{
container: this,
ctx: ctx.ctx,
},
...this.resolveDeps(injectionData.deps, ctx),
)
}
resolvedDeps = this.resolveDeps(injectionData, ctx)
}
// @ts-expect-error - type narrow
const result = value(...resolvedDeps)
Expand Down
Loading

0 comments on commit 867d760

Please sign in to comment.