Skip to content

Commit b823815

Browse files
authored
feat(brownie): add static register method to BrownieStoreProtocol (#195)
Cleaner API for store initialization - replaces verbose `_ = Store(initialState, key: BrownfieldStore.storeName)` with `BrownfieldStore.register(initialState)`
1 parent 91a163c commit b823815

File tree

7 files changed

+24
-47
lines changed

7 files changed

+24
-47
lines changed

apps/AppleApp/Brownfield Apple App/BrownfieldAppleApp.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ struct BrownfieldAppleApp: App {
1616
ReactNativeBrownfield.shared.startReactNative {
1717
print("React Native has been loaded")
1818
}
19-
20-
_ = Store(initialState, key: BrownfieldStore.storeName)
19+
20+
BrownfieldStore.register(initialState)
2121
}
2222

2323
var body: some Scene {

apps/TesterIntegrated/swift/App.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct MyApp: App {
2121
print("loaded")
2222
}
2323

24-
_ = Store(initialState, key: BrownfieldStore.storeName)
24+
BrownfieldStore.register(initialState)
2525
}
2626

2727
var body: some Scene {

docs/docs/brownie/codegen.mdx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,6 @@ The generated struct:
5454
- Conforms to `BrownieStoreProtocol` with auto-generated `storeName`
5555
- Uses mutable `var` properties
5656

57-
## Auto-Generation Hooks
58-
59-
### iOS (Podfile)
60-
61-
Run codegen before pod install:
62-
63-
```ruby
64-
pre_install do |installer|
65-
system("npx", "brownie", "codegen", "-p", "swift")
66-
end
67-
```
6857

6958
## How It Works
7059

docs/docs/brownie/overview.mdx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,6 @@ npm install @callstack/brownie
4343
yarn add @callstack/brownie
4444
```
4545

46-
### iOS Setup
47-
48-
Add to your `Podfile`:
49-
50-
```ruby
51-
pre_install do |installer|
52-
system("npx", "brownfield", "codegen", "-p", "swift")
53-
end
54-
```
55-
56-
Then run `pod install`.
57-
5846
## Quick Example
5947

6048
**TypeScript (store definition):**

docs/docs/brownie/swift-usage.mdx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ struct MyApp: App {
2828
print("React Native loaded")
2929
}
3030

31-
// Initialize store with initial state
32-
_ = Store(initialState, key: BrownfieldStore.storeName)
31+
// Register store with initial state
32+
BrownfieldStore.register(initialState)
3333
}
3434

3535
var body: some Scene {
@@ -287,3 +287,10 @@ $counter.set { $0 + 1 } // increment using current value
287287
| `StoreManager.get(key:as:)` | Retrieve typed store by key |
288288
| `shared.snapshot(key:)` | Get raw snapshot dictionary |
289289
| `shared.removeStore(key:)` | Remove and cleanup store |
290+
291+
### BrownieStoreProtocol
292+
293+
| Method | Description |
294+
| -------------- | ------------------------------------- |
295+
| `register(_:)` | Register store with initial state |
296+
| `storeName` | Static property with store identifier |

packages/brownie/ArchitectureOverview.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ packages/brownie/
246246
- Observes `BrownieStoreUpdated` notification, rebuilds typed state from C++ snapshot
247247
- `deinit` removes notification observer
248248

249+
**BrownieStoreProtocol** - Protocol for generated store types:
250+
251+
- `storeName` - Static property with store identifier
252+
- `register(_:)` - Static method to register store with initial state (extension method)
253+
249254
**StoreManager** - Swift-side registry (delegates to C++):
250255

251256
- `register(store:key:)` - Register Swift store
@@ -406,22 +411,3 @@ The ObjC++ bridge layer has the most room for optimization:
406411
- **Single-property sync** - Currently `pushStateToCxx()` serializes entire state. Could track dirty properties and only sync changed values.
407412
- **Lazy Swift state rebuild** - Defer `Codable` struct reconstruction until property actually accessed.
408413
- **Direct C++ ↔ Swift interop** - Swift 5.9+ has experimental C++ interop that could bypass ObjC++ bridge entirely.
409-
410-
## Auto-generation Hooks
411-
412-
### iOS (Podfile)
413-
414-
```ruby
415-
pre_install do |installer|
416-
system("npx", "brownie", "codegen", "-p", "swift")
417-
end
418-
```
419-
420-
### Android (build.gradle.kts)
421-
422-
```kotlin
423-
tasks.register("generateBrownfieldStore") {
424-
exec { commandLine("npx", "brownie", "codegen", "-p", "kotlin") }
425-
}
426-
preBuild.dependsOn("generateBrownfieldStore")
427-
```

packages/brownie/ios/BrownieStore.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ public protocol BrownieStoreProtocol: Codable {
1010
static var storeName: String { get }
1111
}
1212

13+
public extension BrownieStoreProtocol {
14+
/// Registers the store with the given initial state.
15+
static func register(_ initialState: Self) {
16+
_ = Store(initialState, key: Self.storeName)
17+
}
18+
}
19+
1320
public struct StoreKey<State: Codable>: EnvironmentKey {
1421
public static var defaultValue: Store<State> { fatalError("Store not provided") }
1522
}

0 commit comments

Comments
 (0)