Skip to content
This repository was archived by the owner on Aug 14, 2025. It is now read-only.
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
6 changes: 2 additions & 4 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ <h1>Basic Example with error message</h1>
</div>
</div> <!-- /container -->

<script src="https://unpkg.com/react@15.3.0/dist/react.js" defer></script>
<script src="https://unpkg.com/react-dom@15.3.0/dist/react-dom.js" defer></script>
<script src="/react-preload.js" defer></script>
<script src="/examples.js" defer></script>
<!--<script src="https://unpkg.com/react@15.3.0/dist/react.js" defer></script>-->
<!--<script src="https://unpkg.com/react-dom@15.3.0/dist/react-dom.js" defer></script>-->
</body>
</html>
10 changes: 8 additions & 2 deletions examples/src/Examples.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import Preload from 'react-preload';
import Preload from '../../modules/index';
import Spinner from './Spinner';

const imgBase = 'https://picsum.photos/800/600?';
Expand Down Expand Up @@ -28,6 +28,11 @@ export class Example1 extends React.Component {
})
}

_getProgress = (completedCount, total) => {
const percent = (Math.round(completedCount/total * 10000)/100).toFixed(2) + '%'
console.log(percent);
}

render() {
const images = [
`${imgBase}text=1-${this.state.cacheBreaker}`,
Expand All @@ -38,7 +43,7 @@ export class Example1 extends React.Component {

return (
<div>
<button
<button
onClick={this._handleClickNewImages}
type="button"
className="btn btn-primary"
Expand All @@ -48,6 +53,7 @@ export class Example1 extends React.Component {
onError={this._handleImageLoadError}
onSuccess={this._handleImageLoadSuccess}
loadingIndicator={<Spinner />}
getProgress={this._getProgress}
mountChildren
resolveOnError
>
Expand Down
2 changes: 2 additions & 0 deletions examples/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom';
import ExamplePlayer from './ExamplePlayer';
import { Example1 } from './Examples';

Expand Down
14 changes: 8 additions & 6 deletions modules/ImageHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const reflect = p => p.then(v => ({v, status: 'fulfilled' }),
e => ({e, status: 'rejected' }));

const ImageHelper = {
loadImage(url, options) {
completedCount: 0,
loadImage(url, options, callback) {
const image = ImageCache.get(url, options);

return new Promise((resolve, reject) => {
const handleSuccess = () => {
callback && callback(++this.completedCount);
resolve(image);
};
const handleError = () => {
Expand Down Expand Up @@ -48,16 +50,16 @@ const ImageHelper = {
});
},

loadImages(urls, options) {
const promises = urls.map(url => reflect(this.loadImage(url, options)));
loadImages(urls, options, callback) {
if (this.completedCount !== 0) this.completedCount = 0; // 重置0
const promises = urls.map(url => reflect(this.loadImage(url, options, callback)));
return Promise.all(promises).then((promises) => {
return promises.map((p) => {
if(p.status !== 'fulfilled') {
if (p.status !== 'fulfilled') {
throw new Exception('One or more images failed to load');
}
return p;
}
)
});
});
},

Expand Down
10 changes: 9 additions & 1 deletion modules/Preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const propTypes = {
// Success callback
onSuccess: PropTypes.func,

// getProgress
getProgress: PropTypes.func,

// Whether or not we should still show the content
// even if there is a preloading error
resolveOnError: PropTypes.bool,
Expand All @@ -37,6 +40,7 @@ const defaultProps = {
autoResolveDelay: 0,
onError: null,
onSuccess: null,
getProgress: null,
resolveOnError: true,
mountChildren: true,
};
Expand Down Expand Up @@ -87,7 +91,7 @@ class Preload extends React.Component {

loadImages = () => {
const { images, autoResolveDelay } = this.props;
ImageHelper.loadImages(images).then(
ImageHelper.loadImages(images, {}, this.updateProgress).then(
this._handleSuccess,
this._handleError,
);
Expand All @@ -100,6 +104,10 @@ class Preload extends React.Component {
}
}

updateProgress = (completedCount) => {
this.props.getProgress(completedCount, this.props.images.length);
}

_handleAutoResolve = () => {
this._handleSuccess({ didAutoResolve: true });
};
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.10.0",
"react-dom": "^15.0.0",
"html-webpack-plugin": "^3.2.0",
"react": "^15.0.0",
"react-dom": "^15.6.2",
"react-test-renderer": "^16.4.2",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0",
Expand Down
55 changes: 33 additions & 22 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = [{
devtool: 'source-map',
entry: {
'react-preload': './modules/index',
'examples': './examples/src/index',
},
output: {
path: path.resolve('dist'),
filename: '[name].js',
libraryTarget: 'var',
library: 'ReactPreload'
},
externals: {
react: 'React',
'react-dom': 'ReactDOM',
'react-preload': 'ReactPreload',
},
module: {
rules: [{
test: /\.js$/,
use: 'babel-loader'
}]
}
devtool: 'source-map',
entry: {
// 'react-preload': './modules/index',
// 'examples': './examples/src/index',
main: ['./examples/src/index'],
vendor: ['react', 'react-dom'],
},
output: {
path: path.resolve('dist'),
filename: '[name].js',
// libraryTarget: 'var',
// library: 'ReactPreload'
},
externals: {
// 'react': 'React',
// 'react-dom': 'ReactDOM',
// 'react-preload': 'ReactPreload',
},
module: {
rules: [{
test: /\.js$/,
use: 'babel-loader'
}]
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './examples/index.html',
chunks: ['main', 'vendor'],
}),
]

}];