Skip to content

Commit e75127b

Browse files
committed
[IMP] selection_stream: add unobserve method
The selection stream processor was missing a method to stop observing events after starting to observe them, which could lead to memory leak. The `AggregateStatisticsStore` was never releasing its observation, which was not a problem in practice because the store is never disposed, but let's fix it anyway. Task: 3149088 Part-of: #7241 Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
1 parent d0b1647 commit e75127b

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

packages/o-spreadsheet-engine/src/selection_stream/event_stream.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export class EventStream<Event> {
6565
this.observers.set(owner, { owner, callbacks });
6666
}
6767

68+
unobserve(owner: Owner) {
69+
this.observers.delete(owner);
70+
}
71+
6872
/**
6973
* Capture the stream for yourself
7074
*/

packages/o-spreadsheet-engine/src/selection_stream/selection_stream_processor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ export class SelectionStreamProcessorImpl implements SelectionStreamProcessor {
7777
this.stream.observe(owner, callbacks);
7878
}
7979

80+
unobserve(owner: unknown) {
81+
this.stream.unobserve(owner);
82+
}
83+
8084
release(owner: unknown) {
8185
if (this.stream.isListening(owner)) {
8286
this.stream.release(owner);

packages/o-spreadsheet-engine/src/types/selection_stream_processor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type StatefulStream<Event, State> = {
99
resetDefaultAnchor: (owner: unknown, state: State) => void;
1010
resetAnchor: (owner: unknown, state: State) => void;
1111
observe: (owner: unknown, callbacks: StreamCallbacks<Event>) => void;
12+
unobserve: (owner: unknown) => void;
1213
release: (owner: unknown) => void;
1314
getBackToDefault(): void;
1415
};

src/components/bottom_bar/bottom_bar_statistic/aggregate_statistics_store.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ export class AggregateStatisticsStore extends SpreadsheetStore {
7171
this.model.selection.observe(this, {
7272
handleEvent: this.handleEvent.bind(this),
7373
});
74+
this.onDispose(() => {
75+
this.model.selection.unobserve(this);
76+
});
7477
}
7578

7679
handle(cmd: Command) {

0 commit comments

Comments
 (0)