import * as React from 'react' ; import { AppStoreModel } from './AppStoreModel' ; import * as InitActions from '../actions/InitActions' ; import * as SettingsActions from '../actions/SettingsActions' ; import * as LibraryActions from '../actions/LibraryActions' ; import * as PlayerActions from '../actions/PlayerActions' ; import * as SearchActions from '../actions/SearchActions' ; import * as PlaylistActions from '../actions/PlaylistActions' ; import AppStore , { ActionsModel } from './AppStore' ; import Player from '../libraries/Player' ; import { webFrame } from 'electron' ; const defaultValue = { state : { settings : SettingsActions . getSettings ( ) , originalLibrary : [ ] , library : [ ] , player : { cursor : - 2 , queue : [ ] , originalQueue : [ ] , playing : false , } , playlist : { currentPlaylist : { name : 'none' , tracks : [ ] , type : 'folder' } , currentIndex : 0 , currentTracks : [ ] , playlists : [ ] , } , } , init : null , settings : null , library : null , player : null , search : null , playlist : null , } ; const storeContext = React . createContext < AppStore > ( defaultValue ) ; export const AppStoreConsumer = storeContext . Consumer ; export class AppStoreProvider extends React . Component < any , AppStoreModel > { actions : ActionsModel ; constructor ( props ) { super ( props ) ; this . state = defaultValue . state ; Player . player . muted = defaultValue . state . settings . player . mute ; Player . setVolume ( defaultValue . state . settings . player . volume ) ; webFrame . setZoomFactor ( defaultValue . state . settings . system . zoomFactor ) ; this . actions = { init : null , settings : null , library : null , player : null , search : null , playlist : null , } ; this . wrapActions ( ) ; } wrapActions = ( ) => { const initActions = { ... InitActions } ; for ( const key in initActions ) { if ( initActions . hasOwnProperty ( key ) ) { initActions [ key ] = this . wrap ( initActions [ key ] ) ; } } this . actions . init = initActions ; const settingsActions = { ... SettingsActions } ; for ( const key in settingsActions ) { if ( settingsActions . hasOwnProperty ( key ) ) { settingsActions [ key ] = this . wrap ( settingsActions [ key ] ) ; } } this . actions . settings = settingsActions ; const libraryActions = { ... LibraryActions } ; for ( const key in libraryActions ) { if ( libraryActions . hasOwnProperty ( key ) ) { libraryActions [ key ] = this . wrap ( libraryActions [ key ] ) ; } } this . actions . library = libraryActions ; const playerActions = { ... PlayerActions } ; for ( const key in playerActions ) { if ( playerActions . hasOwnProperty ( key ) ) { playerActions [ key ] = this . wrap ( playerActions [ key ] ) ; } } this . actions . player = playerActions ; const searchActions = { ... SearchActions } ; for ( const key in searchActions ) { if ( searchActions . hasOwnProperty ( key ) ) { searchActions [ key ] = this . wrap ( searchActions [ key ] ) ; } } this . actions . search = searchActions ; const playlistActions = { ... PlaylistActions } ; for ( const key in playlistActions ) { if ( playlistActions . hasOwnProperty ( key ) ) { playlistActions [ key ] = this . wrap ( playlistActions [ key ] ) ; } } this . actions . playlist = playlistActions ; } ; wrap = < T extends ( ... args : any [ ] ) => any > ( fn ) : T => { return ( async ( ... args ) => { this . setState ( await fn ( ... args , this . state ) ) ; } ) as T ; } ; render ( ) { return ( < storeContext . Provider value = { { state : this . state , init : this . actions . init , settings : this . actions . settings , player : this . actions . player , library : this . actions . library , search : this . actions . search , playlist : this . actions . playlist , } } > { this . props . children } < /storeContext.Provider>); } }