From 8ae5dc040a1d2ae23cca82dd7378dd0868bc5571 Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Tue, 30 May 2017 16:25:29 +0200 Subject: [PATCH] elements/file-list: only update when necessary --- elements/file-list.js | 50 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/elements/file-list.js b/elements/file-list.js index 97096c20..3b5065a1 100644 --- a/elements/file-list.js +++ b/elements/file-list.js @@ -23,7 +23,10 @@ var fileList = css` module.exports = function () { var component = microcomponent({ - name: 'file-list' + name: 'file-list', + state: { + update: true + } }) component.on('render', render) component.on('update', update) @@ -32,24 +35,31 @@ module.exports = function () { function render () { var { dat, onupdate } = this.props - if (!dat.files && dat.archive && dat.archive.on) { - dat.files = [] - dat.archive.on('content', function () { - var fs = { name: '/', fs: dat.archive } - var progress = mirror(fs, '/', { dryRun: true }) - progress.on('put', function (file) { - file.name = file.name.slice(1) - if (file.name === '') return - dat.files.push({ - path: file.name, - stat: file.stat - }) - dat.files.sort(function (a, b) { - return a.path.localeCompare(b.path) + if (dat) { + if (dat.files) { + this.state.update = false + } else { + if (dat.archive) { + dat.files = [] + dat.archive.on('content', function () { + var fs = { name: '/', fs: dat.archive } + var progress = mirror(fs, '/', { dryRun: true }) + progress.on('put', function (file) { + file.name = file.name.slice(1) + if (file.name === '') return + dat.files.push({ + path: file.name, + stat: file.stat + }) + dat.files.sort(function (a, b) { + return a.path.localeCompare(b.path) + }) + component.state.update = true + onupdate() + }) }) - onupdate() - }) - }) + } + } } return html` @@ -84,7 +94,7 @@ module.exports = function () { ` } - function update () { - return true + function update (props) { + return props.dat !== this.props.dat || this.state.update } }