Skip to content

Commit 3149256

Browse files
committed
Simplify subscription management - usually no manual cleanup needed (GC handles it)
1 parent 2420307 commit 3149256

File tree

3 files changed

+14
-57
lines changed

3 files changed

+14
-57
lines changed

code_samples/lib/command_it/command_chaining_cleanup.dart

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,6 @@ class CleanupManager {
3131
}
3232
// #endregion cleanup_basic
3333

34-
// #region cleanup_multiple
35-
class MultiPipeManager {
36-
late final inputCommand = Command.createSync<String, String>(
37-
(s) => s,
38-
initialValue: '',
39-
);
40-
41-
late final saveCommand = Command.createAsyncNoResult<String>(
42-
(s) async => api.saveContent(s),
43-
);
44-
45-
late final logCommand = Command.createSync<String, void>(
46-
(s) => print('Logged: $s'),
47-
initialValue: null,
48-
);
49-
50-
late final analyticsCommand = Command.createSync<String, void>(
51-
(s) => print('Analytics: $s'),
52-
initialValue: null,
53-
);
54-
55-
// Multiple subscriptions
56-
final List<ListenableSubscription> _subscriptions = [];
57-
58-
MultiPipeManager() {
59-
_subscriptions.addAll([
60-
inputCommand.pipeToCommand(saveCommand),
61-
inputCommand.pipeToCommand(logCommand),
62-
inputCommand.pipeToCommand(analyticsCommand),
63-
]);
64-
}
65-
66-
void dispose() {
67-
// Cancel all subscriptions
68-
for (final sub in _subscriptions) {
69-
sub.cancel();
70-
}
71-
_subscriptions.clear();
72-
73-
inputCommand.dispose();
74-
saveCommand.dispose();
75-
logCommand.dispose();
76-
analyticsCommand.dispose();
77-
}
78-
}
79-
// #endregion cleanup_multiple
80-
8134
void main() {
8235
// Examples compile but don't run
8336
}

docs/documentation/command_it/command_chaining.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,17 @@ The real power comes from combining `pipeToCommand` with [listen_it operators](/
127127

128128
## Subscription Management
129129

130-
`pipeToCommand` returns a `ListenableSubscription`. Always store and cancel it to prevent memory leaks.
130+
`pipeToCommand` returns a `ListenableSubscription`. However, **you usually don't need to manage it manually**.
131131

132-
### Basic Cleanup
132+
::: tip Automatic Cleanup
133+
When commands and pipes are in the same object (manager/service class), the subscription is automatically garbage collected when the containing object becomes unreachable. No manual cleanup needed!
133134

134-
<<< @/../code_samples/lib/command_it/command_chaining_cleanup.dart#cleanup_basic
135+
See [listen_it Memory Management](/documentation/listen_it/best_practices#understanding-chain-garbage-collection) for details.
136+
:::
135137

136-
### Multiple Subscriptions
138+
For cases where you do need manual control (e.g., dynamically created pipes with operators):
137139

138-
<<< @/../code_samples/lib/command_it/command_chaining_cleanup.dart#cleanup_multiple
140+
<<< @/../code_samples/lib/command_it/command_chaining_cleanup.dart#cleanup_basic
139141

140142
## Warning: Circular Pipes
141143

docs/es/documentation/command_it/command_chaining.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,17 @@ El verdadero poder viene de combinar `pipeToCommand` con [operadores de listen_i
127127

128128
## Gestión de Suscripciones
129129

130-
`pipeToCommand` retorna un `ListenableSubscription`. Siempre guárdalo y cancélalo para prevenir memory leaks.
130+
`pipeToCommand` retorna un `ListenableSubscription`. Sin embargo, **usualmente no necesitas gestionarlo manualmente**.
131131

132-
### Limpieza Básica
132+
::: tip Limpieza Automática
133+
Cuando los commands y pipes están en el mismo objeto (clase manager/servicio), la suscripción es automáticamente recolectada por el garbage collector cuando el objeto contenedor se vuelve inalcanzable. ¡No se necesita limpieza manual!
133134

134-
<<< @/../code_samples/lib/command_it/command_chaining_cleanup.dart#cleanup_basic
135+
Ver [Gestión de Memoria de listen_it](/es/documentation/listen_it/best_practices#understanding-chain-garbage-collection) para detalles.
136+
:::
135137

136-
### Múltiples Suscripciones
138+
Para casos donde necesitas control manual (ej., pipes creados dinámicamente con operadores):
137139

138-
<<< @/../code_samples/lib/command_it/command_chaining_cleanup.dart#cleanup_multiple
140+
<<< @/../code_samples/lib/command_it/command_chaining_cleanup.dart#cleanup_basic
139141

140142
## Advertencia: Pipes Circulares
141143

0 commit comments

Comments
 (0)