-
-
Notifications
You must be signed in to change notification settings - Fork 18
Add initializing lifecycle hook to AtomEffect #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /// An atom effect that performs an arbitrary action before the atom is first used and initialized, | ||
| /// or once it is released and re-initialized. | ||
| public struct InitializingEffect: AtomEffect { | ||
| private let action: @MainActor () -> Void | ||
|
|
||
| /// Creates an atom effect that performs the given action before the atom is initialized. | ||
| public init(perform action: @MainActor @escaping () -> Void) { | ||
| self.action = action | ||
| } | ||
|
|
||
| /// A lifecycle event that is triggered before the atom is first used and initialized, | ||
| /// or once it is released and re-initialized. | ||
| public func initializing(context: Context) { | ||
| action() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import XCTest | ||
|
|
||
| @testable import Atoms | ||
|
|
||
| final class InitializingEffectTests: XCTestCase { | ||
| @MainActor | ||
| func testEvent() { | ||
| let context = AtomCurrentContext(store: .dummy) | ||
| var performedCount = 0 | ||
| let effect = InitializingEffect { | ||
| performedCount += 1 | ||
| } | ||
|
|
||
| effect.initializing(context: context) | ||
| XCTAssertEqual(performedCount, 1) | ||
|
|
||
| effect.initialized(context: context) | ||
| XCTAssertEqual(performedCount, 1) | ||
|
|
||
| effect.updated(context: context) | ||
| XCTAssertEqual(performedCount, 1) | ||
|
|
||
| effect.released(context: context) | ||
| XCTAssertEqual(performedCount, 1) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,9 @@ final class ReleaseEffectTests: XCTestCase { | |
| performedCount += 1 | ||
| } | ||
|
|
||
| effect.initializing(context: context) | ||
|
||
| XCTAssertEqual(performedCount, 0) | ||
|
|
||
| effect.initialized(context: context) | ||
| XCTAssertEqual(performedCount, 0) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,9 @@ final class UpdateEffectTests: XCTestCase { | |
| performedCount += 1 | ||
| } | ||
|
|
||
| effect.initializing(context: context) | ||
|
||
| XCTAssertEqual(performedCount, 0) | ||
|
|
||
| effect.initialized(context: context) | ||
| XCTAssertEqual(performedCount, 0) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding an inline comment here to clarify that the initializing hook is intentionally invoked before retrieving the atom's value to capture pre-initialization state.