-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20482 from MetaMask/Version-v10.34.5
Version v10.34.5
- Loading branch information
Showing
56 changed files
with
3,246 additions
and
160 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import assert from 'assert'; | ||
import AppMetadataController from './app-metadata'; | ||
|
||
const EXPECTED_DEFAULT_STATE = { | ||
currentAppVersion: '', | ||
previousAppVersion: '', | ||
previousMigrationVersion: 0, | ||
currentMigrationVersion: 0, | ||
}; | ||
|
||
describe('AppMetadataController', () => { | ||
describe('constructor', () => { | ||
it('accepts initial state and does not modify it if currentMigrationVersion and platform.getVersion() match respective values in state', async () => { | ||
const initState = { | ||
currentAppVersion: '1', | ||
previousAppVersion: '1', | ||
previousMigrationVersion: 1, | ||
currentMigrationVersion: 1, | ||
}; | ||
const appMetadataController = new AppMetadataController({ | ||
state: initState, | ||
currentMigrationVersion: 1, | ||
currentAppVersion: '1', | ||
}); | ||
assert.deepStrictEqual(appMetadataController.store.getState(), initState); | ||
}); | ||
|
||
it('sets default state and does not modify it', async () => { | ||
const appMetadataController = new AppMetadataController({ | ||
state: {}, | ||
}); | ||
assert.deepStrictEqual( | ||
appMetadataController.store.getState(), | ||
EXPECTED_DEFAULT_STATE, | ||
); | ||
}); | ||
|
||
it('sets default state and does not modify it if options version parameters match respective default values', async () => { | ||
const appMetadataController = new AppMetadataController({ | ||
state: {}, | ||
currentMigrationVersion: 0, | ||
currentAppVersion: '', | ||
}); | ||
assert.deepStrictEqual( | ||
appMetadataController.store.getState(), | ||
EXPECTED_DEFAULT_STATE, | ||
); | ||
}); | ||
|
||
it('updates the currentAppVersion state property if options.currentAppVersion does not match the default value', async () => { | ||
const appMetadataController = new AppMetadataController({ | ||
state: {}, | ||
currentMigrationVersion: 0, | ||
currentAppVersion: '1', | ||
}); | ||
assert.deepStrictEqual(appMetadataController.store.getState(), { | ||
...EXPECTED_DEFAULT_STATE, | ||
currentAppVersion: '1', | ||
}); | ||
}); | ||
|
||
it('updates the currentAppVersion and previousAppVersion state properties if options.currentAppVersion, currentAppVersion and previousAppVersion are all different', async () => { | ||
const appMetadataController = new AppMetadataController({ | ||
state: { | ||
currentAppVersion: '2', | ||
previousAppVersion: '1', | ||
}, | ||
currentAppVersion: '3', | ||
currentMigrationVersion: 0, | ||
}); | ||
assert.deepStrictEqual(appMetadataController.store.getState(), { | ||
...EXPECTED_DEFAULT_STATE, | ||
currentAppVersion: '3', | ||
previousAppVersion: '2', | ||
}); | ||
}); | ||
|
||
it('updates the currentMigrationVersion state property if the currentMigrationVersion param does not match the default value', async () => { | ||
const appMetadataController = new AppMetadataController({ | ||
state: {}, | ||
currentMigrationVersion: 1, | ||
}); | ||
assert.deepStrictEqual(appMetadataController.store.getState(), { | ||
...EXPECTED_DEFAULT_STATE, | ||
currentMigrationVersion: 1, | ||
}); | ||
}); | ||
|
||
it('updates the currentMigrationVersion and previousMigrationVersion state properties if the currentMigrationVersion param, the currentMigrationVersion state property and the previousMigrationVersion state property are all different', async () => { | ||
const appMetadataController = new AppMetadataController({ | ||
state: { | ||
currentMigrationVersion: 2, | ||
previousMigrationVersion: 1, | ||
}, | ||
currentMigrationVersion: 3, | ||
}); | ||
assert.deepStrictEqual(appMetadataController.store.getState(), { | ||
...EXPECTED_DEFAULT_STATE, | ||
currentMigrationVersion: 3, | ||
previousMigrationVersion: 2, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import EventEmitter from 'events'; | ||
import { ObservableStore } from '@metamask/obs-store'; | ||
|
||
/** | ||
* The state of the AppMetadataController | ||
*/ | ||
export type AppMetadataControllerState = { | ||
currentAppVersion: string; | ||
previousAppVersion: string; | ||
previousMigrationVersion: number; | ||
currentMigrationVersion: number; | ||
}; | ||
|
||
/** | ||
* The options that NetworkController takes. | ||
*/ | ||
export type AppMetadataControllerOptions = { | ||
currentMigrationVersion?: number; | ||
currentAppVersion?: string; | ||
state?: Partial<AppMetadataControllerState>; | ||
}; | ||
|
||
const defaultState: AppMetadataControllerState = { | ||
currentAppVersion: '', | ||
previousAppVersion: '', | ||
previousMigrationVersion: 0, | ||
currentMigrationVersion: 0, | ||
}; | ||
|
||
/** | ||
* The AppMetadata controller stores metadata about the current extension instance, | ||
* including the currently and previously installed versions, and the most recently | ||
* run migration. | ||
* | ||
*/ | ||
export default class AppMetadataController extends EventEmitter { | ||
/** | ||
* Observable store containing controller data. | ||
*/ | ||
store: ObservableStore<AppMetadataControllerState>; | ||
|
||
/** | ||
* Constructs a AppMetadata controller. | ||
* | ||
* @param options - the controller options | ||
* @param options.state - Initial controller state. | ||
* @param options.currentMigrationVersion | ||
* @param options.currentAppVersion | ||
*/ | ||
constructor({ | ||
currentAppVersion = '', | ||
currentMigrationVersion = 0, | ||
state = {}, | ||
}: AppMetadataControllerOptions) { | ||
super(); | ||
|
||
this.store = new ObservableStore({ | ||
...defaultState, | ||
...state, | ||
}); | ||
|
||
this.#maybeUpdateAppVersion(currentAppVersion); | ||
|
||
this.#maybeUpdateMigrationVersion(currentMigrationVersion); | ||
} | ||
|
||
/** | ||
* Updates the currentAppVersion in state, and sets the previousAppVersion to the old currentAppVersion. | ||
* | ||
* @param maybeNewAppVersion | ||
*/ | ||
#maybeUpdateAppVersion(maybeNewAppVersion: string): void { | ||
const oldCurrentAppVersion = this.store.getState().currentAppVersion; | ||
|
||
if (maybeNewAppVersion !== oldCurrentAppVersion) { | ||
this.store.updateState({ | ||
currentAppVersion: maybeNewAppVersion, | ||
previousAppVersion: oldCurrentAppVersion, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the migrationVersion in state. | ||
* | ||
* @param maybeNewMigrationVersion | ||
*/ | ||
#maybeUpdateMigrationVersion(maybeNewMigrationVersion: number): void { | ||
const oldCurrentMigrationVersion = | ||
this.store.getState().currentMigrationVersion; | ||
|
||
if (maybeNewMigrationVersion !== oldCurrentMigrationVersion) { | ||
this.store.updateState({ | ||
previousMigrationVersion: oldCurrentMigrationVersion, | ||
currentMigrationVersion: maybeNewMigrationVersion, | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.