reactive-di
Version:
Reactive dependency injection
59 lines (51 loc) • 1.49 kB
JavaScript
// @flow
import type {TypedPropertyDescriptor} from '../interfaces'
import {nameId} from './JssSheetManager'
function addDebugInfo<V: Object>(obj: V): V {
if (obj[nameId] === undefined) {
for (let k in obj) {
const prop = obj[k]
if (prop && typeof prop === 'object') {
prop[nameId] = k
}
}
}
return obj
}
function themeProp<V: Object>(
proto: Object,
name: string,
descr: TypedPropertyDescriptor<V>
) {
const className: string = proto.constructor.displayName || proto.constructor.name
const getSheet: (() => V) | void = descr.get
const value: V | void = descr.value
if (getSheet === undefined && value === undefined) {
throw new Error(`Need ${className} { @theme get ${name}() }`)
}
if (getSheet) {
proto[`${name}#`] = getSheet
}
return {
enumerable: descr.enumerable,
configurable: descr.configurable,
get(): V {
const obj: Object = value || this[`${name}#`]()
return (addDebugInfo(obj): any)
}
}
}
declare function theme<V>(): () => typeof themeProp
declare function theme<V: Object>(
proto: Object,
name: string,
descr: TypedPropertyDescriptor<V>
): TypedPropertyDescriptor<*>
export default function theme<V: Object>(
proto: Object,
name: string,
descr: TypedPropertyDescriptor<V>
) {
return themeProp(proto, name, descr)
}
theme.fn = addDebugInfo