Skip to content

Commit

Permalink
feat: be able to unsubscribe from a gwm event
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Aug 27, 2023
1 parent 6abcb5e commit 2634f14
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Workspace,
GwmCommand,
Container,
EventSubscription,
} from './types';
import { resolveWebSocketApi } from './websocket';

Expand Down Expand Up @@ -106,7 +107,7 @@ export class GwmClient {
*
* @param command WM command to run (eg. "focus workspace 1").
* @param contextContainer (optional) Container or ID of container to use as
* context.
* context. If not provided, this defaults to the currently focused container.
* @throws If command fails.
*/
async runCommand(
Expand Down Expand Up @@ -185,7 +186,9 @@ export class GwmClient {
events: T,
callback: SubscribeCallback<T[number]>,
): Promise<UnlistenFn> {
await this.sendAndWaitReply(`subscribe -e ${events.join(',')}`);
const response = await this.sendAndWaitReply<EventSubscription>(
`subscribe -e ${events.join(',')}`,
);

const unlisten = this.onMessage((e) => {
const serverMessage: ServerMessage<GwmEventData> = JSON.parse(e.data);
Expand All @@ -199,10 +202,12 @@ export class GwmClient {
}
});

// TODO: Properly unsubscribe from the event(s). Use `async` here to prevent
// breaking API changes in the future.
return async () => {
unlisten();

await this.sendAndWaitReply<EventSubscription>(
`unsubscribe ${response.subscriptionId}`,
);
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/types/client-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type SubscribeMessage =
| `subscribe --events ${string}`
| `subscribe -e ${string}`;

export type UnsubscribeMessage = `unsubscribe ${string}`;

export type InvokeCommandMessage =
| `command "${GwmCommand}"`
| `command "${GwmCommand}" --context-container-id ${string}`
Expand All @@ -16,6 +18,7 @@ export type GetWindowsMessage = 'windows';

export type ClientMessage =
| SubscribeMessage
| UnsubscribeMessage
| InvokeCommandMessage
| GetMonitorsMessage
| GetWorkspacesMessage
Expand Down
3 changes: 3 additions & 0 deletions src/types/shared/event-subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface EventSubscription {
subscriptionId: string;
}
1 change: 1 addition & 0 deletions src/types/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './border-delta';
export * from './container';
export * from './direction';
export * from './event-subscription';
export * from './floating-placement';
export * from './floating-window';
export * from './maximized-window';
Expand Down

0 comments on commit 2634f14

Please sign in to comment.