From e2c13867baf66a6ae22c975563f8755e782d07a9 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sun, 23 Sep 2018 18:34:30 +0200 Subject: adding timeline component --- app/client/common/fileViewer.component.js | 87 +++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 27 deletions(-) (limited to 'app/client/common/fileViewer.component.js') diff --git a/app/client/common/fileViewer.component.js b/app/client/common/fileViewer.component.js index d98073b..4938650 100644 --- a/app/client/common/fileViewer.component.js +++ b/app/client/common/fileViewer.component.js @@ -21,11 +21,37 @@ const video_types = { 'mp4': 'video/mp4', } +const THROTTLE_FETCH_TIME = 200 + class FileViewer extends Component { state = { loading: false, stale: false, - buffer: {} + buffer: {}, + url: null, + } + + componentDidMount(){ + this.fetch() + } + + componentDidUpdate(prevProps){ + if (this.props.file !== prevProps.file) { + this.deferFetch() + } + } + + componentWillUnmount(){ + if (this.state.url) { + window.URL.revokeObjectURL(this.state.url) + } + } + + deferFetch(){ + clearTimeout(this.timeout) + this.timeout = setTimeout(() => { + this.fetch() + }, THROTTLE_FETCH_TIME) } fetch() { @@ -40,48 +66,56 @@ class FileViewer extends Component { this.setState({ buffer: null, loading: true }) if (thumbnail) { - console.log('fetch thumbnail', fn) + // console.log('fetch thumbnail', fn) const size = parseInt(thumbnail) || 200 actions.socket .thumbnail({ module, fn, size }) .then(this.loadBuffer.bind(this)) } else { - console.log('fetch file', fn) + // console.log('fetch file', fn) actions.socket .read_file({ module, fn }) .then(this.loadBuffer.bind(this)) } } + loadBuffer(buffer) { - console.log('fetched buffer', buffer) + // console.log('fetched buffer', buffer) + const { name, buf } = buffer + const ext = extension(name) + if (this.state.url) { + window.URL.revokeObjectURL(this.state.url) + } + let url + if (buf) { + if (ext in image_types) { + url = getURLFor(buf, image_types[ext]) + } else if (ext in audio_types) { + url = getURLFor(buf, audio_types[ext]) + } else if (ext in video_types) { + url = getURLFor(buf, video_types[ext]) + } else { + url = ab2str(buf) + } + } const { stale } = this.state - this.setState({ buffer, loading: false, stale: false, }, () => { - console.log('loaded') + this.setState({ ext, url, buffer, loading: false, stale: false, }, () => { + // console.log('loaded') if (stale) { - console.log('stale, fetching...') + // console.log('stale, fetching...') this.fetch() } }) } - componentDidMount(){ - this.fetch() - } - - componentDidUpdate(nextProps){ - if (this.props.file !== nextProps.file) { - this.fetch() - } - } - render() { const { file } = this.props if (!file) { return
} - const { loading, buffer } = this.state + const { loading, buffer, url, ext } = this.state if (loading) { - return
Loading...
+ return
Loading...
} const { error, @@ -90,24 +124,23 @@ class FileViewer extends Component { buf, } = buffer if (error) { - return
{error}
+ return
{error}
} if (!name) { return
} - if (!buf) { - return
File empty
+ if (!buf || !url) { + return
File empty
} - const ext = extension(name) let tag; if (ext in image_types) { - tag = + tag = } else if (ext in audio_types) { - tag =