Last active
September 6, 2017 16:21
-
-
Save kevzettler/5042f5675e4f3e809f2a8e08180f6ef3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { | |
REQUEST_ASSET_BINARY, | |
RECEIVE_ASSET_BINARY | |
} from './constants'; | |
export const requestAssetBinary = (path) => ({ | |
type: REQUEST_ASSET_BINARY, | |
path | |
}); | |
export const receiveAssetBinary = (path, arrayBuffer) => ({ | |
type : RECEIVE_ASSET_BINARY, | |
path, | |
arrayBuffer, | |
}); | |
export const fetchAssetBinary = (assetName) => (dispatch, getState) => { | |
const state = getState(); | |
const assetPath = state.config.assetPath + '/' + assetName; | |
if(!state.assets[assetName]){ | |
dispatch(requestAssetBinary(assetName)); | |
return fetch(assetPath) | |
.then(response => response.arrayBuffer()) | |
.then(arrayBuffer => { | |
dispatch(receiveAssetBinary(assetName, arrayBuffer)) | |
return arrayBuffer; | |
}); | |
} | |
return new Promise((resolve, reject) => { | |
resolve(state.assets[assetName].arrayBuffer); | |
}); | |
}; |
This file contains hidden or 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
import { handleActions } from 'redux-actions'; | |
import { | |
REQUEST_ASSET_BINARY, | |
RECEIVE_ASSET_BINARY | |
} from './constants'; | |
export const assetReducer = (state, action) => { | |
switch (action.type) { | |
case REQUEST_ASSET_BINARY: | |
if(!state.assets[action.path]){ | |
state.assets[action.path] = {}; | |
} | |
return state; | |
break; | |
case RECEIVE_ASSET_BINARY: | |
state.assets = Object.assign({}, state.assets); | |
state.assets[action.path].arrayBuffer = action.arrayBuffer; | |
return Object.assign({}, state); | |
break; | |
default: | |
return state; | |
} | |
} | |
export default assetReducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment