Skip to content

Commit

Permalink
Add getRunnableSnaps method to SnapController (#3049)
Browse files Browse the repository at this point in the history
Fixes: #3048
  • Loading branch information
david0xd authored Jan 30, 2025
1 parent 362208e commit ce86a74
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 93.06,
"functions": 96.59,
"functions": 96.61,
"lines": 98.08,
"statements": 97.8
}
32 changes: 32 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10022,6 +10022,38 @@ describe('SnapController', () => {
});
});

describe('SnapController:getRunnableSnaps', () => {
it('calls SnapController.getRunnableSnaps()', () => {
const messenger = getSnapControllerMessenger();
const mockSnap = getMockSnapData({
id: MOCK_SNAP_ID,
origin: MOCK_ORIGIN,
});
const mockSnap2 = getMockSnapData({
id: `${MOCK_SNAP_ID}2` as SnapId,
origin: MOCK_ORIGIN,
enabled: false,
});

const snapController = getSnapController(
getSnapControllerOptions({
messenger,
state: {
snaps: getPersistedSnapsState(
mockSnap.stateObject,
mockSnap2.stateObject,
),
},
}),
);

const result = messenger.call('SnapController:getRunnableSnaps');
expect(result).toStrictEqual([getTruncatedSnap()]);

snapController.destroy();
});
});

describe('SnapController:install', () => {
it('calls SnapController.installSnaps()', async () => {
const messenger = getSnapControllerMessenger();
Expand Down
21 changes: 21 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ import type {
} from './registry';
import { SnapsRegistryStatus } from './registry';
import { RequestQueue } from './RequestQueue';
import { getRunnableSnaps } from './selectors';
import { Timer } from './Timer';

export const controllerName = 'SnapController';
Expand Down Expand Up @@ -406,6 +407,11 @@ export type GetAllSnaps = {
handler: SnapController['getAllSnaps'];
};

export type GetRunnableSnaps = {
type: `${typeof controllerName}:getRunnableSnaps`;
handler: SnapController['getRunnableSnaps'];
};

export type StopAllSnaps = {
type: `${typeof controllerName}:stopAllSnaps`;
handler: SnapController['stopAllSnaps'];
Expand Down Expand Up @@ -465,6 +471,7 @@ export type SnapControllerActions =
| GetPermittedSnaps
| InstallSnaps
| GetAllSnaps
| GetRunnableSnaps
| IncrementActiveReferences
| DecrementActiveReferences
| GetRegistryMetadata
Expand Down Expand Up @@ -1150,6 +1157,11 @@ export class SnapController extends BaseController<
(...args) => this.getAllSnaps(...args),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:getRunnableSnaps`,
(...args) => this.getRunnableSnaps(...args),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:incrementActiveReferences`,
(...args) => this.incrementActiveReferences(...args),
Expand Down Expand Up @@ -2387,6 +2399,15 @@ export class SnapController extends BaseController<
return Object.values(this.state.snaps).map(truncateSnap);
}

/**
* Gets all runnable snaps.
*
* @returns All runnable snaps.
*/
getRunnableSnaps(): TruncatedSnap[] {
return getRunnableSnaps(this.getAllSnaps());
}

/**
* Gets the serialized permitted snaps of the given origin, if any.
*
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-controllers/src/test-utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ export const getSnapControllerMessenger = (
'SnapController:disable',
'SnapController:remove',
'SnapController:getAll',
'SnapController:getRunnableSnaps',
'SnapController:getPermitted',
'SnapController:install',
'SnapController:incrementActiveReferences',
Expand Down

0 comments on commit ce86a74

Please sign in to comment.