-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathInjectStorage.swift
More file actions
43 lines (37 loc) · 1.41 KB
/
InjectStorage.swift
File metadata and controls
43 lines (37 loc) · 1.41 KB
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
import Storage
/// `InjectStorage` subclass of `DelegatedStorage` that uses a `[AnyHashable: Any]`.
open class InjectStorage: DelegatedStorage {
/// `InjectStorage` shared instance.
open class var standard: InjectStorage { shared }
private static let shared = InjectStorage()
var groups = [DependencyGroupKey: InjectStorage]()
var storage = [StoreKey: [Any]]()
/**
Returns the `[Any]` of dependencies associated with the specified `StoreKey`.
- Parameter key: A `StoreKey` in storage.
*/
override open func array(forKey key: StoreKey) -> [Any]? { storage[hash(key)] }
override open func set(object: Any?, forKey key: StoreKey) {
let storeKey = hash(key)
if let dependency = object {
if storage[storeKey] == nil {
storage[storeKey] = [Any]()
}
storage[storeKey]?.append(dependency)
}
}
/**
Ensure it's explicitly on the same actor, though implied by class context.
- Parameter groupKey: A `DependencyGroupKey` in storage.
*/
@StorageActor
public func storageForGroup(_ groupKey: DependencyGroupKey) -> InjectStorage {
if let existingGroupStorage = groups[groupKey] {
return existingGroupStorage
} else {
let newGroupStorage = InjectStorage()
groups[groupKey] = newGroupStorage
return newGroupStorage
}
}
}