Skip to content

Commit

Permalink
Restore ComposableController compatibility between different BaseCont…
Browse files Browse the repository at this point in the history
…rollers (#458)

When adding support for BaseControllerV2 to the ComposableController
in #447, we accidentally removed support for using two different
versions of the BaseController in the same ComposableController. This
is because we used the `instanceof` operator to check whether a
controller was extended from BaseController.

The `instanceof` check has been replaced by a check for the `subscribe`
function. It seems unlikely that a BaseControllerV2 controller will
ever have a `subscribe` function because the controller messenger
will be used for all events.

The BaseControllerV2 class now prevents the use of the `subscribe`
property, ensuring the duck typing strategy used by the
ComposableController to tell old and new controllers apart continues to
work.
  • Loading branch information
Gudahtt authored and MajorLift committed Oct 11, 2023
1 parent 02353d5 commit 2b9d6d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/BaseControllerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ export class BaseController<

public readonly metadata: StateMetadata<S>;

/**
* The existence of the `subscribe` property is how the ComposableController detects whether a
* controller extends the old BaseController or the new one. We set it to `never` here to ensure
* this property is never used for new BaseController-based controllers, to ensure the
* ComposableController never mistakes them for an older style controller.
*/
public readonly subscribe: never;

/**
* Creates a BaseController instance.
*
Expand Down
4 changes: 2 additions & 2 deletions src/ComposableController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export class ComposableController extends BaseController<never, any> {
this.messagingSystem = messenger;
this.controllers.forEach((controller) => {
const { name } = controller;
if (controller instanceof BaseController) {
controller.subscribe((state) => {
if ((controller as BaseController<any, any>).subscribe !== undefined) {
(controller as BaseController<any, any>).subscribe((state) => {
this.update({ [name]: state });
});
} else if (this.messagingSystem) {
Expand Down

0 comments on commit 2b9d6d9

Please sign in to comment.