|
| 1 | +/// A context structure that to read, set, and otherwise interacting with atoms. |
| 2 | +@MainActor |
| 3 | +public struct AtomUpdatedContext: AtomContext { |
| 4 | + @usableFromInline |
| 5 | + internal let _store: StoreContext |
| 6 | + |
| 7 | + internal init(store: StoreContext) { |
| 8 | + self._store = store |
| 9 | + } |
| 10 | + |
| 11 | + /// Accesses the value associated with the given atom without watching to it. |
| 12 | + /// |
| 13 | + /// This method returns a value for the given atom. Even if you access to a value with this method, |
| 14 | + /// it doesn't initiating watch the atom, so if none of other atoms or views is watching as well, |
| 15 | + /// the value will not be cached. |
| 16 | + /// |
| 17 | + /// ```swift |
| 18 | + /// let context = ... |
| 19 | + /// print(context.read(TextAtom())) // Prints the current value associated with `TextAtom`. |
| 20 | + /// ``` |
| 21 | + /// |
| 22 | + /// - Parameter atom: An atom that associates the value. |
| 23 | + /// |
| 24 | + /// - Returns: The value associated with the given atom. |
| 25 | + @inlinable |
| 26 | + public func read<Node: Atom>(_ atom: Node) -> Node.Loader.Value { |
| 27 | + _store.read(atom) |
| 28 | + } |
| 29 | + |
| 30 | + /// Sets the new value for the given writable atom. |
| 31 | + /// |
| 32 | + /// This method only accepts writable atoms such as types conforming to ``StateAtom``, |
| 33 | + /// and assign a new value for the atom. |
| 34 | + /// When you assign a new value, it notifies update immediately to downstream atoms or views. |
| 35 | + /// |
| 36 | + /// - SeeAlso: ``AtomViewContext/subscript`` |
| 37 | + /// |
| 38 | + /// ```swift |
| 39 | + /// let context = ... |
| 40 | + /// context.set("New text", for: TextAtom()) |
| 41 | + /// print(context.read(TextAtom())) // Prints "New text" |
| 42 | + /// ``` |
| 43 | + /// |
| 44 | + /// - Parameters |
| 45 | + /// - value: A value to be set. |
| 46 | + /// - atom: An atom that associates the value. |
| 47 | + @inlinable |
| 48 | + public func set<Node: StateAtom>(_ value: Node.Loader.Value, for atom: Node) { |
| 49 | + _store.set(value, for: atom) |
| 50 | + } |
| 51 | + |
| 52 | + /// Refreshes and then return the value associated with the given refreshable atom. |
| 53 | + /// |
| 54 | + /// This method only accepts refreshable atoms such as types conforming to: |
| 55 | + /// ``TaskAtom``, ``ThrowingTaskAtom``, ``AsyncSequenceAtom``, ``PublisherAtom``. |
| 56 | + /// It refreshes the value for the given atom and then return, so the caller can await until |
| 57 | + /// the value completes the update. |
| 58 | + /// Note that it can be used only in a context that supports concurrency. |
| 59 | + /// |
| 60 | + /// ```swift |
| 61 | + /// let context = ... |
| 62 | + /// let image = await context.refresh(AsyncImageDataAtom()).value |
| 63 | + /// print(image) // Prints the data obtained through network. |
| 64 | + /// ``` |
| 65 | + /// |
| 66 | + /// - Parameter atom: An atom that associates the value. |
| 67 | + /// |
| 68 | + /// - Returns: The value which completed refreshing associated with the given atom. |
| 69 | + @discardableResult |
| 70 | + @inlinable |
| 71 | + public func refresh<Node: Atom>(_ atom: Node) async -> Node.Loader.Value where Node.Loader: RefreshableAtomLoader { |
| 72 | + await _store.refresh(atom) |
| 73 | + } |
| 74 | + |
| 75 | + /// Resets the value associated with the given atom, and then notify. |
| 76 | + /// |
| 77 | + /// This method resets a value for the given atom, and then notify update to the downstream |
| 78 | + /// atoms and views. Thereafter, if any of other atoms or views is watching the atom, a newly |
| 79 | + /// generated value will be produced. |
| 80 | + /// |
| 81 | + /// ```swift |
| 82 | + /// let context = ... |
| 83 | + /// context[TextAtom()] = "New text" |
| 84 | + /// print(context.read(TextAtom())) // Prints "New text" |
| 85 | + /// context.reset(TextAtom()) |
| 86 | + /// print(context.read(TextAtom())) // Prints "Text" |
| 87 | + /// ``` |
| 88 | + /// |
| 89 | + /// - Parameter atom: An atom that associates the value. |
| 90 | + @inlinable |
| 91 | + public func reset(_ atom: some Atom) { |
| 92 | + _store.reset(atom) |
| 93 | + } |
| 94 | +} |
0 commit comments