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

feat(base-controller): Allow using internal events/actions #2050

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<CountAction, never>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: ['CountController:count'],
});

let count = 0;
Expand All @@ -36,7 +35,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<MessageAction, never>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedActions: ['MessageController:reset', 'MessageController:concat'],
});

let message = '';
Expand Down Expand Up @@ -71,7 +69,6 @@ describe('RestrictedControllerMessenger', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: ['CountController:increment'],
});

let count = 0;
Expand All @@ -94,7 +91,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<MessageAction, never>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedActions: ['MessageController:message'],
});

const messages: Record<string, string> = {};
Expand Down Expand Up @@ -122,7 +118,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<AddAction, never>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MathController',
allowedActions: ['MathController:add'],
});

restrictedControllerMessenger.registerActionHandler(
Expand All @@ -145,7 +140,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<CountAction, never>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'PingController',
allowedActions: ['PingController:ping'],
});

restrictedControllerMessenger.registerActionHandler(
Expand All @@ -166,7 +160,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<CountAction, never>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'PingController',
allowedActions: ['PingController:ping'],
});

expect(() => {
Expand All @@ -179,7 +172,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<PingAction, never>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'PingController',
allowedActions: ['PingController:ping'],
});

expect(() => {
Expand Down Expand Up @@ -212,7 +204,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

const handler = sinon.stub();
Expand All @@ -234,7 +225,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:complexMessage'],
});

const handler = sinon.stub();
Expand Down Expand Up @@ -263,7 +253,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message', 'MessageController:ping'],
});

const messageHandler = sinon.stub();
Expand Down Expand Up @@ -292,7 +281,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, PingEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'PingController',
allowedEvents: ['PingController:ping'],
});

const handler = sinon.stub();
Expand All @@ -311,7 +299,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

const handler = sinon.stub();
Expand All @@ -338,7 +325,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

const handler = sinon.stub();
Expand All @@ -365,7 +351,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

const handler1 = sinon.stub();
Expand Down Expand Up @@ -395,7 +380,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

const handler = sinon.stub();
Expand All @@ -421,7 +405,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

const handler = sinon.stub();
Expand All @@ -441,7 +424,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

const handler1 = sinon.stub();
Expand All @@ -467,7 +449,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

const handler = sinon.stub();
Expand All @@ -492,7 +473,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:message'],
});

expect(() =>
Expand All @@ -502,6 +482,28 @@ describe('RestrictedControllerMessenger', () => {
).not.toThrow();
});

it('should allow calling an internal action', () => {
type CountAction = {
type: 'CountController:count';
handler: (increment: number) => void;
};
const controllerMessenger = new ControllerMessenger<CountAction, never>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
});

let count = 0;
restrictedControllerMessenger.registerActionHandler(
'CountController:count',
(increment: number) => {
count += increment;
},
);
restrictedControllerMessenger.call('CountController:count', 1);

expect(count).toBe(1);
});

it('should allow calling an external action', () => {
type CountAction = {
type: 'CountController:count';
Expand Down Expand Up @@ -529,6 +531,28 @@ describe('RestrictedControllerMessenger', () => {
expect(count).toBe(1);
});

it('should allow subscribing to an internal event', () => {
type MessageEvent = {
type: 'MessageController:message';
payload: [string];
};
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
});

const handler = sinon.stub();
restrictedControllerMessenger.subscribe(
'MessageController:message',
handler,
);

restrictedControllerMessenger.publish('MessageController:message', 'hello');

expect(handler.calledWithExactly('hello')).toBe(true);
expect(handler.callCount).toBe(1);
});

it('should allow subscribing to an external event', () => {
type MessageEvent = {
type: 'MessageController:message';
Expand Down Expand Up @@ -577,7 +601,7 @@ describe('RestrictedControllerMessenger', () => {

const messageControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedActions: ['MessageController:reset', 'CountController:count'],
allowedActions: ['CountController:count'],
});
const countControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
Expand Down Expand Up @@ -625,7 +649,7 @@ describe('RestrictedControllerMessenger', () => {

const messageControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedEvents: ['MessageController:ping', 'CountController:update'],
allowedEvents: ['CountController:update'],
});
const countControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
Expand Down
Loading
Loading