Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onLoad & onLoadEnd callbacks to Prefetch. #2

Open
wants to merge 1 commit into
base: vizor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Libraries/Pano/Prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,30 @@ const Prefetch = createReactClass({
// Opaque type returned by require('./image.jpg')
PropTypes.number,
]),

/**
* Option onLoad callback called on success
**/
onLoad: PropTypes.func,

/**
* Option onLoadEnd callback called on success or failure
**/
onLoadEnd: PropTypes.func,
},

getDefaultProps: function() {
return {};
},

_onLoad: function() {
this.props.onLoad && this.props.onLoad();
},

_onLoadEnd: function() {
this.props.onLoadEnd && this.props.onLoadEnd();
},

render: function() {
const props = {...this.props} || {};

Expand All @@ -71,7 +89,7 @@ const Prefetch = createReactClass({
}

return (
<RKPrefetch {...props}>
<RKPrefetch {...props} onLoad={this._onLoad} onLoadEnd={this._onLoadEnd}>
{this.props.children}
</RKPrefetch>
);
Expand Down
34 changes: 32 additions & 2 deletions ReactVR/js/Views/Prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ export default class RCTPrefetch extends RCTBaseView {
return;
}

const onError = () => {
onLoadEnd();
};

const onLoad = texture => {
// call onLoad in React
if (texture !== undefined) {
RCTPrefetch.addToCache(uri, texture);
this.UIManager._rnctx.callFunction('RCTEventEmitter', 'receiveEvent', [
this.getTag(),
'topLoad',
[],
]);
}

onLoadEnd();
};

const onLoadEnd = () => {
// call onLoadEnd in React
this.UIManager._rnctx.callFunction('RCTEventEmitter', 'receiveEvent', [
this.getTag(),
'topLoadEnd',
[],
]);
};

// No progress indication for now.
const onProgress = undefined;

if (Array.isArray(uri)) {
// Cubemap, check proper format
if (uri.length !== 6 || !uri[0].uri) {
Expand All @@ -65,13 +95,13 @@ export default class RCTPrefetch extends RCTBaseView {

const loader = new THREE.CubeTextureLoader();
loader.setCrossOrigin('Access-Control-Allow-Origin');
loader.load(urls, texture => RCTPrefetch.addToCache(uri, texture), () => {}, () => {});
loader.load(urls, onLoad, onProgress, onError);
} else {
// Panorama
const url = RCTPrefetch.getUri(uri);
const loader = new THREE.TextureLoader();
loader.setCrossOrigin('Access-Control-Allow-Origin');
loader.load(url, texture => RCTPrefetch.addToCache(uri, texture), () => {}, () => {});
loader.load(url, onLoad, onProgress, onError);
}
}

Expand Down