Skip to content

Commit

Permalink
Use ImmutableJS for JS entities
Browse files Browse the repository at this point in the history
  • Loading branch information
datso committed May 15, 2016
1 parent 5813776 commit 6d24a4e
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 415 deletions.
85 changes: 40 additions & 45 deletions app/modules/accounts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {NetInfo, AppState} from 'react-native'
import * as Navigation from './navigation'
import {Endpoint} from '../../pjsip'
import {
OrderedMap,
Record
Expand All @@ -14,29 +13,42 @@ export const ACCOUNT_REGISTRATION_CHANGED = 'accounts/ACCOUNT_REGISTRATION_CHANG
export const ACCOUNT_DELETED = 'accounts/ACCOUNT_DELETED';

/**
* Actions
* Handles initialization event.
*
* @param {Account[]} accounts
* @returns {Function}
*/

export function initAccounts(accounts) {
return async function(dispatch, getState) {
// -----
accounts.forEach((account) => subscribe(account, dispatch));

// -----
dispatch({type: ACCOUNTS_INIT, accounts});
};
}

/**
* Handle account change event.
*
* @param {Account} account
* @returns {Function}
*/
export function changeAccount(account) {
return async function(dispatch, getState) {
dispatch({type: ACCOUNT_CHANGED, account});
};
}

/**
* Creates new account based on provided configuration.
*
* @param {Object} configuration
* @returns {Function}
*/
export function createAccount(configuration) {
return async function(dispatch, getState) {
// -----
let account = await Endpoint.createAccount({
...configuration // ,
// transport: "TCP"
let endpoint = getState()['app']['endpoint'];
let account = await endpoint.createAccount({
...configuration
});

// -----
subscribe(account, dispatch);

dispatch({type: ACCOUNT_CREATED, account});
dispatch(Navigation.goTo({name: 'home'}));
};
Expand All @@ -45,45 +57,34 @@ export function createAccount(configuration) {
/**
* Action to delete account.
*
* @param {AccountRecord} record
* @param {Account} account
* @returns {Function}
*/
export function deleteAccount(record) {
export function deleteAccount(account) {
return async function(dispatch, getState) {
let account = record.get('ref');

// -----
await Endpoint.deleteAccount(account);
let endpoint = getState()['app']['endpoint'];
await endpoint.deleteAccount(account);

// -----
// TODO: Unsubscribe

dispatch({type: ACCOUNT_DELETED, account});
dispatch(Navigation.goTo({name: 'home'}));
};
}

function subscribe(account, dispatch) {
account.addListener(
"registration_changed",
(account, registration) => {
dispatch({type: ACCOUNT_REGISTRATION_CHANGED, account, registration})
}
);
}
//function subscribe(account, dispatch) {
// account.addListener(
// "registration_changed",
// (account, registration) => {
// dispatch({type: ACCOUNT_REGISTRATION_CHANGED, account, registration})
// }
// );
//}

/**
* Reducer
*/

var AccountRecord = Record({
id: -1,
uri: null,
registration: null,
ref: null // Original link to account
});


const initialState = {
isLoading: true,
map: new OrderedMap()
Expand All @@ -97,10 +98,7 @@ export default function app(state = initialState, action) {
isLoading: false,
map: action.accounts.reduce(
(result, account) => {
return result.set(account.getId(), new AccountRecord({
...account.toJson(),
ref: account
}));
return result.set(account.getId(), account);
},
state.map
)
Expand All @@ -111,10 +109,7 @@ export default function app(state = initialState, action) {
case ACCOUNT_REGISTRATION_CHANGED:
return {
...state,
map: state.map.set(action.account.getId(), new AccountRecord({
...action.account.toJson(),
ref: action.account
}))
map: state.map.set(action.account.getId(), action.account)
};

case ACCOUNT_DELETED:
Expand Down
66 changes: 23 additions & 43 deletions app/modules/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {NetInfo, AppState} from 'react-native'
import * as Navigation from './navigation'
import {initAccounts} from './accounts'
import {initCalls} from './calls'
import {initAccounts, changeAccount} from './accounts'
import {initCalls, receiveCall, changeCall, terminateCall} from './calls'
import {Endpoint} from '../../pjsip'

export const INITIALIZED = 'app/INITIALIZED';
export const CHANGE_NET_STATUS = 'app/CHANGE_NET_STATUS';
export const CHANGE_APP_STATE = 'app/CHANGE_APP_STATE';

/**
* Action Creators
Expand All @@ -18,67 +16,49 @@ export function init() {
// Retrieving PjSip service state
// It is possible that Endpoint instance already have registered accounts and active calls.
// (because Javascript state is not persistent when User close application, e.g. application goes to background state)
let state = await Endpoint.start();

let endpoint = new Endpoint();
let state = await endpoint.start();
let {accounts, calls} = state;

// Subscribe to endpoint events
endpoint.on("registration_changed", (account) => {
dispatch(changeAccount(account));
});
endpoint.on("call_received", (call) => {
dispatch(receiveCall(call));
});
endpoint.on("call_changed", (call) => {
dispatch(changeCall(call));
});
endpoint.on("call_terminated", (call) => {
dispatch(terminateCall(call));
});

dispatch(initAccounts(accounts));
dispatch(initCalls(calls));
dispatch({type: INITIALIZED});
dispatch({type: INITIALIZED, payload: endpoint});

dispatch(Navigation.goAndReplace({name: 'home'}))

}
}

// function setupNetStatusListener() {
// return dispatch => {
// NetInfo.isConnected.addEventListener('change',
// async status => {
// dispatch({type: CHANGE_NET_STATUS, payload: status});
// await dispatch(onNetStatusChangeFaye(status))
// }
// );
// }
// }
//
// function setupAppStatusListener() {
// return (dispatch, getState) => {
// AppState.addEventListener('change', status => {
// // TODO: Update drawer rooms state and messages in current room
// // if app status changes from backgrount to active
// dispatch({type: CHANGE_APP_STATE, payload: status})
// })
// }
// }


/**
* Reducer
*/

const initialState = {
online: false
endpoint: null
};

export default function app(state = initialState, action) {
switch (action.type) {
case INITIALIZED:
// return {...state,
// online: action.netStatus
// }
return state;

case CHANGE_NET_STATUS:
return {...state,
online: action.payload
};

case CHANGE_APP_STATE:
return {...state,
appState: action.payload
endpoint: action.payload
};

default:
return state
}
}
}
Loading

0 comments on commit 6d24a4e

Please sign in to comment.