Skip to content

Documentation recreate feature - #11

Merged
JhonaCodes merged 7 commits into
mainfrom
documentation_recreate_feature
Dec 11, 2025
Merged

Documentation recreate feature#11
JhonaCodes merged 7 commits into
mainfrom
documentation_recreate_feature

Conversation

@JhonaCodes

Copy link
Copy Markdown
Owner

No description provided.

This implementation resolves the TODO that was causing infinite loops during
ViewModel initialization.

## Key Features

- **Factory Function Storage**: Stores the original factory function for
  recreation without requiring it to be passed again
- **Infinite Loop Protection**: Uses `_isRecreating` guard flag to prevent
  recursive recreation during ViewModel initialization
- **Proper Cleanup**: Properly disposes old ViewModel listeners via
  `stopListeningVM()` and `removeListeners()`
- **Lifecycle Management**: Maintains key, related states, and reference counts
- **Complete Notification**: Notifies all listeners and parent related states
- **Error Handling**: Comprehensive error handling with descriptive messages

## Use Cases

- After logout to create fresh user state
- Reset application state to initial values
- Testing scenarios requiring clean state
- Recovery from corrupted or invalid state

## Changes

### lib/src/notifier/reactive_notifier.dart
- Added `_createFunction` field to store factory function
- Added `_isRecreating` guard flag for infinite loop prevention
- Implemented complete `recreate()` method (lines 1311-1505)
- Added `_disposeOldNotifier()` helper method for cleanup
- Added `isRecreating` getter for debugging
- Modified `replaceNotifier()` to bypass circular update checks

### test/reactive_notifier_recreate_test.dart (new)
- 24 comprehensive tests covering:
  - Basic recreation with simple types
  - Recreation with ViewModels and AsyncViewModels
  - Infinite loop protection
  - Listener notification
  - Auto-dispose integration
  - Related states behavior
  - Edge cases and memory management

### CLAUDE.md
- Added `recreate()` to key methods list
- Added complete documentation section
- Included usage examples and comparison with `reinitializeInstance()`

All 24 tests pass successfully.
## Changes

### New Modular Documentation Structure

Created  directory with individual files for each API:
- context.md - BuildContext? getter with fallback
- has-context.md - Context availability check
- require-context.md - Required context with errors
- global-context.md - Direct global context access
- has-global-context.md - Global context availability
- require-global-context.md - Required global context
- init-context.md - ReactiveNotifier.initContext() setup
- wait-for-context.md - AsyncViewModel waitForContext parameter

### Builder Documentation

Created :
- Complete ReactiveContextBuilder documentation
- InheritedWidget vs markNeedsBuild strategies
- Performance considerations and examples

### Updated Files

- docs/features/context-access.md - Converted to index/overview
- docs/guides/context-pattern.md - Updated with modular references
- docs/README.md - Updated structure with context system
- docs/features/viewmodel.md - Updated context references
- docs/features/async-viewmodel.md - Added context access table

## Benefits

- Consistent with viewmodel/ and async-viewmodel/ modular structure
- Each API method/property has dedicated documentation
- Easy to find and reference specific functionality
- Better organization for maintenance and updates
@JhonaCodes
JhonaCodes merged commit 6c2953d into main Dec 11, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant