-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
139 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
class Domain { | ||
public class Domain { | ||
// MARK: Lifecycle | ||
|
||
init(name: String, parent: Domain?) { | ||
init(_ name: String, domain: Domain? = nil) { | ||
self.name = name | ||
self.parent = parent | ||
self.parent = domain | ||
|
||
self.graphite = Node(name: name, kind: .domain, priority: .child) | ||
|
||
if let domain { | ||
forward(from: eventCreated, to: domain.eventCreated) | ||
forward(from: storeCreated, to: domain.storeCreated) | ||
forward(from: domainCreated, to: domain.domainCreated) | ||
} | ||
|
||
eventCreated.watch { unit in | ||
if let onCreate = self.onCreateEvent { | ||
onCreate(unit) | ||
} | ||
} | ||
|
||
storeCreated.watch { unit in | ||
if let onCreate = self.onCreateStore { | ||
onCreate(unit) | ||
} | ||
} | ||
|
||
domainCreated.watch { unit in | ||
if let onCreate = self.onCreateDomain { | ||
onCreate(unit) | ||
} | ||
} | ||
} | ||
|
||
// MARK: Public | ||
|
||
public var graphite: Node | ||
public let name: String | ||
public var onCreateEvent: ((_ event: AnyEvent) -> Subscription)? | ||
public var onCreateStore: ((_ store: Store<Any>) -> Subscription)? | ||
public var onCreateEffect: ((_ effect: Effect<Any, Any, Error>) -> Subscription)? | ||
public var onCreateDomain: ((_ domain: Domain) -> Subscription)? | ||
|
||
public let parent: Domain? | ||
public let eventCreated = Event<AnyEvent>() | ||
public let storeCreated = Event<AnyStore>() | ||
public let domainCreated = Event<Domain>() | ||
|
||
public var kind: String { "domain" } | ||
public var onCreateEvent: ((_ event: AnyEvent) -> Void)? | ||
public var onCreateStore: ((_ store: AnyStore) -> Void)? | ||
public var onCreateDomain: ((_ domain: Domain) -> Void)? | ||
|
||
// MARK: Internal | ||
public let parent: Domain? | ||
|
||
let events = [AnyEvent]() | ||
public var kind: String { "domain" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
@testable import Effector | ||
import XCTest | ||
|
||
final class DomainTests: XCTestCase { | ||
func testEventWatch() async throws { | ||
let domain = Domain("app") | ||
|
||
var log = [Int]() | ||
|
||
domain.onCreateEvent = { event in | ||
event.watch { value in | ||
log.append(value as! Int) | ||
} | ||
} | ||
|
||
let event = Event<Int>(domain: domain) | ||
|
||
event(1) | ||
event(2) | ||
|
||
XCTAssertEqual(log, [1, 2]) | ||
} | ||
|
||
func testStoreReset() async throws { | ||
let domain = Domain("app") | ||
|
||
let reset = Event<Void>() | ||
|
||
domain.onCreateStore = { store in | ||
store.reset(reset) | ||
} | ||
|
||
let storeA = Store<Int>(0, domain: domain) | ||
let storeB = Store<Int>(0, domain: domain) | ||
|
||
storeA.setState(1) | ||
storeB.setState(1) | ||
|
||
XCTAssertEqual(storeA.getState(), 1) | ||
XCTAssertEqual(storeB.getState(), 1) | ||
|
||
reset() | ||
|
||
XCTAssertEqual(storeA.getState(), 0) | ||
XCTAssertEqual(storeB.getState(), 0) | ||
} | ||
|
||
func testSubdomain() async throws { | ||
let app = Domain("app") | ||
let domain = Domain("sub", domain: app) | ||
|
||
var log = [Int]() | ||
|
||
app.onCreateEvent = { event in | ||
event.watch { value in | ||
log.append(value as! Int) | ||
} | ||
} | ||
|
||
let event = Event<Int>(domain: domain) | ||
|
||
event(1) | ||
event(2) | ||
|
||
XCTAssertEqual(log, [1, 2]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters