From 27cf1635f7e1c0d309d44e7f8488788fdd2b386d Mon Sep 17 00:00:00 2001 From: CPPAlien Date: Tue, 26 Nov 2019 11:08:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=B4=E9=9C=B2=20LRU=20=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/downloader.js | 12 ++++++------ painter.js | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/downloader.js b/lib/downloader.js index 3fae8e7..679fbdb 100644 --- a/lib/downloader.js +++ b/lib/downloader.js @@ -34,7 +34,7 @@ export default class Dowloader { * 下载文件,会用 lru 方式来缓存文件到本地 * @param {String} url 文件的 url */ - download(url) { + download(url, lru) { return new Promise((resolve, reject) => { if (!(url && util.isValidUrl(url))) { resolve(url); @@ -51,7 +51,7 @@ export default class Dowloader { }, fail: (error) => { console.error(`the file is broken, redownload it, ${JSON.stringify(error)}`); - downloadFile(url).then((path) => { + downloadFile(url, lru).then((path) => { resolve(path); }, () => { reject(); @@ -59,7 +59,7 @@ export default class Dowloader { }, }); } else { - downloadFile(url).then((path) => { + downloadFile(url, lru).then((path) => { resolve(path); }, () => { reject(); @@ -69,7 +69,7 @@ export default class Dowloader { } } -function downloadFile(url) { +function downloadFile(url, lru) { return new Promise((resolve, reject) => { wx.downloadFile({ url: url, @@ -84,13 +84,13 @@ function downloadFile(url) { filePath: tempFilePath, success: (tmpRes) => { const newFileSize = tmpRes.size; - doLru(newFileSize).then(() => { + lru ? doLru(newFileSize).then(() => { saveFile(url, newFileSize, tempFilePath).then((filePath) => { resolve(filePath); }); }, () => { resolve(tempFilePath); - }); + }) : resolve(tempFilePath); }, fail: (error) => { // 文件大小信息获取失败,则此文件也不要进行存储 diff --git a/painter.js b/painter.js index 6308472..33144f7 100644 --- a/painter.js +++ b/painter.js @@ -52,6 +52,10 @@ Component({ type: Boolean, value: false, }, + LRU: { + type: Boolean, + value: true, + }, action: { type: Object, observer: function (newVal, oldVal) { @@ -226,7 +230,12 @@ Component({ doView.css = Object.assign({}, doView.css, newVal.css) } } - (newVal && newVal.url && doView.url && newVal.url !== doView.url) && (doView.url = newVal.url); + if (newVal && newVal.url && doView.url && newVal.url !== doView.url) { + downloader.download(newVal.url, this.properties.LRU).then((path) => { + doView.originUrl = doView.url + doView.url = path + }) + }; (newVal && newVal.text && doView.text && newVal.text !== doView.text) && (doView.text = newVal.text); (newVal && newVal.content && doView.content && newVal.content !== doView.content) && (doView.content = newVal.content); @@ -605,7 +614,7 @@ Component({ const paletteCopy = JSON.parse(JSON.stringify(palette)); if (paletteCopy.background) { preCount++; - downloader.download(paletteCopy.background).then((path) => { + downloader.download(paletteCopy.background, this.properties.LRU).then((path) => { paletteCopy.background = path; completeCount++; if (preCount === completeCount) { @@ -623,7 +632,7 @@ Component({ if (view && view.type === 'image' && view.url) { preCount++; /* eslint-disable no-loop-func */ - downloader.download(view.url).then((path) => { + downloader.download(view.url, this.properties.LRU).then((path) => { view.originUrl = view.url; view.url = path; wx.getImageInfo({