# kotlin-js-di

> Kotlin.js di library

Latest version **0.0.6** (published 2018-09-23) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install kotlin-js-di
pnpm add kotlin-js-di
yarn add kotlin-js-di
bun add kotlin-js-di
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2018-09-23 |
| First published | 2018-04-02 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 647.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | diklimchuk |
| Maintainers | diklimchuk |
| Keywords | Kotlin, di, Kotlin.js, JavaScript |

## Links

- npm: https://www.npmjs.com/package/kotlin-js-di
- Repository: https://github.com/diklimchuk/Kotlin-js-di
- Issues: https://github.com/diklimchuk/Kotlin-js-di/issues
- npm.io page: https://npm.io/package/kotlin-js-di

## Dependencies (1)

- [kotlin](https://npm.io/package/kotlin.md) 1.2.70

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.0.6 (latest) — 2018-09-23
- 0.0.5 — 2018-04-04
- 0.0.4 — 2018-04-03
- 0.0.3 — 2018-04-02
- 0.0.2 — 2018-04-02
- 0.0.1 — 2018-04-02

## README

# Kotlin-js-di
Dependency injection library for kotlin.js. Supports multiple modules, scopes and late bindings.
#### This library is in beta. Api is not comfortable to use yet and will change in future.

# Integration
Gradle:
```groovy
    buildscript {
        ext.kotlin_js_di_version='0.0.5'
    }

    allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }
    
    dependencies {
        compile "com.github.diklimchuk:Kotlin-js-di:$kotlin_js_di_version"
    }
```
[Npm](https://www.npmjs.com/package/kotlin-js-di):
```
    npm i kotlin-js-di
```

# Getting started
### Modules
Modules determine how object will be created:
```kotlin
// Define classes
class Router(rootUrl: String)
class Application(router: Router)
// Modules can provide dependencies by name
val ROUTER_MODULE = createDiModule {
    it provides { Router(get("RootUrl")) } with "Router"
    it binds "/app" with "RotUrl"
}
// Modules can take dependencies from other modules.
val APPLICATION_MODULE = createDiModule {
    it scopes { Application(get()) }
}
```
All dependencies in module will be lazily evaluated.

Modules can create object in 3 ways:
* `provides` will create new instance each time
* `scopes` will instantiate object once until module scope won't be released
* `binds` will use the same instance every time. It is not allowed have dependencies and can be used to skip braces around.

### Components
Components can provide dependencies:
```kotlin
// Define component
class MainComponent : DiComponent(APPLICATION_MODULE, ROUTER_MODULE)
```
```kotlin
// Create component
val component = MainComponent()
// Then retrieve all dependencies you need
val app: Application = component.get()
val router: Router = component.get()
val rootUrl: String = component.get("RootUrl")
```

### Subcomponents
Subcomponent allow dependencies to live for a period less than Singleton:
```kotlin
class UserProvider
// Define scope (i.e for a page): 
val pageScope = DiScope.SINGLETON.createChild()
// Define subcomponent in a module
val PAGE_MODULE = createDiModule {
    it hasSubcomponents {
        it add DiComponent(createDiModule {
            it scopes UserProvider()
        }) scoped pageScope
    }
}
// Add Module to main component
val component = DiComponent(PAGE_MODULE)
// Open scope:
fun onPageOpened() {
    val subcomponent = component.openScope(pageScope)
    // Inject dependencies
    val userProvider: UserProvider = subcomponent.get()
    // Close scope when it's no longer needed
    subcomponent.release()
}
```

### Lateinit bindings
Allow to add object to dependency graph at runtime
```kotlin
class NoteProvider(noteId: Int)
val pageScope = DiScope.SINGLETON.createChild()
val MODULE = crateDiModule {
    it hasSubcomponents {
        it add DiComponent(createDiModule {
            it scopes NoteProvider(get("NoteId"))
        })
    }
}
fun onPageOpened(noteId: Int) {
    val subcomponent = component.openScope(pageScope) {
        // You can provide dependencies in runtime. They will be bound to opened scope.
        it binds noteId with "NoteId"
    }
    val noteProvider: NoteProvider = subcomponent.get()
    subcomponent.release()
}
```

### TODO
* Log error if both component and subcomponent failed dependency cretion
* Allow only lateinit values
* Better error descriptions
* Logging

---
_Source: https://npm.io/package/kotlin-js-di · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
