Code coverage report for src/createContext.js

Statements: 100% (45 / 45)      Branches: 100% (6 / 6)      Functions: 100% (11 / 11)      Lines: 100% (41 / 41)      Ignored: none     

All files » src/ » createContext.js
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    13 13 13   13 13 13 13   1 2 4       1 1 1       13   65 65     90 90   90     90   90 90 90   90   2 2 2 2   2   1 1               88   88     35 35   35                       157   157 90 89         65  
'use strict';
 
import { generateMask } from './utils'
import { BLUEPRINT, PROVIDER } from './options'
import { CONTAINER_ALIAS, CONTEXT_ALIAS } from './constants'
 
var ALIAS_IDX = 0
var VALUE_IDX = 1
var TYPE_IDX = 2
var DEPS_IDX = 3
 
function createChildContainerFactory(conf) {
	return function (container) {
		return container.createChild(conf)
	}
}
 
function createExporter(containerFactory, exportAlias) {
	return function (container) {
		return containerFactory(container).get(exportAlias)
	}
}
 
export default function createContext(contribute) {
 
	var pending = []
	var context = {
 
		map: (alias) => {
			context.flush()
			pending.push(alias)
 
			return {
				to: (value) => {
 
					pending.push(value)
 
					return {
						as: (...flags) => {
							var mask = generateMask(flags)
 
							if (mask === BLUEPRINT) {
								// test if VALUE is a function
								flags = [PROVIDER]
								pending[VALUE_IDX] = createChildContainerFactory(pending[VALUE_IDX])
								pending.push(generateMask(flags))
								pending.push([CONTAINER_ALIAS])
								
								return {
									exports: (alias) => {
										pending[VALUE_IDX] = createExporter(pending[VALUE_IDX], alias)
										return {
											map: context.map
										}
									},
									map: context.map
								}
							}
 
							pending.push(mask)
							
							return {
								map: context.map,
								
								injecting: (...deps) => {
									pending.push(deps)
 
									return {
										map: context.map
									}
								}
							}
						}
					}
				}
			}
		},
 
		flush: () => {
			var deps = pending[DEPS_IDX] || []
 
			if (pending.length > 2) {
				contribute(pending[ALIAS_IDX], pending[VALUE_IDX], pending[TYPE_IDX], deps)
				pending = []
			}
		}
	}
 
	return context
}