Re-order single

This commit is contained in:
Christian Treffs 2020-08-05 10:29:51 +02:00
parent 493cf9f676
commit d251b3d388
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
1 changed files with 14 additions and 14 deletions

View File

@ -9,20 +9,6 @@ public protocol SingleComponent: Component {
init()
}
extension Nexus {
public func single<S>(_ component: S.Type) -> Single<S> where S: SingleComponent {
let family = self.family(requires: S.self)
precondition(family.count <= 1, "Singleton count of \(S.self) must be 0 or 1: \(family.count)")
let entityId: EntityIdentifier
if family.isEmpty {
entityId = createEntity(with: S()).identifier
} else {
entityId = family.memberIds.first.unsafelyUnwrapped
}
return Single<S>(nexus: self, traits: family.traits, entityId: entityId)
}
}
public struct Single<A> where A: SingleComponent {
public let nexus: Nexus
public let traits: FamilyTraitSet
@ -49,3 +35,17 @@ extension Single where A: SingleComponent {
nexus.get(entity: entityId).unsafelyUnwrapped
}
}
extension Nexus {
public func single<S>(_ component: S.Type) -> Single<S> where S: SingleComponent {
let family = self.family(requires: S.self)
precondition(family.count <= 1, "Singleton count of \(S.self) must be 0 or 1: \(family.count)")
let entityId: EntityIdentifier
if family.isEmpty {
entityId = createEntity(with: S()).identifier
} else {
entityId = family.memberIds.first.unsafelyUnwrapped
}
return Single<S>(nexus: self, traits: family.traits, entityId: entityId)
}
}