Skip to content

Commit

Permalink
Optional AllowedEvents/AllowedActions
Browse files Browse the repository at this point in the history
The restricted controller messenger now allows omitting allowedActions
and allowedEvents. Technically it already allowed this, but it required
setting the type of the allowed actions/events to an empty string. Now
the type is `never` if no actions/events are used.
  • Loading branch information
Gudahtt committed Apr 19, 2021
1 parent a111f7e commit 7c7eb66
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 71 deletions.
20 changes: 5 additions & 15 deletions src/BaseControllerV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand All @@ -72,7 +71,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand All @@ -92,7 +90,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand All @@ -116,7 +113,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand All @@ -140,7 +136,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand All @@ -164,7 +159,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand All @@ -191,7 +185,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand Down Expand Up @@ -228,7 +221,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand Down Expand Up @@ -260,7 +252,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand All @@ -287,7 +278,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand Down Expand Up @@ -315,7 +305,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
new CountController({
Expand All @@ -340,7 +329,6 @@ describe('BaseController', () => {
>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: [],
allowedEvents: ['CountController:stateChange'],
});
const controller = new CountController({
Expand Down Expand Up @@ -792,10 +780,12 @@ describe('getPersistentState', () => {
VisitorControllerAction | VisitorOverflowControllerAction,
VisitorControllerEvent | VisitorOverflowControllerEvent
>();
const visitorControllerMessenger = controllerMessenger.getRestricted({
const visitorControllerMessenger = controllerMessenger.getRestricted<
'VisitorController',
never,
never
>({
name: 'VisitorController',
allowedActions: [],
allowedEvents: [],
});
const visitorController = new VisitorController(
visitorControllerMessenger,
Expand Down
8 changes: 4 additions & 4 deletions src/BaseControllerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export class BaseController<
N,
any,
StateChangeEvent<N, S, any>,
string,
string
string | never,
string | never
>;

private name: N;
Expand All @@ -153,8 +153,8 @@ export class BaseController<
N,
any,
StateChangeEvent<N, S, any>,
string,
string
string | never,
string | never
>;
metadata: StateMetadata<S>;
name: N;
Expand Down
31 changes: 0 additions & 31 deletions src/ControllerMessenger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ describe('RestrictedControllerMessenger', () => {
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'CountController',
allowedActions: ['CountController:count'],
allowedEvents: [],
});

let count = 0;
Expand All @@ -339,7 +338,6 @@ describe('RestrictedControllerMessenger', () => {
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedActions: ['MessageController:reset', 'MessageController:concat'],
allowedEvents: [],
});

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

let count = 0;
Expand All @@ -398,7 +395,6 @@ describe('RestrictedControllerMessenger', () => {
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedActions: ['MessageController:message'],
allowedEvents: [],
});

const messages: Record<string, string> = {};
Expand Down Expand Up @@ -426,7 +422,6 @@ describe('RestrictedControllerMessenger', () => {
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MathController',
allowedActions: ['MathController:add'],
allowedEvents: [],
});

restrictedControllerMessenger.registerActionHandler(
Expand All @@ -450,7 +445,6 @@ describe('RestrictedControllerMessenger', () => {
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'PingController',
allowedActions: ['PingController:ping'],
allowedEvents: [],
});

restrictedControllerMessenger.registerActionHandler(
Expand All @@ -472,7 +466,6 @@ describe('RestrictedControllerMessenger', () => {
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'PingController',
allowedActions: ['PingController:ping'],
allowedEvents: [],
});

expect(() => {
Expand All @@ -486,7 +479,6 @@ describe('RestrictedControllerMessenger', () => {
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'PingController',
allowedActions: ['PingController:ping'],
allowedEvents: [],
});

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

Expand All @@ -541,7 +532,6 @@ describe('RestrictedControllerMessenger', () => {
const controllerMessenger = new ControllerMessenger<never, MessageEvent>();
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'MessageController',
allowedActions: [],
allowedEvents: ['MessageController:message', 'MessageController:ping'],
});

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

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

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

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

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

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

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

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

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

Expand All @@ -793,14 +774,11 @@ describe('RestrictedControllerMessenger', () => {
const externalRestrictedControllerMessenger = controllerMessenger.getRestricted(
{
name: 'CountController',
allowedActions: [],
allowedEvents: [],
},
);
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'OtherController',
allowedActions: ['CountController:count'],
allowedEvents: [],
});

let count = 0;
Expand All @@ -824,13 +802,10 @@ describe('RestrictedControllerMessenger', () => {
const externalRestrictedControllerMessenger = controllerMessenger.getRestricted(
{
name: 'MessageController',
allowedActions: [],
allowedEvents: [],
},
);
const restrictedControllerMessenger = controllerMessenger.getRestricted({
name: 'OtherController',
allowedActions: [],
allowedEvents: ['MessageController:message'],
});

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

let count = 0;
Expand Down Expand Up @@ -916,13 +888,10 @@ describe('RestrictedControllerMessenger', () => {

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

let pings = 0;
Expand Down
Loading

0 comments on commit 7c7eb66

Please sign in to comment.