bloc_signals_replay
Replay, undo, and redo state tracking utilities for BlocSignal and CubitSignal state containers.
🌐 Ecosystem Packages
The BlocSignal monorepo consists of 14 modular packages:
⚡ Overview
bloc_signals_replay brings automatic undo and redo state management capabilities to BlocSignal and CubitSignal, mirroring Felix Angelov's replay_bloc package architecture.
ReplayMixin/ReplayCubit/ReplayCubitMixin: Undo and redo stack support forBlocSignalBaseand method-drivenCubitSignalcontainers.ReplayBloc/ReplayBlocMixin: Undo and redo stack support for event-drivenBlocSignalcontainers, with synthetic_Undoand_Redotransition tracing._ChangeStack: Configurable history limits andshouldReplaystate filtering.
🚀 Quick Start
ReplayCubit
Extend ReplayCubit to automatically track state changes:
import 'package:bloc_signals_replay/bloc_signals_replay.dart';
class CounterCubit extends ReplayCubit<int> {
CounterCubit() : super(initialState: 0);
void increment() => emit(stateValue + 1);
}
void main() {
final cubit = CounterCubit();
cubit.increment(); // state is 1
print(cubit.canUndo); // true
cubit.undo(); // state reverts to 0
print(cubit.canRedo); // true
cubit.redo(); // state becomes 1
}
ReplayBloc
Extend ReplayBloc for event-driven state containers:
import 'package:bloc_signals_replay/bloc_signals_replay.dart';
sealed class CounterEvent extends ReplayEvent {
const CounterEvent();
}
final class Increment extends CounterEvent {
const Increment();
}
class CounterBloc extends ReplayBloc<CounterEvent, int> {
CounterBloc() : super(initialState: 0) {
on<Increment>((event, emit) => emit(stateValue + 1));
}
}
void main() {
final bloc = CounterBloc();
bloc.add(const Increment()); // state is 1
bloc.undo(); // state is 0
bloc.redo(); // state is 1
}
Stack Bounds, State Filtering & Transition Coalescing
Pass a limit parameter, override shouldReplay to filter states, or override shouldCoalesce(previousState, currentState, nextState) to merge continuous edits (such as slider drags or rapid typing) into a single undo/redo step:
class BoundedCubit extends ReplayCubit<int> {
// Cap undo stack depth to 5 states
BoundedCubit() : super(initialState: 0, limit: 5);
// Skip even states during replay
@override
bool shouldReplay(int state) => !state.isEven;
// Coalesce consecutive increments within the same editing burst
@override
bool shouldCoalesce(int previousState, int currentState, int nextState) {
return (nextState - currentState).abs() == 1 &&
(currentState - previousState).abs() == 1;
}
}
Memory Management & Sensitive State History
Because ReplayMixin retains historical state instances in memory to power undo() and redo(), take the following precautions when managing large or sensitive state:
- Bound History Depth (
limit): Passlimit:(ormaxHistoryLength:) in the constructor or assignlimitat runtime to cap retained states. Setlimit = 0during sensitive flows (for example password entry, authentication tokens, or personally identifiable information) to clear existing history and disable recording. - Clear at Session Boundaries (
clearHistory()): CallclearHistory()explicitly at logout or session boundaries to immediately release retained state references. When composed withHydratedMixin, callingawait clear()automatically invokesonReset()and clears the replay stack in both mixin composition orders. - Reactive
canUndo&canRedo:canUndo,canRedo,canUndoSignal, andcanRedoSignalare backed by reactiveComputed<bool>signals (autoDispose: false) and update atomically withstateValueinsidebatch(), socontext.select((c) => c.canUndo),Watch,computed(), andeffect()rebuild automatically. - Automatic Cleanup on Disposal (
close()): Callingclose()automatically invokesclearHistory(), releasing all retained state references and resettingcanUndoandcanRedotofalse.
Libraries
- bloc_signals_replay
- Replay, undo, and redo state tracking utilities for
BlocSignalstate containers.