All files ioc.ts

93.46% Statements 100/107
60.71% Branches 17/28
94.12% Functions 32/34
93.94% Lines 93/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212      1x 1x 1x 1x             1x 1x 1x 1x             1x 2x 2x             1x 1x     1x 1x 1x 1x             1x 3x           1x 4x 3x   4x 2x 2x             1x 2x 2x       1x 1x 1x         1x             7x 7x 7x 7x     1x     1x   1x       1x     1x 1x 1x           1x 5x             1x 4x 4x 4x               1x 2x 2x   2x 2x     1x 3x           1x 1x 1x     1x 2x 2x     2x 2x     2x 2x 2x 2x   2x 2x               12x 7x 5x 7x 7x     1x 1x   1x 1x 5x 5x       2x   2x 2x   2x 14x   7x 7x     2x   2x 2x   1x   1x  
/**
 * Creates an instance of the class and associates it with the give name.
 */
export function named(Iname:string = undefined) {
  return function(target: Function) {
    container.addNamedClass(name, target)
    container.relink(name)
  }
}
 
/**
 * Associates the value returned by the factory method with the given name.
 */
export function factory(name:string) {
  return function(prototype: any, propertyKey: string, descriptor: PropertyDescriptor) {
    container.addNamedInstance(name, prototype[propertyKey]())
    container.relink(name)
  }
}
 
/**
 * Assigns the instance associated with the given name to the property.
 */
export function autowired(name:string) {
  return function(target: any, propertyKey: string) {
    container.wireInstance(name, target, propertyKey)
  }
}
 
/**
 * Marks a class as providing all services for a given tag.
 */
export function serviceClass(Itag:string = undefined) {
  Iif(!tag)
    tag = 'default'
 
  return function(target: Function) {
    container.addNamedClass(tag, target)
    container.addServices(tag)
    container.relink(tag)
  }
}
 
/**
 * Marks a method as provinding the service with the given name.
 */
export function service(name:string) {
  return taggedService(undefined, name)
}
 
/**
 * Marks a method as provinding the service for the given tag with the given name.
 */
export function taggedService(tag:string, name:string) {
  if(!tag)
    tag = 'default'
 
  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    container.addService(tag, target, propertyKey, name)
    container.relink(tag)
  }
}
 
/**
 * Creates an object with functions for each service with the given tag.
 */
export function autowiredService(tag:string) {
  return function(target: any, propertyKey: string) {
    container.createServiceForTag(tag, target, propertyKey)
  }
}
 
const rootService = service('root-service')
const rulebaseService = service('rulebase-service')
export {rootService, rulebaseService}
 
// blackboxDatatype
 
 
class Service {
  tag:string
  name:string
  instance:any
  methodName:string
 
  constructor(tag:string, name:string, instance:any, methodName:string) {
    this.instance = instance
    this.methodName = methodName
    this.tag = tag
    this.name = name
  }
 
  call(params:any):any {
    return this.instance[this.methodName](params)
  }
}
 
class IOCContainer {
  services: { [key:string]:Service }
  instances: { [key:string]:any }
  linkers: { [key:string]:Function[] }
  delimiter = '.'
 
  constructor() {
    this.services = {}
    this.instances = {}
    this.linkers = {}
  }
 
  /**
   * Calls all linkers associated with the given label.
   */
  relink(label:string) {
    Iif(this.linkers[label])
      this.linkers[label].forEach((linker) => linker())
  }
 
  /**
   * Add a linking function associated with the given label.
   */
  private addLinker(tag:string, linker:()=>void) {
    Eif(!this.linkers[tag])
      this.linkers[tag] = []
    this.linkers[tag].push(linker)
  }
 
 
  //////////
  // Instance management
  //////////
 
  private newInstance(type:Function):any {
    let instance:any = {}
    instance['__proto__'] = type.prototype
    // instance.constructor = type
    type.call(instance)
    return instance
  }
 
  private assertNameDoesntExist(name:string) {
    Iif(this.instances[name]) {
      console.error(this.instances);
      throw new Error(`Named object '${name}' already exists.`)
    }
  }
 
  addNamedInstance(name:string, instance:any) {
    this.assertNameDoesntExist(name)
    this.instances[name] = instance
  }
 
  addNamedClass(name:string, type:any) {
    let objectName = name ? name : type.name // default to class name
    this.assertNameDoesntExist(objectName)
 
    // Add new instance:
    let instance = this.newInstance(type)
    this.instances[objectName] = instance
  }
 
  wireInstance(name:string, target:any, propertyKey:string) {
    const linker = () => {
      Eif(this.instances[name])
        target[propertyKey] = this.instances[name]
    }
    linker()
    this.addLinker(name, linker)
  }
 
 
  //////////
  // Service management
  //////////
 
  addService(tag:string, instance:any, key:string, name:string = undefined) {
    if(!name)
      name = key
    const serviceName = tag ? tag + this.delimiter + name : name
    this.services[serviceName] = new Service(tag, name, instance, key)
  }
 
  addServices(name:string) {
    Iif(!this.instances[name])
      throw new Error(`Object named ${name} not found.`)
    let instance = this.instances[name]
    Object.keys(instance.constructor.prototype).forEach( (key:string) => {
      Eif(typeof instance[key] === 'function')
        this.addService(name, instance, key)
    })
  }
 
  createServiceForTag(tag:string, target:any, propertyKey:string) {
 
    const linker = () => {
      let services = {}
 
      Object.keys(this.services)
        .filter( (serviceName:string) => this.services[serviceName].tag === tag )
        .forEach( (serviceName:string) => {
          const service = this.services[serviceName]
          services[service.name] = service.instance[service.methodName].bind(service.instance)
        })
 
      target[propertyKey] = services
    }
    linker()
    this.addLinker(tag, linker)
  }
}
 
const container = new IOCContainer()