Skip to content

Commit

Permalink
feat(asset-bundle): fix cache api: change return value from stream to…
Browse files Browse the repository at this point in the history
… promise
  • Loading branch information
yyc-git committed May 17, 2019
1 parent 606b8b1 commit efa904b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 38 deletions.
12 changes: 9 additions & 3 deletions src/asset_bundle/import/ImportABSystem.re
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ module All = {
ParseABSystem.WAB.unsafeGetHashId(abRelativePath, wholeManifest);

isAssetBundleArrayBufferCachedFunc(. abRelativePath, hashId)
|> Most.fromPromise
|> Most.flatMap(isCached =>
isCached ?
getAssetBundleArrayBufferCacheFunc(. abRelativePath) :
getAssetBundleArrayBufferCacheFunc(. abRelativePath)
|> Most.fromPromise :
LoadABSystem.load(
getAssetBundlePathFunc(.) ++ abRelativePath,
fetchFunc,
)
|> Most.flatMap(ab =>
cacheAssetBundleArrayBufferFunc(. abRelativePath, ab, hashId)
|> Most.fromPromise
|> Most.map(() => ab)
|> Most.concat(Most.just(ab))
)
Expand All @@ -49,6 +52,7 @@ module SAB = {
),
) =>
initAssetBundleArrayBufferCacheFunc(.)
|> Most.fromPromise
|> Most.concat(
Most.just(sabRelativePath)
|> Most.flatMap(sabRelativePath => {
Expand Down Expand Up @@ -171,6 +175,7 @@ module RAB = {
ParseABSystem.WAB.getWholeDependencyRelationMap(wholeManifest);

initAssetBundleArrayBufferCacheFunc(.)
|> Most.fromPromise
|> Most.concat(
FindDependencyDataSystem.findAllDependencyRAbRelativePathByDepthSearch(
abRelativePath,
Expand Down Expand Up @@ -218,7 +223,8 @@ module RAB = {
};

module WAB = {
let loadWABAndSetToState = (wabRelativePath, (getAssetBundlePathFunc, fetchFunc)) =>
let loadWABAndSetToState =
(wabRelativePath, (getAssetBundlePathFunc, fetchFunc)) =>
Most.just(wabRelativePath)
|> Most.flatMap(wabRelativePath => {
let state =
Expand Down Expand Up @@ -250,7 +256,7 @@ module WAB = {
)
|> StateDataMainService.setState(StateDataMain.stateData)
|> ignore;
})
});
});
};

Expand Down
13 changes: 10 additions & 3 deletions src/asset_bundle/import/LoadABSystem.re
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ let load = (abPath: string, fetchFunc) =>
/* TODO need rewrite by editor */
let getAssetBundlePath = (.) => "";

let initAssetBundleArrayBufferCache = (.) => Most.empty();
let initAssetBundleArrayBufferCache =
(.) =>
Js.Promise.make((~resolve, ~reject) =>
(PromiseType.convertResolveToUnit(resolve))(.)
);

let isAssetBundleArrayBufferCached =
(. abRelativePath: string, hashId: string) => Most.just(false);
(. abRelativePath: string, hashId: string) =>
Js.Promise.make((~resolve, ~reject) => resolve(. false));

let getAssetBundleArrayBufferCache =
(. abRelativePath: string) =>
Expand All @@ -49,4 +54,6 @@ let getAssetBundleArrayBufferCache =

let cacheAssetBundleArrayBuffer =
(. abRelativePath: string, ab: ArrayBuffer.t, hashId: string) =>
Most.empty();
Js.Promise.make((~resolve, ~reject) =>
(PromiseType.convertResolveToUnit(resolve))(.)
);
2 changes: 2 additions & 0 deletions src/external/PromiseType.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
external convertResolveToUnit: ((. 'a) => unit) => (. unit) => unit =
"%identity";
52 changes: 34 additions & 18 deletions test/integration/no_worker/asset_bundle/load/loadRAB_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ let _ =
(.) => {
valueRef := 2;

Most.empty();
Js.Promise.make((~resolve, ~reject) =>
(PromiseType.convertResolveToUnit(resolve))(.)
);
},
~isAssetBundleArrayBufferCachedFunc=
(. _, _) => false |> Most.just,
(. _, _) =>
Js.Promise.make((~resolve, ~reject) => resolve(. false)),
~fetchFunc=fetch,
(),
)
Expand Down Expand Up @@ -101,7 +104,8 @@ let _ =
ImportABTool.RAB.loadAllDependencyRABAndSetToState(
~abRelativePath=rab3RelativePath,
~isAssetBundleArrayBufferCachedFunc=
(. _, _) => false |> Most.just,
(. _, _) =>
Js.Promise.make((~resolve, ~reject) => resolve(. false)),
~fetchFunc=fetch,
(),
)
Expand All @@ -128,9 +132,11 @@ let _ =
ImportABTool.RAB.loadAllDependencyRABAndSetToState(
~abRelativePath=rab3RelativePath,
~isAssetBundleArrayBufferCachedFunc=
(. _, _) => true |> Most.just,
(. _, _) =>
Js.Promise.make((~resolve, ~reject) => resolve(. true)),
~getAssetBundleArrayBufferCacheFunc=
(. _) => rab1 |> Most.just,
(. _) =>
Js.Promise.make((~resolve, ~reject) => resolve(. rab1)),
~fetchFunc=fetch,
(),
)
Expand Down Expand Up @@ -158,13 +164,17 @@ let _ =
~abRelativePath=rab3RelativePath,
~isAssetBundleArrayBufferCachedFunc=
(. abRelativePath, hashId) =>
(
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
true : false
)
|> Most.just,
Js.Promise.make((~resolve, ~reject) =>
resolve(.
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
true : false,
)
),
~getAssetBundleArrayBufferCacheFunc=
(. _) => rab1 |> Most.just,
(. _) =>
Js.Promise.make((~resolve, ~reject) =>
resolve(. rab1)
),
~fetchFunc=fetch,
(),
)
Expand Down Expand Up @@ -200,20 +210,26 @@ let _ =
~abRelativePath=rab3RelativePath,
~isAssetBundleArrayBufferCachedFunc=
(. abRelativePath, hashId) =>
(
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
true : false
)
|> Most.just,
Js.Promise.make((~resolve, ~reject) =>
resolve(.
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
true : false,
)
),
~getAssetBundleArrayBufferCacheFunc=
(. _) => rab1 |> Most.just,
(. _) =>
Js.Promise.make((~resolve, ~reject) =>
resolve(. rab1)
),
~cacheAssetBundleArrayBufferFunc=
(. abRelativePath, ab, hashId) => {
cachedABRelativePathRef := abRelativePath;

cachedABHashIdRef := hashId;

Most.empty();
Js.Promise.make((~resolve, ~reject) =>
(PromiseType.convertResolveToUnit(resolve))(.)
);
},
~fetchFunc=fetch,
(),
Expand Down
41 changes: 29 additions & 12 deletions test/integration/no_worker/asset_bundle/load/loadSAB_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ let _ =
(.) => {
valueRef := 2;

Most.empty();
Js.Promise.make((~resolve, ~reject) =>
(PromiseType.convertResolveToUnit(resolve))(.)
);
},
~isAssetBundleArrayBufferCachedFunc=
(. _, _) => false |> Most.just,
(. _, _) =>
Js.Promise.make((~resolve, ~reject) =>
resolve(. false)
),
~fetchFunc=fetch,
(),
)
Expand All @@ -126,9 +131,11 @@ let _ =
ImportABTool.SAB.loadSABAndSetToState(
~sabRelativePath=sab1RelativePath,
~isAssetBundleArrayBufferCachedFunc=
(. _, _) => true |> Most.just,
(. _, _) =>
Js.Promise.make((~resolve, ~reject) => resolve(. true)),
~getAssetBundleArrayBufferCacheFunc=
(. _) => rab1 |> Most.just,
(. _) =>
Js.Promise.make((~resolve, ~reject) => resolve(. rab1)),
~fetchFunc=fetch,
(),
)
Expand Down Expand Up @@ -156,13 +163,17 @@ let _ =
~sabRelativePath=sab1RelativePath,
~isAssetBundleArrayBufferCachedFunc=
(. abRelativePath, hashId) =>
(
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
true : false
)
|> Most.just,
Js.Promise.make((~resolve, ~reject) =>
resolve(.
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
true : false,
)
),
~getAssetBundleArrayBufferCacheFunc=
(. _) => rab1 |> Most.just,
(. _) =>
Js.Promise.make((~resolve, ~reject) =>
resolve(. rab1)
),
~fetchFunc=fetch,
(),
)
Expand Down Expand Up @@ -194,7 +205,10 @@ let _ =
ImportABTool.SAB.loadSABAndSetToState(
~sabRelativePath=sab1RelativePath,
~isAssetBundleArrayBufferCachedFunc=
(. abRelativePath, hashId) => false |> Most.just,
(. abRelativePath, hashId) =>
Js.Promise.make((~resolve, ~reject) =>
resolve(. false)
),
~fetchFunc=fetch,
(),
)
Expand Down Expand Up @@ -227,7 +241,10 @@ let _ =
ImportABTool.SAB.loadSABAndSetToState(
~sabRelativePath=sab1RelativePath,
~isAssetBundleArrayBufferCachedFunc=
(. abRelativePath, hashId) => false |> Most.just,
(. abRelativePath, hashId) =>
Js.Promise.make((~resolve, ~reject) =>
resolve(. false)
),
~fetchFunc=fetch,
(),
)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/asset_bundle/LoadABSystem_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let _ =
describe("isAssetBundleArrayBufferCached", () =>
testPromise("return false", () =>
LoadABSystem.isAssetBundleArrayBufferCached(. "", "")
|> MostTool.testStream(result => result |> expect == false |> resolve)
|> then_(result => result |> expect == false |> resolve)
)
);

Expand All @@ -38,7 +38,7 @@ let _ =
let ab = Obj.magic(-1);

LoadABSystem.cacheAssetBundleArrayBuffer(. "", ab, "")
|> MostTool.testStream(() => 1 |> expect == 1 |> resolve);
|> then_(_ => 1 |> expect == 1 |> resolve);
})
);
});

0 comments on commit efa904b

Please sign in to comment.