import actionCreatorFactory , { ActionCreator } from 'typescript-fsa' ; import { HistoryItem , PendingTransaction , WorkerState } from './WorkerState' ; import Wallet from 'ethereumjs-wallet' ; import { SerializedPaymentChannel } from 'machinomy/dist/lib/payment_channel' ; const actionCreator = actionCreatorFactory ( 'worker' ) ; export const setWallet = actionCreator < Wallet | undefined > ( 'runtime/setWallet' ) ; export function setWalletHandler ( state , wallet ) { return { ... state , runtime : { ... state . runtime , wallet } , } ; } export const setKeyring = actionCreator < string > ( 'persistent/setKeyring' ) ; export function setKeyringHandler ( state , keyring ) { return { ... state , persistent : { ... state . persistent , keyring } , } ; } export interface RestoreWalletParam { keyring : string ; wallet : Wallet ; } export const restoreWallet = actionCreator < RestoreWalletParam > ( 'persistent+runtime/restoreWallet' ) ; export function restoreWalletHandler ( state , param ) { return { ... state , persistent : { ... state . persistent , didInit : true , keyring : param . keyring } , runtime : { ... state . runtime , wallet : param . wallet } , } ; } export const setDidStoreMnemonic = actionCreator < boolean > ( 'persistent/setDidStoreMnemonic' ) ; export function setDidStoreMnemonicHandler ( state ) { return { ... state , persistent : { ... state . persistent , didInit : true } , } ; } export const openChannel = actionCreator < string > ( 'persistent/openChannel' ) ; export function openChannelHandler ( state , channelId ) { return { ... state , persistent : { ... state . persistent , pendingChannelIds : [ ... state . persistent . pendingChannelIds , channelId ] , } , } ; } export const removePendingChannel = actionCreator < string > ( 'persistent/removePendingChannel' ) ; export function removePendingChannelHandler ( state , channelId ) { return { ... state , persistent : { ... state . persistent , pendingChannelIds : state . persistent . pendingChannelIds . filter ( id => id !== channelId ) , } , } ; } export const setTransactionPending = actionCreator < boolean > ( 'runtime/setTransactionPending' ) ; export function setTransactionPendingHandler ( state , pending ) { let pendingDate = 0 ; if ( pending ) { pendingDate = Date . now ( ) ; } return { ... state , runtime : { ... state . runtime , isTransactionPending : pendingDate } , } ; } export interface AuthorizationRequestParam { hubUrl : string ; authRealm : string ; } export const setAuthorizationRequest = actionCreator < AuthorizationRequestParam > ( 'runtime/authorizationRequest' ) ; export function setAuthorizationRequestHandler ( state , authorizationRequest ) { return { ... state , runtime : { ... state . runtime , authorizationRequest } } ; } export const rememberPage = actionCreator < string > ( 'persistent/rememberPage' ) ; export function rememberPageHandler ( state , path ) { return { ... state , persistent : { ... state . persistent , rememberPath : path } , } ; } export const setLastUpdateDb = actionCreator < number > ( 'runtime/setLastUpdateDb' ) ; export function setLastUpdateDbHandler ( state , timestamp ) { return { ... state , runtime : { ... state . runtime , lastUpdateDb : timestamp } , } ; } export const setCurrentHubUrl = actionCreator < string > ( 'runtime/setCurrentHub' ) ; export function setCurrentHubUrlHandler ( state , currentHubUrl ) { return { ... state , runtime : { ... state . runtime , currentHubUrl } } ; } export const setCurrentAuthRealm = actionCreator < string > ( 'runtime/setAuthRealm' ) ; export function setCurrentAuthRealmHandler ( state , currentAuthRealm ) { return { ... state , runtime : { ... state . runtime , currentAuthRealm } } ; } export const setCurrentAuthToken = actionCreator < string > ( 'runtime/setCurrentAuthToken' ) ; export function setCurrentAuthTokenHandler ( state , currentAuthToken ) { return { ... state , runtime : { ... state . runtime , currentAuthToken } } ; } export interface SetBrandingParam { title ? : string ; companyName ? : string ; username ? : string ; backgroundColor ? : string ; textColor ? : string ; address : string ; } export const setHubBranding = actionCreator < SetBrandingParam > ( 'persistent/setHubBranding' ) ; export function setHubBrandingHandler ( state , branding ) { return { ... state , runtime : { ... state . runtime , branding } } ; } export interface ToggleFrameParam { isFrameDisplayed : boolean ; forceRedirect ? : string ; isPerformer ? : boolean ; } export const toggleFrame = actionCreator < ToggleFrameParam > ( 'runtime/toggleFrame' ) ; export function toggleFrameHandler ( state , payload ) { return { ... state , runtime : { ... state . runtime , isFrameDisplayed : payload . isFrameDisplayed , forceRedirect : payload . forceRedirect , isPerformer : payload . isPerformer , } } ; } export const setChannels = actionCreator < SerializedPaymentChannel [ ] > ( 'runtime/setChannels' ) ; export function setChannelsHandler ( state , channels ) { return { ... state , runtime : { ... state . runtime , channels : { ... state . runtime . channels , [ state . runtime . currentHubUrl ] : channels } } } ; } export const setChannel = actionCreator < SerializedPaymentChannel > ( 'runtime/setChannel' ) ; export function setChannelHandler ( state , channel ) { let channels = state . runtime . channels [ state . runtime . currentHubUrl ] || [ ] ; channels = [ ] . concat ( channels as any ) ; const index = channels . findIndex ( ( ch ) => ch . channelId === channel . channelId ) ; if ( index === - 1 ) { channels . push ( channel ) ; } else { channels [ index ] = channel ; } return { ... state , runtime : { ... state . runtime , channels : { ... state . runtime . channels , [ state . runtime . currentHubUrl ] : channels } } } ; } export const setHistory = actionCreator < HistoryItem [ ] > ( 'runtime/setHistory' ) ; export function setHistoryHandler ( state , history ) { return { ... state , runtime : { ... state . runtime , history } } ; } export const setBalance = actionCreator < string > ( 'runtime/setBalance' ) ; export function setBalanceHandler ( state , balance ) { return { ... state , runtime : { ... state . runtime , balance } } ; } export const setPendingTransaction = actionCreator < PendingTransaction | null > ( 'runtime/setPendingTransaction' ) ; export function setPendingTransactionHandler ( state , pendingTransaction ) { return { ... state , runtime : { ... state . runtime , pendingTransaction } } ; } export const reset = actionCreator < null > ( 'runtime/reset' ) ; export function resetHandler ( ) { return undefined as any ; } export const setHasActiveWithdrawal = actionCreator < boolean > ( 'runtime/setActiveWithdrawal' ) ; export function setHasActiveWithdrawalHandler ( state , hasActiveWithdrawal ) { return { ... state , runtime : { ... state . runtime , hasActiveWithdrawal } } ; }