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

move subPackPipe to engine and cache asset slowly #72

Merged
merged 2 commits into from Feb 19, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+59 −38
Diff settings

Always

Just for now

Prev

use asset directly, then cache it slowly

refine
  • Loading branch information...
holycanvas committed Feb 19, 2019
commit 221c6b641f06b69e9de6f0eea37e771526be361d
@@ -35,6 +35,12 @@ var binary_format = [

const REGEX = /^\w+:\/\/.*/;

// used to control cache
var cacheQueue = {};
var checkNextPeriod = false;
// cache one per cycle
var cachePeriod = 100;

var fs = wx.getFileSystemManager ? wx.getFileSystemManager() : null;

var _newAssets = [];
@@ -185,12 +191,17 @@ function nextPipe(item, callback) {
var queue = cc.LoadingItems.getQueue(item);
queue.addListener(item.id, function (item) {
if (item.error) {
fs && fs.unlink({
filePath: item.url,
success: function () {
cc.log('Load failed, removed local file ' + item.url + ' successfully!');
}
});
if (item.url in cacheQueue) {
delete cacheQueue[item.url];
}
else {
fs && fs.unlink({
filePath: item.url,
success: function () {
cc.log('Load failed, removed local file ' + item.url + ' successfully!');
}
});
}
}
});
callback(null, null);
@@ -322,6 +333,38 @@ function ensureDirFor (path, callback) {
});
}

function cacheAsset (url, localPath) {
cacheQueue[url] = localPath;

if (!checkNextPeriod) {
checkNextPeriod = true;
function cache () {
checkNextPeriod = false;
for (var url in cacheQueue) {
var localPath = cacheQueue[url];
ensureDirFor(localPath, function () {
// Save to local path
wx.saveFile({
tempFilePath: url,
filePath: localPath,
success: function (res) {
cc.log('cache success ' + localPath);
}
});
});

delete cacheQueue[url];
if (!cc.js.isEmptyObject(cacheQueue) && !checkNextPeriod) {
checkNextPeriod = true;
setTimeout(cache, cachePeriod);
}
return;
}
};
setTimeout(cache, cachePeriod);
}
}

function downloadRemoteFile (item, callback) {
// Download from remote server
var relatUrl = item.url;
@@ -340,37 +383,15 @@ function downloadRemoteFile (item, callback) {
if (res.statusCode === 200 && res.tempFilePath) {
// http reading is not cached
var temp = res.tempFilePath;
var localPath = wx.env.USER_DATA_PATH + '/' + relatUrl;
// check and mkdir remote folder has exists
ensureDirFor(localPath, function () {
// Save to local path
wx.saveFile({
tempFilePath: res.tempFilePath,
filePath: localPath,
success: function (res) {
// cc.log('save:' + localPath);
item.url = res.savedFilePath;
if (item.type && non_text_format.indexOf(item.type) !== -1) {
nextPipe(item, callback);
}
else {
readText(item, callback);
}
},
fail: function (res) {
// Failed to save file, then just use temp
console.log(res && res.errMsg ? res.errMsg : 'save file failed: ' + remoteUrl);
console.log('It might be due to out of storage spaces, you can clean your storage spaces manually.');
item.url = temp;
if (item.type && non_text_format.indexOf(item.type) !== -1) {
nextPipe(item, callback);
}
else {
readText(item, callback);
}
}
});
});
item.url = temp;
if (item.type && non_text_format.indexOf(item.type) !== -1) {
nextPipe(item, callback);
}
else {
readText(item, callback);
}
cacheAsset(temp, wx.env.USER_DATA_PATH + '/' + relatUrl);

}
else {
cc.warn("Download file failed: " + remoteUrl);
@@ -387,7 +408,7 @@ function downloadRemoteFile (item, callback) {
errorMessage: res && res.errMsg ? res.errMsg : "Download file failed: " + remoteUrl
}, null);
}
})
});
}

// function downloadRemoteTextFile (item, callback) {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.