diff --git a/src/actions/index.js b/src/actions/index.js index c632dc3a..a23de9ad 100755 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -1,3 +1,4 @@ import albumActions from './albumActions' +import mopidyServiceActions from './mopidyServiceActions' diff --git a/src/actions/mopidyServiceActions.js b/src/actions/mopidyServiceActions.js new file mode 100755 index 00000000..ce085af9 --- /dev/null +++ b/src/actions/mopidyServiceActions.js @@ -0,0 +1,21 @@ + +/** + * Actions and Action Creators + **/ + +export const MOPIDY_ONLINE = 'MOPIDY_ONLINE' +export const UPDATE_TRACKLIST = 'UPDATE_TRACKLIST' + +export function mopidyOnline( online ){ + return { + type: MOPIDY_ONLINE, + online: online + } +} + +export function updateTracklist( tracks ){ + return { + type: UPDATE_TRACKLIST, + tracks: tracks + } +} \ No newline at end of file diff --git a/src/services/actions.js b/src/actions/spotifyActions.js similarity index 100% rename from src/services/actions.js rename to src/actions/spotifyActions.js diff --git a/src/bootstrap.js b/src/bootstrap.js index 7d5e29d7..e439cdfc 100755 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -5,13 +5,13 @@ import { createStore } from 'redux' import reducer from './reducers/index' import Services from './services/Services' +// create our global store +let store = createStore( reducer, {} ); + // start all our services -Promise.all(Services.setup()) +Promise.all(Services.setup( store )) .then(() => { console.info('Services started...'); }); -// create our global store -let store = createStore( reducer, {} ); - export default store; diff --git a/src/components/App.js b/src/components/App.js index 824ac903..c66d8248 100755 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,4 +1,8 @@ +/** + * Root level application + **/ + import React, { PropTypes } from 'react'; import ReactDOM from 'react-dom'; import { createStore, bindActionCreators } from 'redux' @@ -9,16 +13,10 @@ import * as actions from '../actions/index' import Services from '../services/Services' -/** - * The application 'brain' - * - * All data handling and fetching is done through this handler - **/ class App extends React.Component{ constructor(props){ super(props); - this.handleClick = this.handleClick.bind(this); } diff --git a/src/components/Queue.js b/src/components/Queue.js index ed6da288..94717965 100755 --- a/src/components/Queue.js +++ b/src/components/Queue.js @@ -18,13 +18,9 @@ class NowPlaying extends React.Component{ // on render componentDidMount(){ - var self = this; Services.get('services.mopidy') .then( function(MopidyService){ - MopidyService.connection.tracklist.getTlTracks() - .then( function(tracks){ - self.setState({ tracks : tracks }); - }); + MopidyService.getCurrentTracklist(); }) } diff --git a/src/index.js b/src/index.js index f2f3a86a..3ca212c8 100755 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,6 @@ /** * Base-level application wrapper **/ - -console.info('Application initiated'); - import React, { PropTypes } from 'react'; import ReactDOM from 'react-dom'; @@ -16,26 +13,6 @@ import App from './components/App' import Album from './components/Album' import Queue from './components/Queue' -import Services from './services/Services' - -Promise.all(Services.setup()) - .then(() => { - console.info('Services started...'); - }); - - -/** - * Render our application into the real DOM - * - * Sets up all of our routes, and the relevant components for each. - * Provider facilitates global access to the store by using connect() - - - - - - **/ - ReactDOM.render( diff --git a/src/reducers/mopidyServiceReducer.js b/src/reducers/mopidyServiceReducer.js new file mode 100755 index 00000000..3c07d0ff --- /dev/null +++ b/src/reducers/mopidyServiceReducer.js @@ -0,0 +1,18 @@ + +import * as actions from '../actions/mopidyServiceActions' + +export default function reducer(mopidyService = {}, action){ + switch (action.type) { + + case actions.UPDATE_TRACKLIST: + return Object.assign({}, mopidyService, { + tracks: action.tracks + }); + + default: + return mopidyService + } +} + + + diff --git a/src/services/reducer.js b/src/reducers/spotifyReducer.js similarity index 100% rename from src/services/reducer.js rename to src/reducers/spotifyReducer.js diff --git a/src/services/MopidyService.js b/src/services/MopidyService.js index fd947a43..2a40bc4e 100755 --- a/src/services/MopidyService.js +++ b/src/services/MopidyService.js @@ -1,35 +1,69 @@ import Mopidy from 'mopidy' -function MopidyService( connection ){ - this.connection = connection; -}; -MopidyService.attachKey = 'services.mopidy'; +/** + * Create an IOC wrapper for our MopidyService instance + * + * This initiates our service, and provides a container for our connection. We wrap + * this in a Promise so any requests to .get() will be delayed until we're connected. Genius! + **/ -MopidyService.attach = function( app ){ - console.info('MopidyService: Attaching...'); +let MopidyServiceWrapper = { + attachKey: 'services.mopidy', + attach: function( store ){ + console.info('MopidyServiceWrapper: Attaching...'); + var service = new MopidyService( store ); + return new Promise((resolve) => { + service.connection.on('state:online', () => { + resolve( service ); + }); + }); + } +} - return new Promise((resolve) => { +export default MopidyServiceWrapper + + +/** + * Mopidy service + * + * Handles internal requests and passes them on to our connection + **/ +class MopidyService { + + constructor( store ){ var mopidyhost = 'music.plasticstudio.co';//window.location.hostname; var mopidyport = "6680"; var protocol = 'ws'; - var connection = new Mopidy({ + + this.connection = new Mopidy({ webSocketUrl: protocol+"://" + mopidyhost + ":" + mopidyport + "/mopidy/ws", callingConvention: 'by-position-or-by-name' }); - connection.on((a, msg) => { - //console.log(msg); - // msg && msg.method ? - // console.log('<-', msg.method) : - // (msg ? console.log('->', JSON.parse(msg.data)) : console.log('->', msg)); - }); - connection.on('state:online', () => { - console.info('MopidyService: Attached'); - var service = new MopidyService( connection ); - resolve( service ); - }); - }); -} + this.connection.on( + (type, message) => this.handleMessage( type, message ) + ); + this.store = store; + } -export default MopidyService; \ No newline at end of file + setConnection( connection ){ + this.connection = connection; + } + + clearConnection(){ + this.connection = false; + } + + handleMessage( type, message ){ + //console.log( message ); + } + + getCurrentTracklist(){ + this.connection.tracklist.getTlTracks() + .then( function(tracks){ + console.log(tracks); + }); + } + +} \ No newline at end of file diff --git a/src/services/SpotifyService.js b/src/services/SpotifyService.js index 30c65172..51f828d3 100755 --- a/src/services/SpotifyService.js +++ b/src/services/SpotifyService.js @@ -1,6 +1,6 @@ import React, { PropTypes } from 'react' -import * as actions from './actions' +import * as actions from '../actions/spotifyActions' import { connect } from 'react-redux' import { bindActionCreators } from 'redux'