Skip to content

Commit 2df985d

Browse files
Lazily init ready state
1 parent 6664fae commit 2df985d

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

packages/snaps-controllers/src/snaps/SnapController.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10772,6 +10772,23 @@ describe('SnapController', () => {
1077210772

1077310773
describe('SnapController actions', () => {
1077410774
describe('SnapController:init', () => {
10775+
it('populates `isReady`', async () => {
10776+
const rootMessenger = getControllerMessenger();
10777+
const messenger = getSnapControllerMessenger(rootMessenger);
10778+
10779+
const snapController = getSnapController(
10780+
getSnapControllerOptions({ messenger }),
10781+
);
10782+
10783+
expect(snapController.state.isReady).toBe(false);
10784+
messenger.call('SnapController:init');
10785+
10786+
await waitForStateChange(messenger);
10787+
expect(snapController.state.isReady).toBe(true);
10788+
10789+
snapController.destroy();
10790+
});
10791+
1077510792
it('calls `onStart` for all Snaps with the `endowment:lifecycle-hooks` permission', async () => {
1077610793
const rootMessenger = getControllerMessenger();
1077710794
const messenger = getSnapControllerMessenger(rootMessenger);

packages/snaps-controllers/src/snaps/SnapController.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,9 @@ export class SnapController extends BaseController<
13351335
* runnable Snaps.
13361336
*/
13371337
init() {
1338+
// Lazily populate the `isReady` state.
1339+
this.#ensureCanUsePlatform().catch(logWarning);
1340+
13381341
this.#callLifecycleHooks(METAMASK_ORIGIN, HandlerType.OnStart);
13391342
}
13401343

@@ -1545,7 +1548,7 @@ export class SnapController extends BaseController<
15451548
* Also updates any preinstalled Snaps to the latest allowlisted version.
15461549
*/
15471550
async updateRegistry(): Promise<void> {
1548-
await this.#assertCanUsePlatform();
1551+
await this.#ensureCanUsePlatform();
15491552
await this.messenger.call('SnapsRegistry:update');
15501553

15511554
const blockedSnaps = await this.messenger.call(
@@ -1725,12 +1728,14 @@ export class SnapController extends BaseController<
17251728
/**
17261729
* Waits for onboarding and then asserts whether the Snaps platform is allowed to run.
17271730
*/
1728-
async #assertCanUsePlatform() {
1731+
async #ensureCanUsePlatform() {
17291732
// Ensure the user has onboarded before allowing access to Snaps.
17301733
await this.#ensureOnboardingComplete();
17311734

17321735
const flags = this.#getFeatureFlags();
17331736

1737+
// If the user has onboarded, the Snaps Platform is considered ready,
1738+
// if it isn't forced to be disabled via feature flags.
17341739
const isReady = flags.disableSnaps !== true;
17351740
if (this.state.isReady !== isReady) {
17361741
this.update((state) => {
@@ -1819,7 +1824,7 @@ export class SnapController extends BaseController<
18191824
* @param snapId - The id of the Snap to start.
18201825
*/
18211826
async startSnap(snapId: SnapId): Promise<void> {
1822-
await this.#assertCanUsePlatform();
1827+
await this.#ensureCanUsePlatform();
18231828
const snap = this.state.snaps[snapId];
18241829

18251830
if (!snap.enabled) {
@@ -2720,7 +2725,7 @@ export class SnapController extends BaseController<
27202725
origin: string,
27212726
requestedSnaps: RequestSnapsParams,
27222727
): Promise<RequestSnapsResult> {
2723-
await this.#assertCanUsePlatform();
2728+
await this.#ensureCanUsePlatform();
27242729

27252730
const result: RequestSnapsResult = {};
27262731

@@ -3006,7 +3011,7 @@ export class SnapController extends BaseController<
30063011
if (!automaticUpdate) {
30073012
this.#assertCanInstallSnaps();
30083013
}
3009-
await this.#assertCanUsePlatform();
3014+
await this.#ensureCanUsePlatform();
30103015

30113016
const snap = this.getExpect(snapId);
30123017

@@ -3641,7 +3646,7 @@ export class SnapController extends BaseController<
36413646
handler: handlerType,
36423647
request: rawRequest,
36433648
}: SnapRpcHookArgs & { snapId: SnapId }): Promise<unknown> {
3644-
await this.#assertCanUsePlatform();
3649+
await this.#ensureCanUsePlatform();
36453650

36463651
const snap = this.get(snapId);
36473652

0 commit comments

Comments
 (0)