-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from jagreene/simulator-refactor
ADD: Rewritten Simulator Using Es6, Jsx, and Redux
- Loading branch information
Showing
37 changed files
with
6,618 additions
and
1,114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": [ "es2015", "stage-2", "react" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Simulator/wdc-simulator.js | ||
Simulator/tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"extends": [ | ||
"airbnb" | ||
], | ||
"parser": "babel-eslint", | ||
"env": { | ||
"browser": true, | ||
}, | ||
"globals": { | ||
"toastr": false, | ||
}, | ||
"rules": { | ||
"max-len": "warn", | ||
"indent": "warn", | ||
"react/jsx-indent": "warn", | ||
"no-unused-vars": "warn", | ||
"prefer-const": "warn", | ||
"no-console": ["warn", { "allow": ["warn", "error"] }], | ||
"spaced-comment": "off", | ||
"react/prop-types": "off", | ||
"react/prefer-stateless-function": "off", | ||
"no-use-before-define": "off", | ||
"react/sort-comp": "off", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: node_js | ||
node_js: | ||
- node | ||
before_script: | ||
- export DISPLAY=:99.0 | ||
- sh -e /etc/init.d/xvfb start | ||
- sleep 3 | ||
script: | ||
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] && npm run test:travis || npm run unit:coveralls' | ||
addons: | ||
sauce_connect: | ||
username: jaustingreene | ||
access_key: | ||
secure: HJoFcs33myPRMQjYxgYnGG1VbxYdbzxUiNRK713qhmCKoIFfZahy7PpOimQ8+0dRJ2rv33DSK5v20LbH7tiOWVWEfEwVOVln8ItPjLTBHX3dUaO8OuO+JA18MFLVBwIWqdrd+h3lvACCjRTnl5DMYbVT37vMPe81xYgee0VqEFaD/3otHwvqVwkWC6GA3r6siXXA45ZeJod1qFQI4G9pixUuFQ6h5cjyk8PFi9n4XWeREgiRjFx6PUVzyK9MnA9swuRHRkLuICmlrpfGjxT0mt/a2m6vbgxf9prFijXV1ngV7GXaqT4hAsk5vnyAQt6kfnIDUBOywnRXLHOHZpzI2ZTRwV8JJ3DIMhEtJZ9At3u+FzXWvaFG7MMBinujQ39iUipKWYgKPJTEkjwOsfROVhclgMC6VZKG/f7FJw85/vKogssxkYimWfqLeDboHjzVzMhUzUPoky59JcNrt48/95Ko7TLMze46mwkmdFMY/5aeekGZhwS2qUHUEb+VV5LxloXkoV6lxlQCCgmNhMRvzYbh2otozjuDvCNOkUU4k+onE2+UfwkVtRlvisA4TLVAltwAMF6h4iVUEz/OiA37756E9qg3rMarV+HKJbjvOtnXOYEzY047LRL17dUl1Ovcg2yKdmaeJa3By0O6Z0mkIDSsIVtDp/pKpr44i2sKsmY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
import { eventNames, phases } from '../utils/consts'; | ||
import { validateData, validateSchema } from '../utils/validation.js'; | ||
import * as simulatorActions from './simulator_actions'; | ||
|
||
// Receiving Message Thunks | ||
export function receiveMessage(payload) { | ||
// Routes received messages to other thunks as necessary | ||
return (dispatch) => { | ||
try { | ||
const { msgData, msgName, props: attrs, version } = JSON.parse(payload.data); | ||
|
||
if(version=== "1.1.0" || version === "1.1.1") { | ||
var errmsg = 'this simulator only supports wdcs that are using version 2.0 of the api or later, your wdc\'s version is: ' + wdcapiversion; | ||
toastr.error(errmsg, 'unsupported wdc version error:') | ||
} | ||
|
||
if (attrs) { | ||
dispatch(simulatorActions.setWdcAttrs(attrs)); | ||
} | ||
|
||
switch (msgName) { | ||
case eventNames.LOG: { | ||
dispatch(handleLog(msgData.logMsg)); | ||
break; | ||
} | ||
|
||
case eventNames.LOADED: { | ||
dispatch(handleLoaded(version)); | ||
handleLoaded(); | ||
break; | ||
} | ||
|
||
case eventNames.INIT_CB: { | ||
dispatch(handleInit()); | ||
break; | ||
} | ||
|
||
case eventNames.SUBMIT: { | ||
dispatch(handleSubmit()); | ||
break; | ||
} | ||
|
||
case eventNames.SCHEMA_CB: { | ||
const schema = msgData.schema; | ||
dispatch(handleSchemaCallback(schema)); | ||
break; | ||
} | ||
|
||
case eventNames.DATA_CB: { | ||
const tableName = msgData.tableName; | ||
const data = msgData.data; | ||
dispatch(handleDataCallback(tableName, data)); | ||
break; | ||
} | ||
|
||
case eventNames.SHUTDOWN_CB: { | ||
dispatch(handleShutdownCallback()); | ||
break; | ||
} | ||
|
||
case eventNames.ABORT: { | ||
const errorMsg = msgData.errorMsg; | ||
dispatch(handleAbort(errorMsg)); | ||
break; | ||
} | ||
|
||
case eventNames.DATA_DONE_CB: { | ||
dispatch(handleDataDoneCallback()); | ||
break; | ||
} | ||
|
||
case eventNames.ABORT_AUTH: { | ||
const errorMsg = msgData.errorMsg; | ||
dispatch(handleAbortForAuth(errorMsg)); | ||
break; | ||
} | ||
default: { | ||
// The message was not for the simulator, ignore it. | ||
break; | ||
} | ||
} | ||
} catch (e) { | ||
// The message was not for the simulator, ignore it. | ||
return; | ||
} | ||
}; | ||
} | ||
|
||
export function handleLog(msg) { | ||
return () => { | ||
/* eslint-disable no-console */ | ||
console.log(msg); | ||
/* eslint-enable no-console */ | ||
}; | ||
} | ||
|
||
export function handleLoaded() { | ||
return (dispatch) => { | ||
dispatch(sendInit()); | ||
}; | ||
} | ||
|
||
export function handleInit() { | ||
return (dispatch, getState) => { | ||
const { currentPhase } = getState(); | ||
dispatch(simulatorActions.setPhaseInitCallbackCalled(true)); | ||
dispatch(simulatorActions.setPhaseInProgress(true)); | ||
// If we are in the Gather Data Phase, ask wdc for headers | ||
if (currentPhase === phases.GATHER_DATA) { | ||
dispatch(sendGetHeaders()); | ||
} | ||
}; | ||
} | ||
|
||
export function handleSubmit() { | ||
return (dispatch) => { | ||
// Clean up simulator and start Data Gather Phase | ||
dispatch(simulatorActions.setPhaseSubmitCalled(true)); | ||
dispatch(simulatorActions.setPhaseInProgress(false)); | ||
dispatch(simulatorActions.closeSimulatorWindow()); | ||
dispatch(simulatorActions.resetTables()); | ||
dispatch(simulatorActions.startGatherDataPhase()); | ||
}; | ||
} | ||
|
||
export function handleSchemaCallback(schema) { | ||
return (dispatch, getState) => { | ||
// Validate schema, and populate store with new table objects | ||
// using the schema info | ||
const { wdcShouldFetchAllTables } = getState(); | ||
if (validateSchema(schema)) { | ||
const newTables = schema.reduce((tables, tableInfo) => { | ||
const newTable = { | ||
schema: tableInfo, | ||
data: [], | ||
}; | ||
|
||
return { ...tables, [tableInfo.id]: newTable }; | ||
}, {}); | ||
dispatch(simulatorActions.addTables(newTables)); | ||
|
||
// if we are supposed to automatically fetch the tables, do it | ||
if (wdcShouldFetchAllTables) { | ||
dispatch(fetchAllData()); | ||
} else { | ||
// We we aren't going to fetch immediately | ||
// consider the phase paused until the getGeta | ||
// message is sent. This is so we can properly | ||
// disable the Fetch Data button | ||
dispatch(simulatorActions.setPhaseInProgress(false)); | ||
} | ||
} else { | ||
toastr.error('Please see debug console for details.', 'WDC Validation Error'); | ||
} | ||
}; | ||
} | ||
|
||
export function handleDataCallback(tableName, data) { | ||
return (dispatch, getState) => { | ||
// if data is valid, add it to the appropriate tables object in the store | ||
if (validateData(data)) { | ||
const { tables } = getState(); | ||
const newTables = Object.assign({}, tables); | ||
newTables[tableName].data = newTables[tableName].data.concat(data); | ||
dispatch(simulatorActions.setTables(newTables)); | ||
} else { | ||
toastr.error('Please see debug console for details.', 'WDC Validation Error'); | ||
} | ||
}; | ||
} | ||
|
||
export function handleDataDoneCallback() { | ||
return (dispatch) => { | ||
// Tell the wdc to shutdown now that we have our data | ||
dispatch(sendShutdown()); | ||
}; | ||
} | ||
|
||
export function handleShutdownCallback() { | ||
return (dispatch) => { | ||
// Wdc has told us it's shutdown, end our phase | ||
dispatch(simulatorActions.setPhaseInProgress(false)); | ||
}; | ||
} | ||
|
||
export function handleAbort(errMsg) { | ||
return (dispatch) => { | ||
// Something went wrong, end phase, alert user | ||
dispatch(simulatorActions.setPhaseInProgress(false)); | ||
console.error(errMsg); | ||
toastr.error(errMsg, 'The WDC reported an error:'); | ||
}; | ||
} | ||
|
||
export function handleAbortForAuth(errMsg) { | ||
return (dispatch) => { | ||
// Need auth, close the simulator, tell the user | ||
const toastTitle = 'The WDC has been aborted for auth, ' + | ||
'use the "Start Auth Phase" to test ' + | ||
'your WDC Auth Mode:'; | ||
dispatch(simulatorActions.setPhaseInProgress(false)); | ||
dispatch(simulatorActions.closeSimulatorWindow()); | ||
toastr.error(errMsg, toastTitle); | ||
}; | ||
} | ||
|
||
// Send message thunks | ||
export function sendMessage(messageName, payload = {}) { | ||
return (dispatch, getState) => { | ||
// Construct payload and send info to whichever | ||
// window we are posting too | ||
const { simulatorWindow, wdcAttrs } = getState(); | ||
const messagePayload = JSON.stringify({ | ||
msgName: messageName, | ||
msgData: payload, | ||
props: wdcAttrs, | ||
}); | ||
simulatorWindow.postMessage(messagePayload, '*'); | ||
}; | ||
} | ||
|
||
export function sendInit() { | ||
return (dispatch, getState) => { | ||
const { currentPhase } = getState(); | ||
dispatch(sendMessage(eventNames.INIT, { phase: currentPhase })); | ||
}; | ||
} | ||
|
||
export function sendGetHeaders() { | ||
return (dispatch) => { | ||
dispatch(sendMessage(eventNames.SCHEMA_GET)); | ||
}; | ||
} | ||
|
||
// Takes an array of tableuInfo/incValue pairs | ||
export function sendGetData(tablesAndIncrementValues, isFreshFetch) { | ||
return (dispatch) => { | ||
if (isFreshFetch) { | ||
const tableKey = tablesAndIncrementValues[0].tableInfo.id; | ||
dispatch(simulatorActions.resetTableData(tableKey)); | ||
} | ||
|
||
dispatch(sendMessage(eventNames.DATA_GET, { tablesAndIncrementValues })); | ||
dispatch(simulatorActions.setPhaseInProgress(true)); | ||
}; | ||
} | ||
|
||
export function sendShutdown() { | ||
return (dispatch) => { | ||
dispatch(sendMessage(eventNames.SHUTDOWN)); | ||
}; | ||
} | ||
|
||
export function fetchAllData() { | ||
return (dispatch, getState) => { | ||
// Loop through our tables and tell the wdc to give us the data | ||
// for each of them | ||
const { tables } = getState(); | ||
Object.keys(tables).forEach(key => { | ||
dispatch(sendGetData([{ tableInfo: tables[key].schema }])); | ||
}); | ||
}; | ||
} |
Oops, something went wrong.