Skip to content

Commit

Permalink
fix: prevent race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjalle-S committed Jun 25, 2024
1 parent 24b826d commit 7999919
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/base/eventListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* (Department of Information and Computing Sciences)
*/
import type { Settings } from ".";
import type { ReceiveMessage, SendMessage } from "../internal";
import type { ReadyMessage, ReceiveMessage, SendMessage } from "../internal";

/**
* Performs an action every time a message of the specified type is received.
Expand Down Expand Up @@ -36,12 +36,17 @@ export function receiveMessage<
if (data.type === typeFilter) callback(data.data as TData);
}

const readyMessage: ReadyMessage = {
data: undefined,
type: `${typeFilter}Ready`
};

windowContext.addEventListener("message", updateMessage);
sendMessage(readyMessage);

return () => windowContext.removeEventListener("message", updateMessage);
}


/**
* Performs an action every time a message with settings is received.
*
Expand Down
2 changes: 1 addition & 1 deletion src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* @packageDocumentation Contains the basic GraphPolaris ml settings plugin API. This is required for any plugin, regardless of the framework used.
*
* @categoryDescription Message event listeners
* These are functions to listen for specific types of messages arriving.
*/
Expand Down
20 changes: 13 additions & 7 deletions src/internal/message.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
* (Department of Information and Computing Sciences)
*/

import type {
Settings
} from "../base";
import type { Settings } from "../base";

/**
* The base message type for all messages.
Expand Down Expand Up @@ -45,18 +43,26 @@ export interface SettingsRequestMessage extends BaseMessage {
data: undefined;
}

/**
* A message that notifies the frontend that the add-on wants to receive data.
*
* @internal
*/
export interface ReadyMessage extends BaseMessage {
data: undefined;
type: `${ReceiveMessage["type"]}Ready`;
}

/**
* The types of messages that an add-on can receive.
*
* @internal
*/
export type ReceiveMessage =
| SettingsMessage
| SettingsRequestMessage;
export type ReceiveMessage = SettingsMessage | SettingsRequestMessage;

/**
* The types of messages that an add-on can send.
*
* @internal
*/
export type SendMessage = SettingsMessage;
export type SendMessage = ReadyMessage | SettingsMessage;
6 changes: 1 addition & 5 deletions src/react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import { createContext, useContext, useEffect, useMemo, useState } from "react";

import type { ReceiveMessage } from "../internal";

import {
type Settings,
receiveMessage,
sendMessage
} from "../base";
import { type Settings, receiveMessage, sendMessage } from "../base";

/**
* The context for providing the window object to the hooks and components.
Expand Down

0 comments on commit 7999919

Please sign in to comment.