zed 0.2.0

A minimal, Redux-like state management library for Rust with advanced features.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! # Store Module
//!
//! Redux-like store for centralized state management.
//!
//! ## Features
//!
//! - Thread-safe with `Arc<Mutex<T>>`
//! - Subscribe/unsubscribe to state changes
//! - Batch dispatch operations
//! - Dynamic reducer replacement
//! - Read-only state access
//!
//! ## Example
//!
//! ```rust
//! use zed::{Store, create_reducer};
//!
//! #[derive(Clone, Debug, PartialEq)]
//! struct AppState {
//!     count: i32,
//! }
//!
//! #[derive(Clone)]
//! enum Action {
//!     Increment,
//!     Decrement,
//! }
//!
//! fn reducer(state: &AppState, action: &Action) -> AppState {
//!     match action {
//!         Action::Increment => AppState { count: state.count + 1 },
//!         Action::Decrement => AppState { count: state.count - 1 },
//!     }
//! }
//!
//! # fn main() {
//! let store = Store::new(AppState { count: 0 }, Box::new(create_reducer(reducer)));
//!
//! // Subscribe and get an ID for later unsubscription
//! let subscription_id = store.subscribe(|state: &AppState| {
//!     println!("Count: {}", state.count);
//! });
//!
//! store.dispatch(Action::Increment);
//! assert_eq!(store.get_state().count, 1);
//!
//! // Unsubscribe when no longer needed
//! store.unsubscribe(subscription_id);
//! # }
//! ```

use crate::reducer::Reducer;
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

/// Type alias for subscription IDs
pub type SubscriptionId = usize;

type SharedState<S> = Arc<Mutex<S>>;
type Subscriber<State> = Box<dyn Fn(&State) + Send + Sync>;
type SubscriberMap<State> = Arc<Mutex<HashMap<SubscriptionId, Subscriber<State>>>>;

/// Redux-like store for centralized state management.
///
/// Thread-safe store with:
/// - Atomic state updates
/// - Subscriber notifications
/// - Batch dispatch support
/// - Dynamic reducer replacement
pub struct Store<State, Action> {
    state: SharedState<State>,
    reducer: Arc<Mutex<Box<dyn Reducer<State, Action> + Send + Sync>>>,
    subscribers: SubscriberMap<State>,
    next_subscriber_id: AtomicUsize,
}

impl<State: Clone + Send + 'static, Action: Send + 'static> Store<State, Action> {
    /// Creates a new store with the given initial state and reducer.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state of the store
    /// * `reducer` - A boxed reducer that handles state transitions
    ///
    /// # Example
    ///
    /// ```rust
    /// use zed::{Store, create_reducer};
    ///
    /// #[derive(Clone)]
    /// struct State { count: i32 }
    ///
    /// #[derive(Clone)]
    /// enum Action { Increment }
    ///
    /// let store = Store::new(
    ///     State { count: 0 },
    ///     Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 }))
    /// );
    /// ```
    pub fn new(
        initial_state: State,
        reducer: Box<dyn Reducer<State, Action> + Send + Sync>,
    ) -> Self {
        Self {
            state: Arc::new(Mutex::new(initial_state)),
            reducer: Arc::new(Mutex::new(reducer)),
            subscribers: Arc::new(Mutex::new(HashMap::new())),
            next_subscriber_id: AtomicUsize::new(0),
        }
    }

    /// Dispatches an action to update the state.
    ///
    /// This method applies the action to the current state using the reducer,
    /// updates the store's state, and notifies all subscribers.
    ///
    /// # Arguments
    ///
    /// * `action` - The action to dispatch
    ///
    /// # Example
    ///
    /// ```rust
    /// # use zed::{Store, create_reducer};
    /// # #[derive(Clone)] struct State { count: i32 }
    /// # #[derive(Clone)] enum Action { Increment }
    /// # let store = Store::new(State { count: 0 }, Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 })));
    /// store.dispatch(Action::Increment);
    /// ```
    pub fn dispatch(&self, action: Action) {
        // Hold state lock for the entire read-modify-write cycle to ensure atomicity
        let new_state = {
            let mut state = self.state.lock().unwrap();
            let reducer = self.reducer.lock().unwrap();
            let new_state = reducer.reduce(&state, &action);
            *state = new_state.clone();
            new_state
        };

        // Notify subscribers (separate lock to reduce contention)
        self.notify_subscribers(&new_state);
    }

    /// Dispatches multiple actions in a batch.
    ///
    /// This is more efficient than dispatching actions individually because
    /// subscribers are only notified once after all actions have been applied.
    ///
    /// # Arguments
    ///
    /// * `actions` - A vector of actions to dispatch
    ///
    /// # Example
    ///
    /// ```rust
    /// # use zed::{Store, create_reducer};
    /// # #[derive(Clone)] struct State { count: i32 }
    /// # #[derive(Clone)] enum Action { Increment }
    /// # let store = Store::new(State { count: 0 }, Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 })));
    /// // All three increments, but subscribers notified only once
    /// store.dispatch_batch(vec![Action::Increment, Action::Increment, Action::Increment]);
    /// assert_eq!(store.get_state().count, 3);
    /// ```
    pub fn dispatch_batch(&self, actions: Vec<Action>) {
        if actions.is_empty() {
            return;
        }

        let new_state = {
            let mut state = self.state.lock().unwrap();
            let reducer = self.reducer.lock().unwrap();

            for action in actions {
                let temp_state = reducer.reduce(&state, &action);
                *state = temp_state;
            }

            state.clone()
        };

        // Notify subscribers once after all actions
        self.notify_subscribers(&new_state);
    }

    /// Subscribes to state changes.
    ///
    /// The provided function will be called whenever the state is updated
    /// through a dispatch action. Returns a subscription ID that can be used
    /// to unsubscribe later.
    ///
    /// # Arguments
    ///
    /// * `f` - A function that will be called with the new state
    ///
    /// # Returns
    ///
    /// A `SubscriptionId` that can be used with `unsubscribe()` to cancel the subscription.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use zed::{Store, create_reducer};
    /// # #[derive(Clone)] struct State { count: i32 }
    /// # #[derive(Clone)] enum Action { Increment }
    /// # let store = Store::new(State { count: 0 }, Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 })));
    /// let id = store.subscribe(|state: &State| {
    ///     println!("Count is now: {}", state.count);
    /// });
    ///
    /// // Later, when you no longer need the subscription
    /// store.unsubscribe(id);
    /// ```
    pub fn subscribe<F>(&self, f: F) -> SubscriptionId
    where
        F: Fn(&State) + Send + Sync + 'static,
    {
        let id = self.next_subscriber_id.fetch_add(1, Ordering::SeqCst);
        self.subscribers.lock().unwrap().insert(id, Box::new(f));
        id
    }

    /// Unsubscribes a previously registered subscriber.
    ///
    /// # Arguments
    ///
    /// * `id` - The subscription ID returned by `subscribe()`
    ///
    /// # Returns
    ///
    /// `true` if the subscriber was found and removed, `false` if no subscriber
    /// with that ID exists.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use zed::{Store, create_reducer};
    /// # #[derive(Clone)] struct State { count: i32 }
    /// # #[derive(Clone)] enum Action { Increment }
    /// # let store = Store::new(State { count: 0 }, Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 })));
    /// let id = store.subscribe(|_| {});
    ///
    /// assert!(store.unsubscribe(id));  // Returns true - subscriber removed
    /// assert!(!store.unsubscribe(id)); // Returns false - already removed
    /// ```
    pub fn unsubscribe(&self, id: SubscriptionId) -> bool {
        self.subscribers.lock().unwrap().remove(&id).is_some()
    }

    /// Gets the current state.
    ///
    /// Returns a clone of the current state. This is safe to call from
    /// multiple threads concurrently.
    ///
    /// For read-only access without cloning, consider using `with_state()`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use zed::{Store, create_reducer};
    /// # #[derive(Clone)] struct State { count: i32 }
    /// # #[derive(Clone)] enum Action { Increment }
    /// # let store = Store::new(State { count: 0 }, Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 })));
    /// let current_state = store.get_state();
    /// println!("Current count: {}", current_state.count);
    /// ```
    pub fn get_state(&self) -> State {
        self.state.lock().unwrap().clone()
    }

    /// Accesses the state without cloning.
    ///
    /// This is useful for read-only access to the state when you don't need
    /// to keep a copy. The provided function receives an immutable reference
    /// to the state and can return a value.
    ///
    /// # Arguments
    ///
    /// * `f` - A function that takes an immutable reference to the state
    ///
    /// # Returns
    ///
    /// The return value of the provided function.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use zed::{Store, create_reducer};
    /// # #[derive(Clone)] struct State { count: i32 }
    /// # #[derive(Clone)] enum Action { Increment }
    /// # let store = Store::new(State { count: 0 }, Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 })));
    /// // Read state without cloning
    /// let double_count = store.with_state(|state| state.count * 2);
    ///
    /// // Check a condition without cloning
    /// let is_positive = store.with_state(|state| state.count > 0);
    /// ```
    pub fn with_state<R, F>(&self, f: F) -> R
    where
        F: FnOnce(&State) -> R,
    {
        let state = self.state.lock().unwrap();
        f(&state)
    }

    /// Replaces the current reducer with a new one.
    ///
    /// This is useful for hot-reloading scenarios or dynamic behavior changes.
    ///
    /// # Arguments
    ///
    /// * `new_reducer` - The new reducer to use for future dispatches
    ///
    /// # Example
    ///
    /// ```rust
    /// # use zed::{Store, create_reducer};
    /// # #[derive(Clone)] struct State { count: i32 }
    /// # #[derive(Clone)] enum Action { Increment }
    /// # let store = Store::new(State { count: 0 }, Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 })));
    /// // Replace with a reducer that increments by 2
    /// let new_reducer = create_reducer(|state: &State, _: &Action| State { count: state.count + 2 });
    /// store.replace_reducer(Box::new(new_reducer));
    /// ```
    pub fn replace_reducer(&self, new_reducer: Box<dyn Reducer<State, Action> + Send + Sync>) {
        let mut reducer = self.reducer.lock().unwrap();
        *reducer = new_reducer;
    }

    /// Returns the number of active subscribers.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use zed::{Store, create_reducer};
    /// # #[derive(Clone)] struct State { count: i32 }
    /// # #[derive(Clone)] enum Action { Increment }
    /// # let store = Store::new(State { count: 0 }, Box::new(create_reducer(|state: &State, _: &Action| State { count: state.count + 1 })));
    /// assert_eq!(store.subscriber_count(), 0);
    ///
    /// let id = store.subscribe(|_| {});
    /// assert_eq!(store.subscriber_count(), 1);
    ///
    /// store.unsubscribe(id);
    /// assert_eq!(store.subscriber_count(), 0);
    /// ```
    pub fn subscriber_count(&self) -> usize {
        self.subscribers.lock().unwrap().len()
    }

    /// Internal helper to notify all subscribers
    fn notify_subscribers(&self, new_state: &State) {
        let subscribers = self.subscribers.lock().unwrap();
        for subscriber in subscribers.values() {
            subscriber(new_state);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::create_reducer;
    use std::sync::Arc;
    use std::thread;
    use std::time::Duration;

    #[derive(Clone, Debug, PartialEq)]
    struct TestState {
        counter: i32,
    }

    #[derive(Clone)]
    enum TestAction {
        Increment,
        Decrement,
        SetValue(i32),
    }

    fn create_test_store() -> Store<TestState, TestAction> {
        let reducer = create_reducer(|state: &TestState, action: &TestAction| match action {
            TestAction::Increment => TestState {
                counter: state.counter + 1,
            },
            TestAction::Decrement => TestState {
                counter: state.counter - 1,
            },
            TestAction::SetValue(val) => TestState { counter: *val },
        });

        Store::new(TestState { counter: 0 }, Box::new(reducer))
    }

    #[test]
    fn test_basic_operations() {
        let store = create_test_store();

        assert_eq!(store.get_state().counter, 0);

        store.dispatch(TestAction::Increment);
        assert_eq!(store.get_state().counter, 1);

        store.dispatch(TestAction::Decrement);
        assert_eq!(store.get_state().counter, 0);

        store.dispatch(TestAction::SetValue(42));
        assert_eq!(store.get_state().counter, 42);
    }

    #[test]
    fn test_subscribe_and_unsubscribe() {
        let store = create_test_store();
        let notifications = Arc::new(Mutex::new(Vec::new()));
        let notifications_clone = notifications.clone();

        assert_eq!(store.subscriber_count(), 0);

        let id = store.subscribe(move |state| {
            notifications_clone.lock().unwrap().push(state.counter);
        });

        assert_eq!(store.subscriber_count(), 1);

        store.dispatch(TestAction::Increment);
        store.dispatch(TestAction::Increment);

        thread::sleep(Duration::from_millis(10));

        {
            let notifs = notifications.lock().unwrap();
            assert_eq!(notifs.len(), 2);
            assert_eq!(notifs[0], 1);
            assert_eq!(notifs[1], 2);
        }

        // Unsubscribe
        assert!(store.unsubscribe(id));
        assert_eq!(store.subscriber_count(), 0);
        assert!(!store.unsubscribe(id)); // Should return false for non-existent ID

        // Dispatch after unsubscribe - no more notifications
        store.dispatch(TestAction::Increment);
        thread::sleep(Duration::from_millis(10));

        let notifs = notifications.lock().unwrap();
        assert_eq!(notifs.len(), 2); // Still 2, not 3
    }

    #[test]
    fn test_dispatch_batch() {
        let store = create_test_store();
        let notifications = Arc::new(Mutex::new(Vec::new()));
        let notifications_clone = notifications.clone();

        store.subscribe(move |state| {
            notifications_clone.lock().unwrap().push(state.counter);
        });

        // Batch dispatch - should only notify once
        store.dispatch_batch(vec![
            TestAction::Increment,
            TestAction::Increment,
            TestAction::Increment,
        ]);

        thread::sleep(Duration::from_millis(10));

        let notifs = notifications.lock().unwrap();
        assert_eq!(notifs.len(), 1); // Only one notification
        assert_eq!(notifs[0], 3); // Final state after all actions
        assert_eq!(store.get_state().counter, 3);
    }

    #[test]
    fn test_with_state() {
        let store = create_test_store();
        store.dispatch(TestAction::SetValue(100));

        // Read without cloning
        let result = store.with_state(|state| state.counter * 2);
        assert_eq!(result, 200);

        // Original state unchanged
        assert_eq!(store.get_state().counter, 100);
    }

    #[test]
    fn test_concurrent_access() {
        let store = Arc::new(create_test_store());
        let mut handles = vec![];

        for _ in 0..10 {
            let store_clone = store.clone();
            let handle = thread::spawn(move || {
                for _ in 0..100 {
                    store_clone.dispatch(TestAction::Increment);
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(store.get_state().counter, 1000);
    }

    #[test]
    fn test_replace_reducer() {
        let store = create_test_store();

        store.dispatch(TestAction::Increment);
        assert_eq!(store.get_state().counter, 1);

        // Replace with a reducer that increments by 10
        let new_reducer = create_reducer(|state: &TestState, action: &TestAction| match action {
            TestAction::Increment => TestState {
                counter: state.counter + 10,
            },
            _ => state.clone(),
        });

        store.replace_reducer(Box::new(new_reducer));

        store.dispatch(TestAction::Increment);
        assert_eq!(store.get_state().counter, 11); // 1 + 10
    }
}