Skip to content
Permalink
Browse files

feat(asset-bundle): run test->use.html use indexDB for cache asset bu…

…ndle

change asset bundle cache api to be async;
  • Loading branch information...
yyc-git committed Apr 26, 2019
1 parent 03d434c commit 7a643fc626084f521249157f530eef88afb9c7c7
@@ -53,10 +53,172 @@
function _init1(script, api, state) {
var databaseName = "Wonder_Database";
var tableName = "Wonder_AssetBundle_Cache";
var cacheFieldName = "Cache";
var hashIdFieldName = "HashId";
var abRelativePathFieldName = "ABRelativePath";
function _getAssetBundlePath() {
return "asset_bundle2/";
};
function _initAssetBundleArrayBufferCache() {
if (!!window.db) {
console.log("already init before");
return api.fromPromiseStream(
new Promise((resolve) => {
resolve();
}, (reject) => {
reject("error");
})
)
};
var request = window.indexedDB.open(databaseName);
return api.fromPromiseStream(
new Promise((resolve, reject) => {
request.onerror = (event) => {
reject("open database error: ", event.target.errorCode);
};
request.onsuccess = (event) => {
var db = event.target.result;
console.log("open onsuccess");
window.db = db;
resolve();
};
request.onupgradeneeded = (event) => {
var db = event.target.result;
console.log("open onupgradeneeded");
if (!db.objectStoreNames.contains(tableName)) {
var objectStore = db.createObjectStore(
tableName,
{
keyPath: abRelativePathFieldName,
autoIncrement: false
}
);
// objectStore.createIndex(cacheFieldName, cacheFieldName, { unique: false });
// objectStore.createIndex(hashIdFieldName, hashIdFieldName, { unique: true });
// objectStore.createIndex(abRelativePathFieldName, abRelativePathFieldName, { unique: true });
}
else {
reject("database->table error: shouldn't exist table: ", tableName);
}
};
})
)
};
function _isAssetBundleArrayBufferCached(abRelativePath, hashId) {
var transaction = window.db.transaction([tableName]);
var objectStore = transaction.objectStore(tableName);
// var abRelativePath = objectStore.index(abRelativePathFieldName);
var request = objectStore.get(abRelativePath);
return api.fromPromiseStream(
new Promise((resolve, reject) => {
request.onerror = (event) => {
reject("error: ", event.target.errorCode);
};
request.onsuccess = (event) => {
if (request.result) {
console.log("is cached: ", abRelativePath, request.result[hashIdFieldName] === hashId);
resolve(request.result[hashIdFieldName] === hashId);
} else {
console.log("get no data");
resolve(false);
}
};
})
)
};
function _getAssetBundleArrayBufferCache(abRelativePath) {
var transaction = window.db.transaction([tableName]);
var objectStore = transaction.objectStore(tableName);
// var abRelativePath = objectStore.index(abRelativePathFieldName);
// var request = abRelativePath.get(abRelativePath);
// var abRelativePath = objectStore.index(abRelativePathFieldName);
var request = objectStore.get(abRelativePath);
return api.fromPromiseStream(
new Promise((resolve, reject) => {
request.onerror = (event) => {
reject("error: ", event.target.errorCode);
};
request.onsuccess = (event) => {
if (request.result) {
console.log("get cached data:", abRelativePath, request.result[cacheFieldName]);
resolve(request.result[cacheFieldName]);
} else {
console.log("get no data");
resolve(null);
}
};
})
)
};
function _cacheAssetBundleArrayBuffer(abRelativePath, ab, hashId) {
var transaction = window.db.transaction([tableName], "readwrite");
var objectStore = transaction.objectStore(tableName);
// var abRelativePath = objectStore.index(abRelativePathFieldName);
// var request = abRelativePath.get(abRelativePath);
// var request = objectStore.get(abRelativePath);
var data = {};
data[abRelativePathFieldName] = abRelativePath;
data[cacheFieldName] = ab;
data[hashIdFieldName] = hashId;
var request =
objectStore.put(
data
);
return api.fromPromiseStream(
new Promise((resolve, reject) => {
request.onerror = (event) => {
reject("cache fail: ", event.target.errorCode);
};
request.onsuccess = (event) => {
console.log("cache success: ", abRelativePath);
resolve();
};
})
)
};
// var loadAssetBundle = api.loadAssetBundle;
// var flatMapStream = api.flatMapStream;
@@ -103,9 +265,10 @@
manifest,
[
_getAssetBundlePath,
api.isAssetBundleArrayBufferCached,
api.getAssetBundleArrayBufferCache,
api.cacheAssetBundleArrayBuffer
_initAssetBundleArrayBufferCache,
_isAssetBundleArrayBufferCached,
_getAssetBundleArrayBufferCache,
_cacheAssetBundleArrayBuffer
]
).concat(
@@ -114,9 +277,10 @@
manifest,
[
_getAssetBundlePath,
api.isAssetBundleArrayBufferCached,
api.getAssetBundleArrayBufferCache,
api.cacheAssetBundleArrayBuffer
_initAssetBundleArrayBufferCache,
_isAssetBundleArrayBufferCached,
_getAssetBundleArrayBufferCache,
_cacheAssetBundleArrayBuffer
]
)
@@ -15,6 +15,8 @@ let _handleStreamError = e => {

let dynamicLoadAB = needRewriteAPI => {
let getAssetBundlePath = needRewriteAPI##getAssetBundlePath;
let initAssetBundleArrayBufferCache =
needRewriteAPI##initAssetBundleArrayBufferCache;
let isAssetBundleArrayBufferCached =
needRewriteAPI##isAssetBundleArrayBufferCached;
let getAssetBundleArrayBufferCache =
@@ -54,6 +56,7 @@ let dynamicLoadAB = needRewriteAPI => {
manifest,
(
getAssetBundlePath,
initAssetBundleArrayBufferCache,
isAssetBundleArrayBufferCached,
getAssetBundleArrayBufferCache,
cacheAssetBundleArrayBuffer,
@@ -66,6 +69,7 @@ let dynamicLoadAB = needRewriteAPI => {
manifest,
(
getAssetBundlePath,
initAssetBundleArrayBufferCache,
isAssetBundleArrayBufferCached,
getAssetBundleArrayBufferCache,
cacheAssetBundleArrayBuffer,
@@ -17,15 +17,20 @@ module All = {
let hashId =
ParseABSystem.WAB.unsafeGetHashId(abRelativePath, wholeManifest);

isAssetBundleArrayBufferCachedFunc(abRelativePath, hashId) ?
getAssetBundleArrayBufferCacheFunc(abRelativePath) |> Most.just :
LoadABSystem.load(
getAssetBundlePathFunc(.) ++ abRelativePath,
fetchFunc,
)
|> Most.tap(ab =>
cacheAssetBundleArrayBufferFunc(abRelativePath, ab, hashId)
);
isAssetBundleArrayBufferCachedFunc(. abRelativePath, hashId)
|> Most.flatMap(isCached =>
isCached ?
getAssetBundleArrayBufferCacheFunc(. abRelativePath) :
LoadABSystem.load(
getAssetBundlePathFunc(.) ++ abRelativePath,
fetchFunc,
)
|> Most.flatMap(ab =>
cacheAssetBundleArrayBufferFunc(. abRelativePath, ab, hashId)
|> Most.map(() => ab)
|> Most.concat(Most.just(ab))
)
);
};
};

@@ -36,6 +41,7 @@ module SAB = {
wholeManifest,
(
getAssetBundlePathFunc,
initAssetBundleArrayBufferCacheFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
@@ -52,36 +58,41 @@ module SAB = {

OperateSABAssetBundleMainService.isLoaded(sabRelativePath, state) ?
Most.empty() :
All.loadAB(
sabRelativePath,
wholeManifest,
wholeDependencyRelationMap,
(
getAssetBundlePathFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
fetchFunc,
),
)
|> Most.tap(sab => {
let state =
StateDataMainService.unsafeGetState(
StateDataMain.stateData,
);
initAssetBundleArrayBufferCacheFunc(.)
|> Most.concat(
All.loadAB(
sabRelativePath,
wholeManifest,
wholeDependencyRelationMap,
(
getAssetBundlePathFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
fetchFunc,
),
)
|> Most.tap(sab => {
let state =
StateDataMainService.unsafeGetState(
StateDataMain.stateData,
);

state
|> OperateSABAssetBundleMainService.markLoaded(
sabRelativePath,
)
|> OperateSABAssetBundleMainService.setLoadedSAB(
sabRelativePath,
sab,
)
|> StateDataMainService.setState(StateDataMain.stateData)
|> ignore;
})
|> Most.map(_ => ());
state
|> OperateSABAssetBundleMainService.markLoaded(
sabRelativePath,
)
|> OperateSABAssetBundleMainService.setLoadedSAB(
sabRelativePath,
sab,
)
|> StateDataMainService.setState(
StateDataMain.stateData,
)
|> ignore;
})
|> Most.map(_ => ()),
);
});
};

@@ -133,6 +144,7 @@ module RAB = {
wholeManifest,
(
getAssetBundlePathFunc,
initAssetBundleArrayBufferCacheFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
@@ -142,28 +154,31 @@ module RAB = {
let wholeDependencyRelationMap =
ParseABSystem.WAB.getWholeDependencyRelationMap(wholeManifest);

FindDependencyDataSystem.findAllDependencyRAbRelativePathByBreadthSearch(
abRelativePath,
wholeDependencyRelationMap,
)
|> Most.from
|> Most.concatMap(rabRelativePathArr =>
rabRelativePathArr
|> Js.Array.map(rabRelativePath =>
_loadAndAssembleRAB(
rabRelativePath,
wholeManifest,
wholeDependencyRelationMap,
(
getAssetBundlePathFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
fetchFunc,
),
)
)
|> Most.mergeArray
initAssetBundleArrayBufferCacheFunc(.)
|> Most.concat(
FindDependencyDataSystem.findAllDependencyRAbRelativePathByBreadthSearch(
abRelativePath,
wholeDependencyRelationMap,
)
|> Most.from
|> Most.concatMap(rabRelativePathArr =>
rabRelativePathArr
|> Js.Array.map(rabRelativePath =>
_loadAndAssembleRAB(
rabRelativePath,
wholeManifest,
wholeDependencyRelationMap,
(
getAssetBundlePathFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
fetchFunc,
),
)
)
|> Most.mergeArray
),
);
};
};

0 comments on commit 7a643fc

Please sign in to comment.
You can’t perform that action at this time.