Skip to content

Commit efa904b

Browse files
committed
feat(asset-bundle): fix cache api: change return value from stream to promise
1 parent 606b8b1 commit efa904b

File tree

6 files changed

+86
-38
lines changed

6 files changed

+86
-38
lines changed

src/asset_bundle/import/ImportABSystem.re

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ module All = {
1818
ParseABSystem.WAB.unsafeGetHashId(abRelativePath, wholeManifest);
1919

2020
isAssetBundleArrayBufferCachedFunc(. abRelativePath, hashId)
21+
|> Most.fromPromise
2122
|> Most.flatMap(isCached =>
2223
isCached ?
23-
getAssetBundleArrayBufferCacheFunc(. abRelativePath) :
24+
getAssetBundleArrayBufferCacheFunc(. abRelativePath)
25+
|> Most.fromPromise :
2426
LoadABSystem.load(
2527
getAssetBundlePathFunc(.) ++ abRelativePath,
2628
fetchFunc,
2729
)
2830
|> Most.flatMap(ab =>
2931
cacheAssetBundleArrayBufferFunc(. abRelativePath, ab, hashId)
32+
|> Most.fromPromise
3033
|> Most.map(() => ab)
3134
|> Most.concat(Most.just(ab))
3235
)
@@ -49,6 +52,7 @@ module SAB = {
4952
),
5053
) =>
5154
initAssetBundleArrayBufferCacheFunc(.)
55+
|> Most.fromPromise
5256
|> Most.concat(
5357
Most.just(sabRelativePath)
5458
|> Most.flatMap(sabRelativePath => {
@@ -171,6 +175,7 @@ module RAB = {
171175
ParseABSystem.WAB.getWholeDependencyRelationMap(wholeManifest);
172176

173177
initAssetBundleArrayBufferCacheFunc(.)
178+
|> Most.fromPromise
174179
|> Most.concat(
175180
FindDependencyDataSystem.findAllDependencyRAbRelativePathByDepthSearch(
176181
abRelativePath,
@@ -218,7 +223,8 @@ module RAB = {
218223
};
219224

220225
module WAB = {
221-
let loadWABAndSetToState = (wabRelativePath, (getAssetBundlePathFunc, fetchFunc)) =>
226+
let loadWABAndSetToState =
227+
(wabRelativePath, (getAssetBundlePathFunc, fetchFunc)) =>
222228
Most.just(wabRelativePath)
223229
|> Most.flatMap(wabRelativePath => {
224230
let state =
@@ -250,7 +256,7 @@ module WAB = {
250256
)
251257
|> StateDataMainService.setState(StateDataMain.stateData)
252258
|> ignore;
253-
})
259+
});
254260
});
255261
};
256262

src/asset_bundle/import/LoadABSystem.re

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ let load = (abPath: string, fetchFunc) =>
3030
/* TODO need rewrite by editor */
3131
let getAssetBundlePath = (.) => "";
3232

33-
let initAssetBundleArrayBufferCache = (.) => Most.empty();
33+
let initAssetBundleArrayBufferCache =
34+
(.) =>
35+
Js.Promise.make((~resolve, ~reject) =>
36+
(PromiseType.convertResolveToUnit(resolve))(.)
37+
);
3438

3539
let isAssetBundleArrayBufferCached =
36-
(. abRelativePath: string, hashId: string) => Most.just(false);
40+
(. abRelativePath: string, hashId: string) =>
41+
Js.Promise.make((~resolve, ~reject) => resolve(. false));
3742

3843
let getAssetBundleArrayBufferCache =
3944
(. abRelativePath: string) =>
@@ -49,4 +54,6 @@ let getAssetBundleArrayBufferCache =
4954

5055
let cacheAssetBundleArrayBuffer =
5156
(. abRelativePath: string, ab: ArrayBuffer.t, hashId: string) =>
52-
Most.empty();
57+
Js.Promise.make((~resolve, ~reject) =>
58+
(PromiseType.convertResolveToUnit(resolve))(.)
59+
);

src/external/PromiseType.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
external convertResolveToUnit: ((. 'a) => unit) => (. unit) => unit =
2+
"%identity";

test/integration/no_worker/asset_bundle/load/loadRAB_test.re

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ let _ =
6161
(.) => {
6262
valueRef := 2;
6363

64-
Most.empty();
64+
Js.Promise.make((~resolve, ~reject) =>
65+
(PromiseType.convertResolveToUnit(resolve))(.)
66+
);
6567
},
6668
~isAssetBundleArrayBufferCachedFunc=
67-
(. _, _) => false |> Most.just,
69+
(. _, _) =>
70+
Js.Promise.make((~resolve, ~reject) => resolve(. false)),
6871
~fetchFunc=fetch,
6972
(),
7073
)
@@ -101,7 +104,8 @@ let _ =
101104
ImportABTool.RAB.loadAllDependencyRABAndSetToState(
102105
~abRelativePath=rab3RelativePath,
103106
~isAssetBundleArrayBufferCachedFunc=
104-
(. _, _) => false |> Most.just,
107+
(. _, _) =>
108+
Js.Promise.make((~resolve, ~reject) => resolve(. false)),
105109
~fetchFunc=fetch,
106110
(),
107111
)
@@ -128,9 +132,11 @@ let _ =
128132
ImportABTool.RAB.loadAllDependencyRABAndSetToState(
129133
~abRelativePath=rab3RelativePath,
130134
~isAssetBundleArrayBufferCachedFunc=
131-
(. _, _) => true |> Most.just,
135+
(. _, _) =>
136+
Js.Promise.make((~resolve, ~reject) => resolve(. true)),
132137
~getAssetBundleArrayBufferCacheFunc=
133-
(. _) => rab1 |> Most.just,
138+
(. _) =>
139+
Js.Promise.make((~resolve, ~reject) => resolve(. rab1)),
134140
~fetchFunc=fetch,
135141
(),
136142
)
@@ -158,13 +164,17 @@ let _ =
158164
~abRelativePath=rab3RelativePath,
159165
~isAssetBundleArrayBufferCachedFunc=
160166
(. abRelativePath, hashId) =>
161-
(
162-
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
163-
true : false
164-
)
165-
|> Most.just,
167+
Js.Promise.make((~resolve, ~reject) =>
168+
resolve(.
169+
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
170+
true : false,
171+
)
172+
),
166173
~getAssetBundleArrayBufferCacheFunc=
167-
(. _) => rab1 |> Most.just,
174+
(. _) =>
175+
Js.Promise.make((~resolve, ~reject) =>
176+
resolve(. rab1)
177+
),
168178
~fetchFunc=fetch,
169179
(),
170180
)
@@ -200,20 +210,26 @@ let _ =
200210
~abRelativePath=rab3RelativePath,
201211
~isAssetBundleArrayBufferCachedFunc=
202212
(. abRelativePath, hashId) =>
203-
(
204-
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
205-
true : false
206-
)
207-
|> Most.just,
213+
Js.Promise.make((~resolve, ~reject) =>
214+
resolve(.
215+
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
216+
true : false,
217+
)
218+
),
208219
~getAssetBundleArrayBufferCacheFunc=
209-
(. _) => rab1 |> Most.just,
220+
(. _) =>
221+
Js.Promise.make((~resolve, ~reject) =>
222+
resolve(. rab1)
223+
),
210224
~cacheAssetBundleArrayBufferFunc=
211225
(. abRelativePath, ab, hashId) => {
212226
cachedABRelativePathRef := abRelativePath;
213227

214228
cachedABHashIdRef := hashId;
215229

216-
Most.empty();
230+
Js.Promise.make((~resolve, ~reject) =>
231+
(PromiseType.convertResolveToUnit(resolve))(.)
232+
);
217233
},
218234
~fetchFunc=fetch,
219235
(),

test/integration/no_worker/asset_bundle/load/loadSAB_test.re

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ let _ =
9797
(.) => {
9898
valueRef := 2;
9999

100-
Most.empty();
100+
Js.Promise.make((~resolve, ~reject) =>
101+
(PromiseType.convertResolveToUnit(resolve))(.)
102+
);
101103
},
102104
~isAssetBundleArrayBufferCachedFunc=
103-
(. _, _) => false |> Most.just,
105+
(. _, _) =>
106+
Js.Promise.make((~resolve, ~reject) =>
107+
resolve(. false)
108+
),
104109
~fetchFunc=fetch,
105110
(),
106111
)
@@ -126,9 +131,11 @@ let _ =
126131
ImportABTool.SAB.loadSABAndSetToState(
127132
~sabRelativePath=sab1RelativePath,
128133
~isAssetBundleArrayBufferCachedFunc=
129-
(. _, _) => true |> Most.just,
134+
(. _, _) =>
135+
Js.Promise.make((~resolve, ~reject) => resolve(. true)),
130136
~getAssetBundleArrayBufferCacheFunc=
131-
(. _) => rab1 |> Most.just,
137+
(. _) =>
138+
Js.Promise.make((~resolve, ~reject) => resolve(. rab1)),
132139
~fetchFunc=fetch,
133140
(),
134141
)
@@ -156,13 +163,17 @@ let _ =
156163
~sabRelativePath=sab1RelativePath,
157164
~isAssetBundleArrayBufferCachedFunc=
158165
(. abRelativePath, hashId) =>
159-
(
160-
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
161-
true : false
162-
)
163-
|> Most.just,
166+
Js.Promise.make((~resolve, ~reject) =>
167+
resolve(.
168+
JudgeTool.isEqual(abRelativePath, rab1RelativePath) ?
169+
true : false,
170+
)
171+
),
164172
~getAssetBundleArrayBufferCacheFunc=
165-
(. _) => rab1 |> Most.just,
173+
(. _) =>
174+
Js.Promise.make((~resolve, ~reject) =>
175+
resolve(. rab1)
176+
),
166177
~fetchFunc=fetch,
167178
(),
168179
)
@@ -194,7 +205,10 @@ let _ =
194205
ImportABTool.SAB.loadSABAndSetToState(
195206
~sabRelativePath=sab1RelativePath,
196207
~isAssetBundleArrayBufferCachedFunc=
197-
(. abRelativePath, hashId) => false |> Most.just,
208+
(. abRelativePath, hashId) =>
209+
Js.Promise.make((~resolve, ~reject) =>
210+
resolve(. false)
211+
),
198212
~fetchFunc=fetch,
199213
(),
200214
)
@@ -227,7 +241,10 @@ let _ =
227241
ImportABTool.SAB.loadSABAndSetToState(
228242
~sabRelativePath=sab1RelativePath,
229243
~isAssetBundleArrayBufferCachedFunc=
230-
(. abRelativePath, hashId) => false |> Most.just,
244+
(. abRelativePath, hashId) =>
245+
Js.Promise.make((~resolve, ~reject) =>
246+
resolve(. false)
247+
),
231248
~fetchFunc=fetch,
232249
(),
233250
)

test/unit/asset_bundle/LoadABSystem_test.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let _ =
2020
describe("isAssetBundleArrayBufferCached", () =>
2121
testPromise("return false", () =>
2222
LoadABSystem.isAssetBundleArrayBufferCached(. "", "")
23-
|> MostTool.testStream(result => result |> expect == false |> resolve)
23+
|> then_(result => result |> expect == false |> resolve)
2424
)
2525
);
2626

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

4040
LoadABSystem.cacheAssetBundleArrayBuffer(. "", ab, "")
41-
|> MostTool.testStream(() => 1 |> expect == 1 |> resolve);
41+
|> then_(_ => 1 |> expect == 1 |> resolve);
4242
})
4343
);
4444
});

0 commit comments

Comments
 (0)