Skip to content

Commit

Permalink
Add removeListener for ClusterClients
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Jan 1, 2025
1 parent 5a59106 commit 768a142
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The main work (all changes without a GitHub username in brackets in the below li
- Feature: Allows to update the Fabric Label during controller runtime using `updateFabricLabel()` on CommissioningController
- Enhancement: Improves Reconnection Handling for devices that use persisted subscriptions
- Enhancement: Use data type definitions from Model for Controller Device type definitions
- Enhancement: Added `remove*Listener()` to ClusterClient objects to remove listeners added with `add*Listener()` or `subscribe*()` (The subscription is not cleared!)
- Fix: When a paired node gets disconnected (or decommissioned) invalidate subscription handlers to prevent reconnection tries

## 0.11.9 (2024-12-11)
Expand Down
6 changes: 6 additions & 0 deletions packages/protocol/src/cluster/client/ClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export function ClusterClient<const T extends ClusterType>(
result[`add${capitalizedAttributeName}AttributeListener`] = <T>(listener: (value: T) => void) => {
(attributes as any)[attributeName].addListener(listener);
};
result[`remove${capitalizedAttributeName}AttributeListener`] = <T>(listener: (value: T) => void) => {
(attributes as any)[attributeName].removeListener(listener);
};
}

function addEventToResult(event: Event<any, any>, eventName: string) {
Expand Down Expand Up @@ -126,6 +129,9 @@ export function ClusterClient<const T extends ClusterType>(
result[`add${capitalizedEventName}EventListener`] = <T>(listener: (value: DecodedEventData<T>) => void) => {
(events as any)[eventName].addListener(listener);
};
result[`remove${capitalizedEventName}EventListener`] = <T>(listener: (value: DecodedEventData<T>) => void) => {
(events as any)[eventName].removeListener(listener);
};
}

const {
Expand Down
8 changes: 8 additions & 0 deletions packages/protocol/src/cluster/client/ClusterClientTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ type ClientAttributeListeners<A extends Attributes> = {
[P in keyof A as `add${Capitalize<string & P>}AttributeListener`]: (
listener: (value: AttributeJsType<A[P]>) => void,
) => void;
} & {
[P in keyof A as `remove${Capitalize<string & P>}AttributeListener`]: (
listener: (value: AttributeJsType<A[P]>) => void,
) => void;
};

type CommandServers<C extends Commands> = { [P in keyof C]: SignatureFromCommandSpec<C[P]> };
Expand All @@ -133,6 +137,10 @@ type ClientEventListeners<E extends Events> = {
[P in keyof E as `add${Capitalize<string & P>}EventListener`]: (
listener: (value: DecodedEventData<EventType<E[P]>>) => void,
) => void;
} & {
[P in keyof E as `remove${Capitalize<string & P>}EventListener`]: (
listener: (value: DecodedEventData<EventType<E[P]>>) => void,
) => void;
};

/** Strongly typed interface of a cluster client */
Expand Down

0 comments on commit 768a142

Please sign in to comment.