-
Notifications
You must be signed in to change notification settings - Fork 639
feat: Add onActive and onInactive lifecycle hooks
#3542
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5596e87
Add `onActive` and `onInactive` lifecycle hooks
Mrtenz b132d5c
Fix test
Mrtenz fce5d2b
Add missing types
Mrtenz f998c30
Update packages/snaps-sdk/src/types/handlers/lifecycle.ts
Mrtenz 5ba5018
Update packages/snaps-sdk/src/types/handlers/lifecycle.ts
Mrtenz e5ac26f
Update example
Mrtenz 213b366
Rename action to `setClientActive`
Mrtenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -479,6 +479,11 @@ export type IsMinimumPlatformVersion = { | |
| handler: SnapController['isMinimumPlatformVersion']; | ||
| }; | ||
|
|
||
| export type SetActive = { | ||
| type: `${typeof controllerName}:setActive`; | ||
| handler: SnapController['setActive']; | ||
| }; | ||
|
|
||
| export type SnapControllerGetStateAction = ControllerGetStateAction< | ||
| typeof controllerName, | ||
| SnapControllerState | ||
|
|
@@ -507,7 +512,8 @@ export type SnapControllerActions = | |
| | GetSnapFile | ||
| | SnapControllerGetStateAction | ||
| | StopAllSnaps | ||
| | IsMinimumPlatformVersion; | ||
| | IsMinimumPlatformVersion | ||
| | SetActive; | ||
|
|
||
| // Controller Messenger Events | ||
|
|
||
|
|
@@ -1293,37 +1299,21 @@ export class SnapController extends BaseController< | |
| `${controllerName}:isMinimumPlatformVersion`, | ||
| (...args) => this.isMinimumPlatformVersion(...args), | ||
| ); | ||
|
|
||
| this.messagingSystem.registerActionHandler( | ||
| `${controllerName}:setActive`, | ||
| (...args) => this.setActive(...args), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Initialise the SnapController. | ||
| * | ||
| * Currently this method calls the `onStart` lifecycle hook for all | ||
| * installed Snaps. | ||
| * runnable Snaps. | ||
| */ | ||
| init() { | ||
| const snaps = this.getRunnableSnaps(); | ||
| for (const { id } of snaps) { | ||
| const hasLifecycleHooksEndowment = this.messagingSystem.call( | ||
| 'PermissionController:hasPermission', | ||
| id, | ||
| SnapEndowments.LifecycleHooks, | ||
| ); | ||
|
|
||
| if (!hasLifecycleHooksEndowment) { | ||
| continue; | ||
| } | ||
|
|
||
| this.#callLifecycleHook(METAMASK_ORIGIN, id, HandlerType.OnStart).catch( | ||
| (error) => { | ||
| logError( | ||
| `Error when calling \`onStart\` lifecycle hook for Snap "${id}": ${getErrorMessage( | ||
| error, | ||
| )}`, | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
| this.#callLifecycleHooks(METAMASK_ORIGIN, HandlerType.OnStart); | ||
| } | ||
|
|
||
| #handlePreinstalledSnaps(preinstalledSnaps: PreinstalledSnap[]) { | ||
|
|
@@ -3713,6 +3703,20 @@ export class SnapController extends BaseController< | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Set the active state of the client. This will trigger the `onActive` or | ||
| * `onInactive` lifecycle hooks for all Snaps. | ||
| * | ||
| * @param active - A boolean indicating whether the client is active or not. | ||
| */ | ||
| setActive(active: boolean) { | ||
|
||
| if (active) { | ||
| this.#callLifecycleHooks(METAMASK_ORIGIN, HandlerType.OnActive); | ||
| } else { | ||
| this.#callLifecycleHooks(METAMASK_ORIGIN, HandlerType.OnInactive); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Determine the execution timeout for a given handler permission. | ||
| * | ||
|
|
@@ -4451,6 +4455,36 @@ export class SnapController extends BaseController< | |
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Call a lifecycle hook for all runnable Snaps. | ||
| * | ||
| * @param origin - The origin of the request. | ||
| * @param handler - The lifecycle hook to call. This should be one of the | ||
| * supported lifecycle hooks. | ||
| */ | ||
| #callLifecycleHooks(origin: string, handler: HandlerType) { | ||
| const snaps = this.getRunnableSnaps(); | ||
| for (const { id } of snaps) { | ||
| const hasLifecycleHooksEndowment = this.messagingSystem.call( | ||
| 'PermissionController:hasPermission', | ||
| id, | ||
| SnapEndowments.LifecycleHooks, | ||
| ); | ||
|
|
||
| if (!hasLifecycleHooksEndowment) { | ||
| continue; | ||
| } | ||
|
|
||
| this.#callLifecycleHook(origin, id, handler).catch((error) => { | ||
| logError( | ||
| `Error calling lifecycle hook "${handler}" for Snap "${id}": ${getErrorMessage( | ||
| error, | ||
| )}`, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Call a lifecycle hook on a snap, if the snap has the | ||
| * `endowment:lifecycle-hooks` permission. If the snap does not have the | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.