Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add controller messaging system #377

Merged
merged 6 commits into from
Mar 11, 2021
Merged

Add controller messaging system #377

merged 6 commits into from
Mar 11, 2021

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Mar 5, 2021

Adds a controller messaging system, which will be used to facilitate inter-controller communication. The controller messenger acts as a message broker, passing actions and events back and forth between controllers. It is fully type safe.

This idea was described in the Controller Messaging System proposal.

This controller messenger doesn't yet support attenuation, so it's not yet possible to restrict which actions and events are interacted with. That will be coming in a later PR.

@Gudahtt Gudahtt force-pushed the controller-messaging branch from 0e63938 to e803cc0 Compare March 5, 2021 18:22
src/ControllerMessenger.ts Outdated Show resolved Hide resolved
@Gudahtt Gudahtt force-pushed the controller-messaging branch 4 times, most recently from c86d06e to 159d49e Compare March 8, 2021 14:29
@Gudahtt Gudahtt force-pushed the controller-messaging branch from 159d49e to 756e986 Compare March 8, 2021 14:38
: never;
export type ExtractEventPayload<Event, T> = Event extends { type: T; payload: infer P } ? P : never;

type ActionConstraint = { type: string; handler: (...args: any) => unknown };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to get rid of this any 🤔 Using unknown or unknown[] seemed to upset TypeScript. What I wanted here was to express that I don't know anything about these parameters, but I don't know how to do that.

Though, maybe it's fine to leave this as-is. There are unit tests for zero parameters, one parameter, and multiple parameters (of various different types). Those should be sufficient for ensuring we're not assuming anything about these parameters in this class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When i was trying to figure out types I had to do some 'any' to allow it to work, even though I had full type safety in practice. Hard to explain/understand why that was true but I think it's fine as is

@Gudahtt Gudahtt marked this pull request as ready for review March 8, 2021 14:44
@Gudahtt Gudahtt requested a review from a team as a code owner March 8, 2021 14:44
@Gudahtt
Copy link
Member Author

Gudahtt commented Mar 8, 2021

Attenuation is added in this follow-up PR: #378. It's still a work-in-progress, but that should give some idea what attenuation could look like.

@Gudahtt
Copy link
Member Author

Gudahtt commented Mar 8, 2021

I don't yet have tests for ensuring this is type-safe. There is no straightforward way of testing this, as any misuse of types would fail to compile, and JavaScript tests wouldn't test the types at all.

There are some libraries that could help here though. I've opened #379 for investigating this further.

In the meantime I've been testing that this is type-safe by manually editing the unit tests into various type violations, and ensuring I get an error when I run the tests.

src/BaseControllerV2.ts Outdated Show resolved Hide resolved
src/BaseControllerV2.ts Outdated Show resolved Hide resolved
brad-decker
brad-decker previously approved these changes Mar 8, 2021
Copy link
Contributor

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the removal of base controller subscribe/unsubscribe can happen here or in a follow up. Happy to see this taking shape, and can't wait to use it in the extension :)

brad-decker
brad-decker previously approved these changes Mar 8, 2021
Copy link
Contributor

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Gudahtt
Copy link
Member Author

Gudahtt commented Mar 8, 2021

Thanks for the review @brad-decker ! Unfortunately I've just discovered that I forgot to document all of these methods though 🤦. So I'm gonna push one more commit in a few minutes to address that shortcoming.

@Gudahtt
Copy link
Member Author

Gudahtt commented Mar 9, 2021

Alright, all methods should now be documented! This is ready for review again.

brad-decker
brad-decker previously approved these changes Mar 9, 2021
Copy link
Contributor

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs LGTM

@Gudahtt
Copy link
Member Author

Gudahtt commented Mar 9, 2021

It'd be nice to get more feedback on this one before merging since it'll have a large impact upon future controller work, so I've tagged more people for review. No pressure though. I'll leave this open another couple of days, or until it gets another approval or two.

@Gudahtt Gudahtt force-pushed the controller-messaging branch from c4fb8f4 to c267a31 Compare March 11, 2021 04:14
Gudahtt added 6 commits March 11, 2021 13:21
Adds a controller messaging system, which will be used to facilitate
inter-controller communication. The controller messenger acts as a
message broker, passing actions and events back and forth between
controllers. It is _fully type safe_.

This idea was described in the Controller Messaging System proposal[1].

This controller messenger doesn't yet support attenuation, so it's not
yet possible to restrict which actions and events are interacted with.
That will be coming in a later PR.

[1]: https://www.notion.so/Controller-Messaging-System-617efb02b9e54bd0a0b0e44c6f776d85
The base controller no longer exposes methods to subscribe and
unsubscribe to state changes. Instead all subscriptions and
unsubscriptions are done with the controller messaging system.
This event name matches the naming convention of the event prefix,
which is also camelCase.
JSDoc comments have been added for each method on the controller
messenger instance.

Additionally, the `event` parameter has been renamed to `eventType`,
and the `action` parameter has been renamed to `actionType`. This was
done while adding docs because it made the documentation easier to
understand.
These types were only used internally. We can export them again if and
when that becomes useful.
@Gudahtt Gudahtt force-pushed the controller-messaging branch from c267a31 to 1671284 Compare March 11, 2021 16:52
Copy link
Contributor

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still LGTM.

@Gudahtt Gudahtt merged commit 1416de9 into develop Mar 11, 2021
@Gudahtt Gudahtt deleted the controller-messaging branch March 11, 2021 17:32
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
Adds a controller messaging system, which will be used to facilitate
inter-controller communication. The controller messenger acts as a
message broker, passing actions and events back and forth between
controllers. It is _fully type safe_.

This idea was described in the Controller Messaging System proposal[1].

This controller messenger doesn't yet support attenuation, so it's not
yet possible to restrict which actions and events are interacted with.
That will be coming in a later PR.

[1]: https://www.notion.so/Controller-Messaging-System-617efb02b9e54bd0a0b0e44c6f776d85

* Remove `subscribe` and `unsubscribe` BaseController methods

The base controller no longer exposes methods to subscribe and
unsubscribe to state changes. Instead all subscriptions and
unsubscriptions are done with the controller messaging system.
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
Adds a controller messaging system, which will be used to facilitate
inter-controller communication. The controller messenger acts as a
message broker, passing actions and events back and forth between
controllers. It is _fully type safe_.

This idea was described in the Controller Messaging System proposal[1].

This controller messenger doesn't yet support attenuation, so it's not
yet possible to restrict which actions and events are interacted with.
That will be coming in a later PR.

[1]: https://www.notion.so/Controller-Messaging-System-617efb02b9e54bd0a0b0e44c6f776d85

* Remove `subscribe` and `unsubscribe` BaseController methods

The base controller no longer exposes methods to subscribe and
unsubscribe to state changes. Instead all subscriptions and
unsubscriptions are done with the controller messaging system.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants