Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/events/ComponentEventsObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,28 @@ describe('ComponentEventsObserver', () => {
expect(didAppearFn).not.toHaveBeenCalled();
});

it.each(['component.with.dots', 'toString', '__proto__'])(
'removes all listeners for the %s component id',
(componentId) => {
const listener = jest.fn();
const registered = uut.registerComponentListener(
{ componentDidDisappear: listener },
componentId
);

uut.unmounted(componentId);
uut.notifyComponentDidDisappear({
componentId,
componentName: 'doesnt matter',
componentType: 'Component',
});
const callCount = listener.mock.calls.length;
registered.remove();

expect(callCount).toBe(0);
}
);

it(`supports multiple listeners with same componentId`, () => {
const screen1 = render(<SimpleScreen componentId={'myCompId'} />);
const screen2 = render(<SimpleScreen componentId={'myCompId'} />);
Expand Down
9 changes: 5 additions & 4 deletions src/events/ComponentEventsObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Component } from 'react';
import isString from 'lodash/isString';
import isNil from 'lodash/isNil';
import uniqueId from 'lodash/uniqueId';
import unset from 'lodash/unset';
import forEach from 'lodash/forEach';
import { EventSubscription } from '../interfaces/EventSubscription';
import { NavigationComponentListener } from '../interfaces/NavigationComponentListener';
Expand All @@ -23,7 +22,9 @@ import { Store } from '../components/Store';
type ReactComponentWithIndexing = NavigationComponentListener & Record<string, any>;

export class ComponentEventsObserver {
private listeners: Record<string, Record<string, ReactComponentWithIndexing>> = {};
private listeners: Record<string, Record<string, ReactComponentWithIndexing>> = Object.create(
null
);
private alreadyRegistered = false;

constructor(
Expand Down Expand Up @@ -92,11 +93,11 @@ export class ComponentEventsObserver {
const key = uniqueId();
this.listeners[componentId][key] = listener;

return { remove: () => unset(this.listeners[componentId], key) };
return { remove: () => delete this.listeners[componentId]?.[key] };
}

public unmounted(componentId: string) {
unset(this.listeners, componentId);
delete this.listeners[componentId];
}

notifyComponentWillAppear(event: ComponentWillAppearEvent) {
Expand Down