Skip to content

Commit

Permalink
Add getState action to BaseControllerV2 (#457)
Browse files Browse the repository at this point in the history
This allows getting the state of the controller via the controller
messenger.
  • Loading branch information
Gudahtt authored and MajorLift committed Oct 11, 2023
1 parent fabd56c commit 9ca7299
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/BaseControllerV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ describe('BaseController', () => {
expect(controller.state).toStrictEqual({ count: 0 });
});

it('should allow getting state via the getState action', () => {
const controllerMessenger = new ControllerMessenger<
CountControllerAction,
CountControllerEvent
>();
new CountController({
messenger: getCountMessenger(controllerMessenger),
name: countControllerName,
state: { count: 0 },
metadata: countControllerStateMetadata,
});

expect(controllerMessenger.call('CountController:getState')).toStrictEqual({
count: 0,
});
});

it('should set initial schema', () => {
const controller = new CountController({
messenger: getCountMessenger(),
Expand Down
5 changes: 5 additions & 0 deletions src/BaseControllerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ export class BaseController<
this.name = name;
this.internalState = state;
this.metadata = metadata;

this.messagingSystem.registerActionHandler(
`${name}:getState`,
() => this.state,
);
}

/**
Expand Down

0 comments on commit 9ca7299

Please sign in to comment.