Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 766 Bytes

README.md

File metadata and controls

42 lines (31 loc) · 766 Bytes

hook-di

Reconsidering dependencies injection in hook style programming.

Highly inspired by vue composition apis.

usage

here is the interfaces

import { InjectionKey } from 'hook-di'

export interface MyService {
  hello: () => string
}
// InjectionKey
export const MyServiceKey: InjectionKey<MyService> = InjectionKey('my-service')

// use MyService hook
export function useMyService(): MyService {
  return {
    hello() {
      return `storea`
    },
  }
}

manually register dependencies and auto inject.

import { createScope, use } from '../src'

const scope = createScope()
scope.register(MyServiceKey, useMyService)

scope.run(() => {
  const service = use(MyServiceKey)
  service.hello() // "storebstorea"
})