Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Types are hard to work out when adding event listeners to the CogsConnection #102

@tomdixon

Description

@tomdixon

You often need to assign a listener so that you can reference it when removing it. This leads to needing to write out the type for the argument of the listener which isn't simple to do currently.

We should look at adding a utility type here, possibly for the whole listener.

Current code for this looks something like:

  useEffect(() => {
    const listener = (
      event: CogsStateChangedEvent<
        Partial<ManifestTypes.StateAsObject<typeof manifest>>
      >
    ) => {
      if (event.state.State) {
        try {
          // TODO: Do something with state
        } catch (e) {
          console.error("Couldn't parse incoming state", e);
        }
      }
    };

    connection.addEventListener("state", listener);

    return () => {
      connection.removeEventListener("state", listener);
    };
  }, [connection]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions