Skip to content

Commit

Permalink
feat: Add app updater subject and state
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Nov 27, 2019
1 parent cd82ca4 commit 423109d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/neuron-ui/src/containers/Main/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
CurrentNetworkID as CurrentNetworkIDSubject,
ConnectionStatus as ConnectionStatusSubject,
SyncedBlockNumber as SyncedBlockNumberSubject,
AppUpdater as AppUpdaterSubject,
Command as CommandSubject,
} from 'services/subjects'
import { ckbCore, getBlockchainInfo, getTipHeader } from 'services/chain'
Expand Down Expand Up @@ -183,6 +184,13 @@ export const useSubscription = ({
})
})

const appUpdaterSubscription = AppUpdaterSubject.subscribe(appUpdaterInfo => {
dispatch({
type: NeuronWalletActions.UpdateAppUpdaterStatus,
payload: appUpdaterInfo,
})
})

const commandSubscription = CommandSubject.subscribe(({ winID, type, payload }: Subject.CommandMetaInfo) => {
if (winID && getWinID() === winID) {
switch (type) {
Expand Down Expand Up @@ -223,6 +231,7 @@ export const useSubscription = ({
currentNetworkIDSubscription.unsubscribe()
connectionStatusSubscription.unsubscribe()
syncedBlockNumberSubscription.unsubscribe()
appUpdaterSubscription.unsubscribe()
commandSubscription.unsubscribe()
}
}, [walletID, pageNo, pageSize, keywords, isAllowedToFetchList, history, dispatch])
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-ui/src/services/subjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const SubjectConstructor = <T>(
| 'connection-status-updated'
| 'synced-block-number-updated'
| 'command'
| 'app-updater-updated'
) => {
return window.ipcRenderer
? {
Expand All @@ -45,6 +46,7 @@ export const NetworkList = SubjectConstructor<Subject.NetworkList>('network-list
export const CurrentNetworkID = SubjectConstructor<Subject.CurrentNetworkID>('current-network-id-updated')
export const ConnectionStatus = SubjectConstructor<Subject.ConnectionStatus>('connection-status-updated')
export const SyncedBlockNumber = SubjectConstructor<Subject.BlockNumber>('synced-block-number-updated')
export const AppUpdater = SubjectConstructor<Subject.AppUpdater>('app-updater-updated')
export const Command = SubjectConstructor<Subject.CommandMetaInfo>('command')

export default {
Expand All @@ -56,5 +58,6 @@ export default {
CurrentNetworkID,
ConnectionStatus,
SyncedBlockNumber,
AppUpdater,
Command,
}
3 changes: 3 additions & 0 deletions packages/neuron-ui/src/states/initStates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import chain from './chain'
import wallet from './wallet'
import settings from './settings'
import nervosDAO from './nervosDAO'
import updater from './updater'

export * from './app'
export * from './chain'
export * from './wallet'
export * from './settings'
export * from './nervosDAO'
export * from './updater'

const initStates = {
app,
chain,
wallet,
settings,
nervosDAO,
updater,
}

export default initStates
8 changes: 8 additions & 0 deletions packages/neuron-ui/src/states/initStates/updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const appUpdaterState: State.AppUpdater = {
checking: false,
downloadProgress: -1,
version: '',
releaseNotes: '',
}

export default appUpdaterState
14 changes: 14 additions & 0 deletions packages/neuron-ui/src/states/stateProvider/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export enum NeuronWalletActions {
UpdateSyncedBlockNumber = 'updateSyncedBlockNumber',
// dao
UpdateNervosDaoData = 'updateNervosDaoData',
// updater
UpdateAppUpdaterStatus = 'updateAppUpdaterStatus',
}
export enum AppActions {
UpdateTransactionID = 'updateTransactionID',
Expand Down Expand Up @@ -97,6 +99,12 @@ export const reducer = (
networks,
wallets,
},
updater: {
checking: false,
downloadProgress: -1,
version: '',
releaseNotes: '',
},
}
}
case NeuronWalletActions.UpdateCodeHash: {
Expand Down Expand Up @@ -224,6 +232,12 @@ export const reducer = (
},
}
}
case NeuronWalletActions.UpdateAppUpdaterStatus: {
return {
...state,
updater: payload,
}
}
case NeuronWalletActions.UpdateNervosDaoData: {
return {
...state,
Expand Down
8 changes: 8 additions & 0 deletions packages/neuron-ui/src/types/App/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,20 @@ declare namespace State {
records: NervosDAORecord[]
}

interface AppUpdater {
checking: boolean
downloadProgress: number
version: string
releaseNotes: string
}

interface AppWithNeuronWallet {
app: App
chain: Chain
settings: Settings
wallet: Wallet
nervosDAO: NervosDAO
updater: AppUpdater
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/neuron-ui/src/types/Subject/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ declare namespace Subject {
}
type ConnectionStatus = boolean
type BlockNumber = string
interface AppUpdater {
checking: boolean
downloadProgress: number
version: string
releaseNotes: string
}
}
5 changes: 5 additions & 0 deletions packages/neuron-wallet/src/controllers/app/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DebouncedCurrentNetworkIDSubject, DebouncedNetworkListSubject } from 'm
import { SampledSyncedBlockNumberSubject, DebouncedConnectionStatusSubject } from 'models/subjects/node'
import { WalletListSubject, CurrentWalletSubject } from 'models/subjects/wallets'
import dataUpdateSubject from 'models/subjects/data-update'
import AppUpdaterSubject from 'models/subjects/app-updater'

interface AppResponder {
sendMessage: (channel: string, arg: any) => void
Expand Down Expand Up @@ -52,4 +53,8 @@ export const subscribe = (dispatcher: AppResponder) => {
dataUpdateSubject.next({ dataType: 'current-wallet', actionType: 'update' })
}
})

AppUpdaterSubject.subscribe(params => {
dispatcher.sendMessage('app-updater-updated', params)
})
}
10 changes: 10 additions & 0 deletions packages/neuron-wallet/src/models/subjects/app-updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Subject } from 'rxjs'

const AppUpdaterSubject = new Subject<{
checking: boolean
downloadProgress: number, // -1: not started, 1: finished, 0~1: downloading
version: string, // "": no update, "v.x.y.z": version v.x.y.z available
releaseNotes: string
}>()

export default AppUpdaterSubject

0 comments on commit 423109d

Please sign in to comment.