1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-11 20:27:12 +01:00
metamask-extension/app/scripts/controllers/app-state.js

171 lines
4.2 KiB
JavaScript
Raw Normal View History

import EventEmitter from 'events'
import ObservableStore from 'obs-store'
export default class AppStateController extends EventEmitter {
/**
* @constructor
* @param {Object} opts
*/
2020-11-03 00:41:28 +01:00
constructor(opts = {}) {
const {
addUnlockListener,
isUnlocked,
initState,
onInactiveTimeout,
showUnlockRequest,
preferencesStore,
} = opts
super()
this.onInactiveTimeout = onInactiveTimeout || (() => undefined)
this.store = new ObservableStore({
timeoutMinutes: 0,
connectedStatusPopoverHasBeenShown: true,
2020-10-06 20:28:38 +02:00
swapsWelcomeMessageHasBeenShown: false,
2020-11-03 00:41:28 +01:00
defaultHomeActiveTabName: null,
...initState,
})
this.timer = null
this.isUnlocked = isUnlocked
this.waitingForUnlock = []
addUnlockListener(this.handleUnlock.bind(this))
this._showUnlockRequest = showUnlockRequest
preferencesStore.subscribe(({ preferences }) => {
const currentState = this.store.getState()
if (currentState.timeoutMinutes !== preferences.autoLockTimeLimit) {
this._setInactiveTimeout(preferences.autoLockTimeLimit)
}
})
const { preferences } = preferencesStore.getState()
this._setInactiveTimeout(preferences.autoLockTimeLimit)
}
/**
* Get a Promise that resolves when the extension is unlocked.
* This Promise will never reject.
*
* @param {boolean} shouldShowUnlockRequest - Whether the extension notification
* popup should be opened.
* @returns {Promise<void>} A promise that resolves when the extension is
* unlocked, or immediately if the extension is already unlocked.
*/
2020-11-03 00:41:28 +01:00
getUnlockPromise(shouldShowUnlockRequest) {
return new Promise((resolve) => {
if (this.isUnlocked()) {
resolve()
} else {
this.waitForUnlock(resolve, shouldShowUnlockRequest)
}
})
}
/**
* Adds a Promise's resolve function to the waitingForUnlock queue.
* Also opens the extension popup if specified.
*
* @param {Promise.resolve} resolve - A Promise's resolve function that will
* be called when the extension is unlocked.
* @param {boolean} shouldShowUnlockRequest - Whether the extension notification
* popup should be opened.
*/
2020-11-03 00:41:28 +01:00
waitForUnlock(resolve, shouldShowUnlockRequest) {
this.waitingForUnlock.push({ resolve })
this.emit('updateBadge')
if (shouldShowUnlockRequest) {
this._showUnlockRequest()
}
}
/**
* Drains the waitingForUnlock queue, resolving all the related Promises.
*/
2020-11-03 00:41:28 +01:00
handleUnlock() {
if (this.waitingForUnlock.length > 0) {
while (this.waitingForUnlock.length > 0) {
this.waitingForUnlock.shift().resolve()
}
this.emit('updateBadge')
}
}
2020-05-25 21:01:28 +02:00
/**
* Sets the default home tab
* @param {string} [defaultHomeActiveTabName] - the tab name
*/
2020-11-03 00:41:28 +01:00
setDefaultHomeActiveTabName(defaultHomeActiveTabName) {
2020-05-25 21:01:28 +02:00
this.store.updateState({
defaultHomeActiveTabName,
})
}
/**
* Record that the user has seen the connected status info popover
*/
2020-11-03 00:41:28 +01:00
setConnectedStatusPopoverHasBeenShown() {
this.store.updateState({
connectedStatusPopoverHasBeenShown: true,
})
}
2020-10-06 20:28:38 +02:00
/**
* Record that the user has seen the swap screen welcome message
*/
2020-11-03 00:41:28 +01:00
setSwapsWelcomeMessageHasBeenShown() {
2020-10-06 20:28:38 +02:00
this.store.updateState({
swapsWelcomeMessageHasBeenShown: true,
})
}
/**
* Sets the last active time to the current time
* @returns {void}
*/
2020-11-03 00:41:28 +01:00
setLastActiveTime() {
this._resetTimer()
}
/**
* Sets the inactive timeout for the app
* @param {number} timeoutMinutes - the inactive timeout in minutes
* @returns {void}
* @private
*/
2020-11-03 00:41:28 +01:00
_setInactiveTimeout(timeoutMinutes) {
this.store.updateState({
timeoutMinutes,
})
this._resetTimer()
}
/**
* Resets the internal inactive timer
*
* If the {@code timeoutMinutes} state is falsy (i.e., zero) then a new
* timer will not be created.
*
* @returns {void}
* @private
*/
2020-11-03 00:41:28 +01:00
_resetTimer() {
2019-12-03 21:50:55 +01:00
const { timeoutMinutes } = this.store.getState()
if (this.timer) {
clearTimeout(this.timer)
}
if (!timeoutMinutes) {
return
}
2020-11-03 00:41:28 +01:00
this.timer = setTimeout(
() => this.onInactiveTimeout(),
timeoutMinutes * 60 * 1000,
)
}
}