Skip to content

Commit

Permalink
Stores DI (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
KazaiMazai authored Aug 21, 2024
1 parent 8adee48 commit c215f86
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Sources/Puredux/Store/StoresDI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// File.swift
//
//
// Created by Sergey Kazakov on 20/08/2024.
//

import Foundation
import SwiftUI

extension StateStore {
func registered() -> Self {
Stores.getInstance().register(self, key: State.self)
return self
}

func registered<Key>(_ key: Key) -> Self {
Stores.getInstance().register(self, key: key)
return self
}

static func resolved() -> Self {
Stores.getInstance().resolve(with: State.self)
}

static func resolved<Key>(with key: Key) -> Self {
Stores.getInstance().resolve(with: key)
}

func unregister() {
Stores.getInstance().unregister(self, key: State.self)
}

func unregister<Key>(_ key: Key) {
Stores.getInstance().unregister(self, key: key)
}
}

final class Stores {
private static let shared = Stores()

static func getInstance() -> Stores {
shared
}

private var dependencies: [String: Any] = [:]

func unregister<T, Key>(_ dependency: T, key: Key? = nil) {
let key = compoundKey(for: T.self, key: key)
dependencies[key] = nil
}

func register<T, Key>(_ dependency: T, key: Key? = nil) {
let key = compoundKey(for: T.self, key: key)
dependencies[key] = dependency
}

func resolve<T, Key>(with key: Key?) -> T {
let dependency: T? = resolveOptional(with: key)
precondition(dependency != nil,
"""
Store of type: \(T.self) not found \(key.map { "for \($0)." } ?? ".")
Must register a store before resolving.
"""
)
return dependency!
}

private func resolveOptional<T, Key>(with key: Key?) -> T? {
let key = compoundKey(for: T.self, key: key)
let dependency = dependencies[key] as? T
return dependency
}

private func compoundKey<T, Key>(for type: T.Type, key: Key?) -> String {
[
String(reflecting: T.self),
key.map { "\($0)" }
]
.compactMap { $0 }
.joined(separator: "-")
}
}

0 comments on commit c215f86

Please sign in to comment.