11package com.callstack.brownie
22
3- import java.util.concurrent.ConcurrentHashMap
4- import java.util.concurrent.atomic.AtomicBoolean
5-
63/* *
74 * Registers a new [Store] instance from this definition and the provided initial state.
85 */
@@ -27,26 +24,17 @@ inline fun <reified State : Any> registerStore(
2724 initialState : State ,
2825): Store <State > = registerStore(storeName, initialState, State ::class .java)
2926
30- private object BrownieStoreRegistrationTracker {
31- private val didRegisterByKey = ConcurrentHashMap <String , AtomicBoolean >()
32-
33- /* *
34- * Returns true only for the first registration attempt of a given store key.
35- */
36- fun markRegistered (key : String ): Boolean {
37- val registrationFlag = didRegisterByKey.getOrPut(key) { AtomicBoolean (false ) }
38- return registrationFlag.compareAndSet(false , true )
39- }
40- }
41-
4227/* *
4328 * Registers once per store name and returns null when the store was already registered.
29+ *
30+ * Idempotency is based on whether a store with this [storeName] currently exists
31+ * in [StoreManager], so clearing or removing a store allows registration again
32+ * within the same process.
4433 */
4534fun <State > BrownieStoreDefinition<State>.registerIfNeeded (initialState : () -> State ): Store <State >? {
46- if ( ! BrownieStoreRegistrationTracker .markRegistered (storeName) ) {
47- return null
35+ return StoreManager .shared.registerIfAbsent (storeName) {
36+ register(initialState())
4837 }
49- return register(initialState())
5038}
5139
5240/* *
0 commit comments