Skip to content

Instantly share code, notes, and snippets.

@Thomvis
Created April 17, 2017 15:06
Show Gist options
  • Save Thomvis/3d015cfc15e9a538ac6712ce2655ada5 to your computer and use it in GitHub Desktop.
Save Thomvis/3d015cfc15e9a538ac6712ce2655ada5 to your computer and use it in GitHub Desktop.
import Dispatch
public typealias Provider<E> = () -> E
class Container {
{% for type in types.all %}
{% if type|annotated:"singleton" %}
private let {{type.name|lowerFirstWord}}Queue = DispatchQueue(label: "singleton:{{type.name|lowerFirstWord}}")
private var __{{type.name|lowerFirstWord}}: {{type.name}}?
public func {{type.name|lowerFirstWord}}() -> {{type.name}} {
return {{type.name|lowerFirstWord}}Queue.sync {
let res = __{{type.name|lowerFirstWord}} ?? _{{type.name|lowerFirstWord}}()
self.__{{type.name|lowerFirstWord}} = res
return res
}
}
{% else %}
public func {{type.name|lowerFirstWord}}() -> {{type.name}} {
return _{{type.name|lowerFirstWord}}()
}
{% endif %}
private func _{{type.name|lowerFirstWord}}() -> {{type.name}} {
{% for ini in type.initializers|annotated:"inject" %}
return {{type.name}}(
{% for param in ini.parameters %}
{% if param.typeName.name|hasPrefix:"Provider<" %}
{{param.argumentLabel}}: {{param.typeName.name|replace:"Provider<",""|replace:">",""|lowerFirstWord}}
{% else %}
{{param.argumentLabel}}: {{param.typeName.name|lowerFirstWord}}(){% if not forloop.last%},{% endif %}
{% endif %}
{% endfor %}
)
{% endfor %}
}
{% endfor %}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment