diff --git a/src/job/worker/WorkerJobHandleSystem.re b/src/job/worker/WorkerJobHandleSystem.re index 2f0a3c0f92..a8639bb82f 100755 --- a/src/job/worker/WorkerJobHandleSystem.re +++ b/src/job/worker/WorkerJobHandleSystem.re @@ -1,7 +1,7 @@ open StateDataMainType; let _getMainInitJobHandles = () => [ - ("detect_environment", DetectEnvironmentWorkerJob.execJob), + ("detect_environment", DetectEnvironmentMainWorkerJob.execJob), ("init_imgui", InitIMGUIMainWorkerJob.execJob), ("init_event", InitEventMainWorkerJob.execJob), ("init_script", InitScriptMainWorkerJob.execJob), diff --git a/src/job/worker/main/init/DetectEnvironmentWorkerJob.re b/src/job/worker/main/init/DetectEnvironmentMainWorkerJob.re similarity index 100% rename from src/job/worker/main/init/DetectEnvironmentWorkerJob.re rename to src/job/worker/main/init/DetectEnvironmentMainWorkerJob.re diff --git a/src/service/state/main/browserDetect/BrowserDetectMainService.re b/src/service/state/main/browserDetect/BrowserDetectMainService.re index 07fc36a301..cbaf54b327 100755 --- a/src/service/state/main/browserDetect/BrowserDetectMainService.re +++ b/src/service/state/main/browserDetect/BrowserDetectMainService.re @@ -60,7 +60,7 @@ let detectMobileNotSupportWorker = state => isMobile(state) ? WonderLog.Log.fatal( WonderLog.Log.buildFatalMessage( - ~title="DetectEnvironmentWorkerJob->execJob", + ~title="DetectEnvironmentMainWorkerJob->execJob", ~description={j|mobile not support worker|j}, ~reason="", ~solution={j||j}, diff --git a/src/service/state/main/job/worker/OperateMainInitWorkerJobMainService.re b/src/service/state/main/job/worker/OperateMainInitWorkerJobMainService.re index 36f025cc58..bd80257c5a 100755 --- a/src/service/state/main/job/worker/OperateMainInitWorkerJobMainService.re +++ b/src/service/state/main/job/worker/OperateMainInitWorkerJobMainService.re @@ -11,58 +11,57 @@ let _buildStreamArr = pipelineJobs: array(mainInitPipelineJob), pipelineSubJobs: array(mainInitPipelineSubJob), stateData, - jobs + jobs, ), findFunc, getJobHandleFunc, - state + state, ) => pipelineSubJobs - |> Js.Array.filter( - ({name}: mainInitPipelineSubJob) => - ! ( - state.jobRecord.workerCustomMainInitRemovedDefaultJobMap - |> WonderCommonlib.MutableHashMapService.has(name) - ) + |> Js.Array.filter(({name}: mainInitPipelineSubJob) => + !( + state.jobRecord.workerCustomMainInitRemovedDefaultJobMap + |> WonderCommonlib.MutableHashMapService.has(name) + ) ) |> WonderCommonlib.ArrayService.reduceOneParam( - [@bs] - ( - (streamArr, {name: subJobName}: mainInitPipelineSubJob) => - switch ( - JobConfigService.findFirst( - pipelineJobs, + (. streamArr, {name: subJobName}: mainInitPipelineSubJob) => + switch ( + JobConfigService.findFirst(pipelineJobs, subJobName, ({name}) => + JobConfigService.filterTargetName(name, subJobName) + ) + ) { + | None => + let {flags} = + OperateMainWorkerJobMainService.getExecutableJob( + jobs, subJobName, - ({name}) => JobConfigService.filterTargetName(name, subJobName) - ) - ) { - | None => - let {flags} = OperateMainWorkerJobMainService.getExecutableJob(jobs, subJobName); - let handleFunc = getJobHandleFunc(subJobName, jobHandleMap); - streamArr - |> ArrayService.push(handleFunc(flags, stateData)) - |> OperateMainWorkerJobMainService.addCustomJobHandleToStreamArr( - subJobName, - state.jobRecord.workerCustomMainInitTargetJobMap, - stateData - ) - | Some(jobRecord) => - streamArr - |> ArrayService.push( - findFunc( - (jobRecord, pipelineJobs, jobHandleMap, jobs, stateData), - getJobHandleFunc, - state - ) - ) - |> OperateMainWorkerJobMainService.addCustomJobHandleToStreamArr( - subJobName, - state.jobRecord.workerCustomMainInitTargetJobMap, - stateData - ) - } - ), - [||] + ); + let handleFunc = getJobHandleFunc(subJobName, jobHandleMap); + streamArr + |> OperateMainWorkerJobMainService.addCustomJobHandleToStreamArr( + subJobName, + [|handleFunc(flags, stateData)|], + state.jobRecord.workerCustomMainInitTargetJobMap, + stateData, + ); + | Some(jobRecord) => + streamArr + |> ArrayService.push( + findFunc( + (jobRecord, pipelineJobs, jobHandleMap, jobs, stateData), + getJobHandleFunc, + state, + ), + ) + |> OperateMainWorkerJobMainService.addCustomJobHandleToStreamArr( + subJobName, + [||], + state.jobRecord.workerCustomMainInitTargetJobMap, + stateData, + ) + }, + [||], ); let rec _find = @@ -72,18 +71,18 @@ let rec _find = pipelineJobs, jobHandleMap, mainInitJobs, - stateData + stateData, ), getJobHandleFunc, - state + state, ) => - switch link { + switch (link) { | "merge" => _buildStreamArr( (jobHandleMap, pipelineJobs, pipelineSubJobs, stateData, mainInitJobs), _find, getJobHandleFunc, - state + state, ) |> WonderBsMost.Most.mergeArray | "concat" => @@ -91,7 +90,7 @@ let rec _find = (jobHandleMap, pipelineJobs, pipelineSubJobs, stateData, mainInitJobs), _find, getJobHandleFunc, - state + state, ) |> MostUtils.concatArray | _ => @@ -101,53 +100,55 @@ let rec _find = ~description={j|invalid link: $link|j}, ~reason="", ~solution={j||j}, - ~params={j||j} - ) + ~params={j||j}, + ), ) }; let _getFrameJobName = () => "frame"; -let _findFrameJob = (jobs) => { +let _findFrameJob = jobs => { WonderLog.Contract.requireCheck( () => WonderLog.( Contract.( Operators.( test( - Log.buildAssertMessage(~expect={j|frame job only has one|j}, ~actual={j|not|j}), + Log.buildAssertMessage( + ~expect={j|frame job only has one|j}, + ~actual={j|not|j}, + ), () => - jobs - |> Js.Array.filter( - ({name}: mainInitPipelineJob) => - JobConfigService.filterTargetName(name, _getFrameJobName()) - ) - |> Js.Array.length == 1 + jobs + |> Js.Array.filter(({name}: mainInitPipelineJob) => + JobConfigService.filterTargetName(name, _getFrameJobName()) + ) + |> Js.Array.length == 1 ) ) ) ), - IsDebugMainService.getIsDebug(StateDataMain.stateData) + IsDebugMainService.getIsDebug(StateDataMain.stateData), ); let jobName = _getFrameJobName(); JobConfigService.unsafeFindFirst( - jobs, - jobName, - ({name}: mainInitPipelineJob) => JobConfigService.filterTargetName(name, jobName) - ) + jobs, jobName, ({name}: mainInitPipelineJob) => + JobConfigService.filterTargetName(name, jobName) + ); }; -let getMainInitJobStream = (jobHandleMap, stateData, getJobHandleFunc, {workerJobRecord} as state) => { - let {setting, mainInitPipelines, mainInitJobs} = workerJobRecord |> OptionService.unsafeGet; +let getMainInitJobStream = + (jobHandleMap, stateData, getJobHandleFunc, {workerJobRecord} as state) => { + let {setting, mainInitPipelines, mainInitJobs} = + workerJobRecord |> OptionService.unsafeGet; let {jobs}: mainInitPipeline = JobConfigService.unsafeFindFirst( - mainInitPipelines, - setting.mainInitPipeline, - ({name}) => JobConfigService.filterTargetName(name, setting.mainInitPipeline) + mainInitPipelines, setting.mainInitPipeline, ({name}) => + JobConfigService.filterTargetName(name, setting.mainInitPipeline) ); _find( (_findFrameJob(jobs), jobs, jobHandleMap, mainInitJobs, stateData), getJobHandleFunc, - state - ) + state, + ); }; \ No newline at end of file diff --git a/src/service/state/main/job/worker/OperateMainLoopWorkerJobMainService.re b/src/service/state/main/job/worker/OperateMainLoopWorkerJobMainService.re index 849789f6fd..4634ef0f42 100755 --- a/src/service/state/main/job/worker/OperateMainLoopWorkerJobMainService.re +++ b/src/service/state/main/job/worker/OperateMainLoopWorkerJobMainService.re @@ -11,58 +11,57 @@ let _buildStreamArr = pipelineJobs: array(mainLoopPipelineJob), pipelineSubJobs: array(mainLoopPipelineSubJob), stateData, - jobs + jobs, ), findFunc, getJobHandleFunc, - state + state, ) => pipelineSubJobs - |> Js.Array.filter( - ({name}: mainLoopPipelineSubJob) => - ! ( - state.jobRecord.workerCustomMainLoopRemovedDefaultJobMap - |> WonderCommonlib.MutableHashMapService.has(name) - ) + |> Js.Array.filter(({name}: mainLoopPipelineSubJob) => + !( + state.jobRecord.workerCustomMainLoopRemovedDefaultJobMap + |> WonderCommonlib.MutableHashMapService.has(name) + ) ) |> WonderCommonlib.ArrayService.reduceOneParam( - [@bs] - ( - (streamArr, {name: subJobName}: mainLoopPipelineSubJob) => - switch ( - JobConfigService.findFirst( - pipelineJobs, + (. streamArr, {name: subJobName}: mainLoopPipelineSubJob) => + switch ( + JobConfigService.findFirst(pipelineJobs, subJobName, ({name}) => + JobConfigService.filterTargetName(name, subJobName) + ) + ) { + | None => + let {flags} = + OperateMainWorkerJobMainService.getExecutableJob( + jobs, subJobName, - ({name}) => JobConfigService.filterTargetName(name, subJobName) - ) - ) { - | None => - let {flags} = OperateMainWorkerJobMainService.getExecutableJob(jobs, subJobName); - let handleFunc = getJobHandleFunc(subJobName, jobHandleMap); - streamArr - |> ArrayService.push(handleFunc(flags, stateData)) - |> OperateMainWorkerJobMainService.addCustomJobHandleToStreamArr( - subJobName, - state.jobRecord.workerCustomMainLoopTargetJobMap, - stateData - ) - | Some(jobRecord) => - streamArr - |> ArrayService.push( - findFunc( - (jobRecord, pipelineJobs, jobHandleMap, jobs, stateData), - getJobHandleFunc, - state - ) - ) - |> OperateMainWorkerJobMainService.addCustomJobHandleToStreamArr( - subJobName, - state.jobRecord.workerCustomMainLoopTargetJobMap, - stateData - ) - } - ), - [||] + ); + let handleFunc = getJobHandleFunc(subJobName, jobHandleMap); + streamArr + |> OperateMainWorkerJobMainService.addCustomJobHandleToStreamArr( + subJobName, + [|handleFunc(flags, stateData)|], + state.jobRecord.workerCustomMainLoopTargetJobMap, + stateData, + ); + | Some(jobRecord) => + streamArr + |> ArrayService.push( + findFunc( + (jobRecord, pipelineJobs, jobHandleMap, jobs, stateData), + getJobHandleFunc, + state, + ), + ) + |> OperateMainWorkerJobMainService.addCustomJobHandleToStreamArr( + subJobName, + [||], + state.jobRecord.workerCustomMainLoopTargetJobMap, + stateData, + ) + }, + [||], ); let rec _find = @@ -72,18 +71,18 @@ let rec _find = pipelineJobs, jobHandleMap, mainLoopJobs, - stateData + stateData, ), getJobHandleFunc, - state + state, ) => - switch link { + switch (link) { | "merge" => _buildStreamArr( (jobHandleMap, pipelineJobs, pipelineSubJobs, stateData, mainLoopJobs), _find, getJobHandleFunc, - state + state, ) |> WonderBsMost.Most.mergeArray | "concat" => @@ -91,7 +90,7 @@ let rec _find = (jobHandleMap, pipelineJobs, pipelineSubJobs, stateData, mainLoopJobs), _find, getJobHandleFunc, - state + state, ) |> MostUtils.concatArray | _ => @@ -101,53 +100,55 @@ let rec _find = ~description={j|invalid link: $link|j}, ~reason="", ~solution={j||j}, - ~params={j||j} - ) + ~params={j||j}, + ), ) }; let _getFrameJobName = () => "frame"; -let _findFrameJob = (jobs) => { +let _findFrameJob = jobs => { WonderLog.Contract.requireCheck( () => WonderLog.( Contract.( Operators.( test( - Log.buildAssertMessage(~expect={j|frame job only has one|j}, ~actual={j|not|j}), + Log.buildAssertMessage( + ~expect={j|frame job only has one|j}, + ~actual={j|not|j}, + ), () => - jobs - |> Js.Array.filter( - ({name}: mainLoopPipelineJob) => - JobConfigService.filterTargetName(name, _getFrameJobName()) - ) - |> Js.Array.length == 1 + jobs + |> Js.Array.filter(({name}: mainLoopPipelineJob) => + JobConfigService.filterTargetName(name, _getFrameJobName()) + ) + |> Js.Array.length == 1 ) ) ) ), - IsDebugMainService.getIsDebug(StateDataMain.stateData) + IsDebugMainService.getIsDebug(StateDataMain.stateData), ); let jobName = _getFrameJobName(); JobConfigService.unsafeFindFirst( - jobs, - jobName, - ({name}: mainLoopPipelineJob) => JobConfigService.filterTargetName(name, jobName) - ) + jobs, jobName, ({name}: mainLoopPipelineJob) => + JobConfigService.filterTargetName(name, jobName) + ); }; -let getMainLoopJobStream = (jobHandleMap, stateData, getJobHandleFunc, {workerJobRecord} as state) => { - let {setting, mainLoopPipelines, mainLoopJobs} = workerJobRecord |> OptionService.unsafeGet; +let getMainLoopJobStream = + (jobHandleMap, stateData, getJobHandleFunc, {workerJobRecord} as state) => { + let {setting, mainLoopPipelines, mainLoopJobs} = + workerJobRecord |> OptionService.unsafeGet; let {jobs}: mainLoopPipeline = JobConfigService.unsafeFindFirst( - mainLoopPipelines, - setting.mainLoopPipeline, - ({name}) => JobConfigService.filterTargetName(name, setting.mainLoopPipeline) + mainLoopPipelines, setting.mainLoopPipeline, ({name}) => + JobConfigService.filterTargetName(name, setting.mainLoopPipeline) ); _find( (_findFrameJob(jobs), jobs, jobHandleMap, mainLoopJobs, stateData), getJobHandleFunc, - state - ) + state, + ); }; \ No newline at end of file diff --git a/src/service/state/main/job/worker/OperateMainWorkerJobMainService.re b/src/service/state/main/job/worker/OperateMainWorkerJobMainService.re index 866be3c6b6..09c6e9b32f 100755 --- a/src/service/state/main/job/worker/OperateMainWorkerJobMainService.re +++ b/src/service/state/main/job/worker/OperateMainWorkerJobMainService.re @@ -7,47 +7,59 @@ let getExecutableJob = (jobs, jobItemName) => JobConfigService.filterTargetName(jobName, jobItemName) ); -let _addHandleFuncToList = (action, handleFunc, handleList) => +let _addHandleFuncStream = (action, handleFuncStream, handleFuncStreamArr) => switch (action) { - | BEFORE => [handleFunc, ...handleList] - | AFTER => handleList @ [handleFunc] + | BEFORE => + handleFuncStreamArr |> Js.Array.unshift(handleFuncStream) |> ignore; + handleFuncStreamArr; + | AFTER => handleFuncStreamArr |> ArrayService.push(handleFuncStream) }; -let rec _findAllCustomJobHandles = - (subJobName, workerCustomMainLoopTargetJobMap, handleList) => +let _buildStream = (stateData, customHandle) => + MostUtils.callFunc(() => { + customHandle(stateData); + None; + }); + +let rec _addSourceJobAllCustomJobHandleStreams = + ( + subJobName, + workerCustomMainLoopTargetJobMap, + stateData, + handleFuncStreamArr, + ) => switch ( workerCustomMainLoopTargetJobMap |> WonderCommonlib.MutableHashMapService.get(subJobName) ) { - | None => handleList + | None => handleFuncStreamArr | Some((customJobName, action, handleFunc)) => - _addHandleFuncToList(action, handleFunc, handleList) - |> _findAllCustomJobHandles( + _addHandleFuncStream( + action, + handleFunc |> _buildStream(stateData), + handleFuncStreamArr, + ) + |> _addSourceJobAllCustomJobHandleStreams( customJobName, workerCustomMainLoopTargetJobMap, + stateData, ) }; -let _buildCustomStreamArr = (customJobHandleList, stateData) => - customJobHandleList - |> List.fold_left( - (streamArr, customHandle) => - streamArr - |> ArrayService.push( - MostUtils.callFunc(() => { - customHandle(stateData); - None; - }), - ), - [||], - ); - let addCustomJobHandleToStreamArr = - (subJobName, workerCustomMainTargetJobMap, stateData, streamArr) => - switch ( - _findAllCustomJobHandles(subJobName, workerCustomMainTargetJobMap, []) - ) { - | list when list |> List.length === 0 => streamArr - | list => - streamArr |> Js.Array.concat(_buildCustomStreamArr(list, stateData)) - }; \ No newline at end of file + ( + subJobName, + handleFuncStreamArr, + workerCustomMainTargetJobMap, + stateData, + streamArr, + ) => + ArrayService.fastConcat( + streamArr, + _addSourceJobAllCustomJobHandleStreams( + subJobName, + workerCustomMainTargetJobMap, + stateData, + handleFuncStreamArr, + ), + ); \ No newline at end of file diff --git a/test/integration/worker/job/main_worker/tool/MainInitJobMainWorkerTool.re b/test/integration/worker/job/main_worker/tool/MainInitJobMainWorkerTool.re index 802013aca7..f57ebbed50 100755 --- a/test/integration/worker/job/main_worker/tool/MainInitJobMainWorkerTool.re +++ b/test/integration/worker/job/main_worker/tool/MainInitJobMainWorkerTool.re @@ -3,7 +3,11 @@ open StateDataMainType; let prepare = () => WorkerWorkerTool.setFakeWorkersAndSetState(MainStateTool.unsafeGetState()); -let test = (sandbox, getWorkerFunc, judgeFunc, state) => { +let createMainInitJobHandleMap = mainInitJobHandles => + HandleJobService.createJobHandleMap(mainInitJobHandles); + +let testWithJobHandleMap = + (sandbox, jobHandleMap, getWorkerFunc, judgeFunc, state) => { open Js.Promise; let worker = getWorkerFunc(state); let postMessageToWorker = WorkerWorkerTool.stubPostMessage(sandbox, worker); @@ -11,10 +15,7 @@ let test = (sandbox, getWorkerFunc, judgeFunc, state) => { MainStateTool.unsafeGetState() |> WorkerJobWorkerTool.getMainInitJobStream( MainStateTool.getStateData(), - ( - WorkerJobHandleSystem.createMainInitJobHandleMap, - WorkerJobHandleSystem.getMainInitJobHandle, - ), + (() => jobHandleMap, WorkerJobHandleSystem.getMainInitJobHandle), ) |> WonderBsMost.Most.forEach(data => switch (data) { @@ -44,4 +45,13 @@ let test = (sandbox, getWorkerFunc, judgeFunc, state) => { } ) |> then_(() => judgeFunc(postMessageToWorker) |> resolve); -}; \ No newline at end of file +}; + +let test = (sandbox, getWorkerFunc, judgeFunc, state) => + testWithJobHandleMap( + sandbox, + WorkerJobHandleSystem.createMainInitJobHandleMap(), + getWorkerFunc, + judgeFunc, + state, + ); \ No newline at end of file diff --git a/test/integration/worker/job/render_worker/tool/RenderJobsRenderWorkerTool.re b/test/integration/worker/job/render_worker/tool/RenderJobsRenderWorkerTool.re index 28886eb239..ce45b07ed3 100755 --- a/test/integration/worker/job/render_worker/tool/RenderJobsRenderWorkerTool.re +++ b/test/integration/worker/job/render_worker/tool/RenderJobsRenderWorkerTool.re @@ -110,7 +110,7 @@ let init = (completeFunc, state) => { ); }; -let execMainLoopJobs = (sandbox, completeFunc) => { +let execMainLoopJobsWithJobHandleMap = (sandbox, jobHandleMap, completeFunc) => { let state = MainInitJobMainWorkerTool.prepare(); let renderWorker = WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state); @@ -119,15 +119,22 @@ let execMainLoopJobs = (sandbox, completeFunc) => { state |> WorkerJobWorkerTool.getMainLoopJobStream( MainStateTool.getStateData(), - ( - WorkerJobHandleSystem.createMainLoopJobHandleMap, - WorkerJobHandleSystem.getMainLoopJobHandle, - ), + (() => jobHandleMap, WorkerJobHandleSystem.getMainLoopJobHandle), ) |> WonderBsMost.Most.drain |> then_(() => completeFunc(postMessageToRenderWorker)); }; +let execMainLoopJobs = (sandbox, completeFunc) => + execMainLoopJobsWithJobHandleMap( + sandbox, + WorkerJobHandleSystem.createMainLoopJobHandleMap(), + completeFunc, + ); + +let createMainLoopJobHandleMap = mainLoopJobHandles => + HandleJobService.createJobHandleMap(mainLoopJobHandles); + let render = (sandbox, postMessageToRenderWorker, completeFunc) => { let state = MainStateTool.unsafeGetState(); let (state, renderData) = diff --git a/test/unit/job/worker/OperateCustomMainInitWorkerJob_test.re b/test/unit/job/worker/OperateCustomMainInitWorkerJob_test.re index b4aa1a7fff..eecf0e28e3 100755 --- a/test/unit/job/worker/OperateCustomMainInitWorkerJob_test.re +++ b/test/unit/job/worker/OperateCustomMainInitWorkerJob_test.re @@ -5,238 +5,271 @@ open Js.Promise; open JobType; let _ = - describe( - "operate custom worker job", - () => { - open Expect; - open Expect.Operators; - open Sinon; - let sandbox = getSandboxDefaultVal(); - let state = ref(MainStateTool.createState()); - beforeEach( - () => { - sandbox := createSandbox(); - SettingWorkerTool.buildFakeCanvasForNotPassCanvasId(sandbox); - state := TestMainWorkerTool.initWithJobConfig(~sandbox, ()) - } - ); - afterEach(() => restoreSandbox(refJsObjToSandbox(sandbox^))); - describe( - "test operate custom job", - () => - describe( - "test operate main init job", - () => { - describe( - "addWorkerMainInitJob", - () => - describe( - "add job to main init pipeline", - () => { - describe( - "test add job after target job", - () => { - testPromise( - "test add job to group job", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.addWorkerMainInitJob( - ("customJob1", "transfer_job_data"), - AFTER, - (stateData) => { - let state = StateDataMainService.unsafeGetState(stateData); - customData |> ArrayService.push(1) |> ignore - } - ); - MainInitJobMainWorkerTool.prepare() - |> MainInitJobMainWorkerTool.test( - sandbox, - (state) => - WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state), - (postMessageToRenderWorker) => customData |> expect == [|1|] - ) - } - ); - testPromise( - "test add job to concat job and merge job", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.addWorkerMainInitJob( - ("customJob1", "transfer_job_data"), - AFTER, - (stateData) => { - let state = StateDataMainService.unsafeGetState(stateData); - customData |> ArrayService.push(1) |> ignore - } - ) - |> JobAPI.addWorkerMainInitJob( - ("customJob2", "send_job_data"), - AFTER, - (stateData) => customData |> ArrayService.push(2) |> ignore - ); - MainInitJobMainWorkerTool.prepare() - |> MainInitJobMainWorkerTool.test( - sandbox, - (state) => - WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state), - (postMessageToRenderWorker) => customData |> expect == [|2, 1|] - ) - } - ); - testPromise( - "test add two job", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.addWorkerMainInitJob( - ("customJob1", "send_init_render_data"), - AFTER, - (stateData) => { - let state = StateDataMainService.unsafeGetState(stateData); - customData |> ArrayService.push(1) |> ignore - } - ) - |> JobAPI.addWorkerMainInitJob( - ("customJob2", "customJob1"), - AFTER, - (stateData) => customData |> ArrayService.push(2) |> ignore - ); - MainInitJobMainWorkerTool.prepare() - |> MainInitJobMainWorkerTool.test( - sandbox, - (state) => - WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state), - (postMessageToRenderWorker) => customData |> expect == [|1, 2|] - ) - } - ) - } - ); - testPromise( - "test add job to head", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.addWorkerMainInitJob( - ("customJob1", "transfer_job_data"), - BEFORE, - (stateData) => { - let state = StateDataMainService.unsafeGetState(stateData); - customData |> ArrayService.push(1) |> ignore - } - ) - |> JobAPI.addWorkerMainInitJob( - ("customJob2", "customJob1"), - BEFORE, - (stateData) => customData |> ArrayService.push(2) |> ignore - ); - MainInitJobMainWorkerTool.prepare() - |> MainInitJobMainWorkerTool.test( - sandbox, - (state) => - WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state), - (postMessageToRenderWorker) => customData |> expect == [|2, 1|] - ) - } - ) - } - ) - ); - describe( - "removeWorkerMainInitJob", - () => { - testPromise( - "test remove custom added job", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.addWorkerMainInitJob( - ("customJob", "transfer_job_data"), - AFTER, - (stateData) => { - let state = StateDataMainService.unsafeGetState(stateData); - customData |> ArrayService.push(1) |> ignore; + describe("operate custom worker job", () => { + open Expect; + open Expect.Operators; + open Sinon; + let sandbox = getSandboxDefaultVal(); + let state = ref(MainStateTool.createState()); + beforeEach(() => { + sandbox := createSandbox(); + SettingWorkerTool.buildFakeCanvasForNotPassCanvasId(sandbox); + state := TestMainWorkerTool.initWithJobConfig(~sandbox, ()); + }); + afterEach(() => restoreSandbox(refJsObjToSandbox(sandbox^))); + describe("test operate custom job", () => + describe("test operate main init job", () => { + describe("addWorkerMainInitJob", () => + describe("add job to main init pipeline", () => { + describe("test add job after target job", () => { + testPromise("test add job to group job", () => { + let customData = [||]; + let state = + state^ + |> JobAPI.addWorkerMainInitJob( + ("customJob1", "transfer_job_data"), + AFTER, + stateData => { + let state = + StateDataMainService.unsafeGetState(stateData); + customData |> ArrayService.push(1) |> ignore; + }, + ); + MainInitJobMainWorkerTool.prepare() + |> MainInitJobMainWorkerTool.test( + sandbox, + state => + WorkerInstanceMainWorkerTool.unsafeGetRenderWorker( + state, + ), + postMessageToRenderWorker => + customData |> expect == [|1|], + ); + }); + testPromise("test add job to concat job and merge job", () => { + let customData = [||]; + let state = + state^ + |> JobAPI.addWorkerMainInitJob( + ("customJob1", "transfer_job_data"), + AFTER, + stateData => { + let state = + StateDataMainService.unsafeGetState(stateData); + customData |> ArrayService.push(1) |> ignore; + }, + ) + |> JobAPI.addWorkerMainInitJob( + ("customJob2", "send_job_data"), AFTER, stateData => + customData |> ArrayService.push(2) |> ignore + ); + MainInitJobMainWorkerTool.prepare() + |> MainInitJobMainWorkerTool.test( + sandbox, + state => + WorkerInstanceMainWorkerTool.unsafeGetRenderWorker( + state, + ), + postMessageToRenderWorker => + customData |> expect == [|2, 1|], + ); + }); + testPromise("test add two job", () => { + let customData = [||]; + let state = + state^ + |> JobAPI.addWorkerMainInitJob( + ("customJob1", "send_init_render_data"), + AFTER, + stateData => { + let state = + StateDataMainService.unsafeGetState(stateData); + customData |> ArrayService.push(1) |> ignore; + }, + ) + |> JobAPI.addWorkerMainInitJob( + ("customJob2", "customJob1"), AFTER, stateData => + customData |> ArrayService.push(2) |> ignore + ); + MainInitJobMainWorkerTool.prepare() + |> MainInitJobMainWorkerTool.test( + sandbox, + state => + WorkerInstanceMainWorkerTool.unsafeGetRenderWorker( + state, + ), + postMessageToRenderWorker => + customData |> expect == [|1, 2|], + ); + }); + }); - () - } - ) - |> JobAPI.removeWorkerMainInitJob("customJob"); - MainInitJobMainWorkerTool.prepare() - |> MainInitJobMainWorkerTool.test( - sandbox, - (state) => WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state), - (postMessageToRenderWorker) => customData |> expect == [||] - ) - } - ); - describe( - "test remove default job", - () => { - testPromise( - "test remove group job", - () => { - let customData = [||]; - let state = - state^ |> JobAPI.removeWorkerMainInitJob("transfer_job_data"); - MainInitJobMainWorkerTool.prepare() - |> MainInitJobMainWorkerTool.test( - sandbox, - (state) => - WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state), - (postMessageToRenderWorker) => - postMessageToRenderWorker - |> expect - |> not_ - |> toCalledWith([| - { - "operateType": "SEND_JOB_DATA", - "pipelineJobs": Sinon.matchAny, - "jobs": Sinon.matchAny - } - |]) - ) - } - ); - testPromise( - "test remove atom job", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.removeWorkerMainInitJob("send_job_data") - |> JobAPI.removeWorkerMainInitJob("get_finish_send_job_data"); - MainInitJobMainWorkerTool.prepare() - |> MainInitJobMainWorkerTool.test( - sandbox, - (state) => - WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state), - (postMessageToRenderWorker) => - postMessageToRenderWorker - |> expect - |> not_ - |> toCalledWith([| - { - "operateType": "SEND_JOB_DATA", - "pipelineJobs": Sinon.matchAny, - "jobs": Sinon.matchAny - } - |]) - ) - } - ) - } - ) - } - ) + testPromise("test add job to head", () => { + state := + TestMainWorkerTool.initWithJobConfig( + ~sandbox, + ~workerJobRecord= + WorkerJobTool.buildWorkerJobConfig( + ~mainInitPipelines= + {| +[ + { + "name": "default", + "jobs": [ + { + "name": "begin_init", + "link": "merge", + "jobs": [ + { + "name": "init" + } + ] + }, + { + "name": "init", + "link": "concat", + "jobs": [ + { + "name": "init_script" } - ) - ) + ] + }, + { + "name": "frame", + "link": "concat", + "jobs": [ + { + "name": "begin_init" + } + ] + } + ] } - ); \ No newline at end of file + ] + |}, + (), + ), + (), + ); + + let customData = [||]; + let state = + state^ + |> JobAPI.addWorkerMainInitJob( + ("customJob1", "init_script"), + BEFORE, + stateData => { + let state = + StateDataMainService.unsafeGetState(stateData); + customData |> ArrayService.push(1) |> ignore; + }, + ) + |> JobAPI.addWorkerMainInitJob( + ("customJob2", "customJob1"), BEFORE, stateData => + customData |> ArrayService.push(2) |> ignore + ); + MainInitJobMainWorkerTool.prepare() + |> MainInitJobMainWorkerTool.testWithJobHandleMap( + sandbox, + MainInitJobMainWorkerTool.createMainInitJobHandleMap([ + ( + "init_script", + (flags, stateData) => + MostUtils.callFunc(() => { + customData |> ArrayService.push(10) |> ignore; + + None; + }), + ), + ]), + state => + WorkerInstanceMainWorkerTool.unsafeGetRenderWorker( + state, + ), + postMessageToRenderWorker => + customData |> expect == [|2, 1, 10|], + ); + }); + }) + ); + describe("removeWorkerMainInitJob", () => { + testPromise("test remove custom added job", () => { + let customData = [||]; + let state = + state^ + |> JobAPI.addWorkerMainInitJob( + ("customJob", "transfer_job_data"), + AFTER, + stateData => { + let state = + StateDataMainService.unsafeGetState(stateData); + customData |> ArrayService.push(1) |> ignore; + + (); + }, + ) + |> JobAPI.removeWorkerMainInitJob("customJob"); + MainInitJobMainWorkerTool.prepare() + |> MainInitJobMainWorkerTool.test( + sandbox, + state => + WorkerInstanceMainWorkerTool.unsafeGetRenderWorker(state), + postMessageToRenderWorker => customData |> expect == [||], + ); + }); + describe("test remove default job", () => { + testPromise("test remove group job", () => { + let customData = [||]; + let state = + state^ |> JobAPI.removeWorkerMainInitJob("transfer_job_data"); + MainInitJobMainWorkerTool.prepare() + |> MainInitJobMainWorkerTool.test( + sandbox, + state => + WorkerInstanceMainWorkerTool.unsafeGetRenderWorker( + state, + ), + postMessageToRenderWorker => + postMessageToRenderWorker + |> expect + |> not_ + |> toCalledWith([| + { + "operateType": "SEND_JOB_DATA", + "pipelineJobs": Sinon.matchAny, + "jobs": Sinon.matchAny, + }, + |]), + ); + }); + testPromise("test remove atom job", () => { + let customData = [||]; + let state = + state^ + |> JobAPI.removeWorkerMainInitJob("send_job_data") + |> JobAPI.removeWorkerMainInitJob("get_finish_send_job_data"); + MainInitJobMainWorkerTool.prepare() + |> MainInitJobMainWorkerTool.test( + sandbox, + state => + WorkerInstanceMainWorkerTool.unsafeGetRenderWorker( + state, + ), + postMessageToRenderWorker => + postMessageToRenderWorker + |> expect + |> not_ + |> toCalledWith([| + { + "operateType": "SEND_JOB_DATA", + "pipelineJobs": Sinon.matchAny, + "jobs": Sinon.matchAny, + }, + |]), + ); + }); + }); + }); + }) + ); + }); \ No newline at end of file diff --git a/test/unit/job/worker/OperateCustomMainLoopWorkerJob_test.re b/test/unit/job/worker/OperateCustomMainLoopWorkerJob_test.re index 5636eb8c75..84661dc4e1 100755 --- a/test/unit/job/worker/OperateCustomMainLoopWorkerJob_test.re +++ b/test/unit/job/worker/OperateCustomMainLoopWorkerJob_test.re @@ -5,25 +5,22 @@ open Js.Promise; open JobType; let _ = - describe( - "operate custom worker job", - () => { - open Expect; - open Expect.Operators; - open Sinon; - let sandbox = getSandboxDefaultVal(); - let state = ref(MainStateTool.createState()); - beforeEach( - () => { - sandbox := createSandbox(); - state := - TestMainWorkerTool.initWithJobConfig(~sandbox, ()) - |> WorkerJobTool.create( - WorkerJobTool.buildWorkerJobConfig( - ~mainInitPipelines= - WorkerJobTool.buildMainInitPipelinesConfigWithoutCreateWorkerInstanceAndMessage - (), - ~mainLoopPipelines={|[ + describe("operate custom worker job", () => { + open Expect; + open Expect.Operators; + open Sinon; + let sandbox = getSandboxDefaultVal(); + let state = ref(MainStateTool.createState()); + beforeEach(() => { + sandbox := createSandbox(); + state := + TestMainWorkerTool.initWithJobConfig(~sandbox, ()) + |> WorkerJobTool.create( + WorkerJobTool.buildWorkerJobConfig( + ~mainInitPipelines= + WorkerJobTool.buildMainInitPipelinesConfigWithoutCreateWorkerInstanceAndMessage(), + ~mainLoopPipelines= + {|[ { "name": "default", "jobs": [ @@ -60,111 +57,146 @@ let _ = ] } ]|}, - () - ) - ) - } - ); - afterEach(() => restoreSandbox(refJsObjToSandbox(sandbox^))); - describe( - "test operate custom job", - () => - describe( - "test operate main loop job", - () => { - describe( - "addWorkerMainLoopJob", - () => - describe( - "add job to main loop pipeline", - () => { - describe( - "test add job after target job", - () => - testPromise( - "test add job to group job", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.addWorkerMainLoopJob( - ("customJob1", "copy_arraybuffer"), - AFTER, - (stateData) => { - let state = StateDataMainService.unsafeGetState(stateData); - customData |> ArrayService.push(1) |> ignore - } - ); - MainStateTool.setState(state) |> ignore; - RenderJobsRenderWorkerTool.execMainLoopJobs( - sandbox, - (_) => customData |> expect == [|1|] |> resolve - ) - } - ) - ); - testPromise( - "test add job to head", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.addWorkerMainLoopJob( - ("customJob1", "copy_arraybuffer"), - BEFORE, - (stateData) => { - let state = StateDataMainService.unsafeGetState(stateData); - customData |> ArrayService.push(1) |> ignore - } - ) - |> JobAPI.addWorkerMainLoopJob( - ("customJob2", "customJob1"), - BEFORE, - (stateData) => customData |> ArrayService.push(2) |> ignore - ); - MainStateTool.setState(state) |> ignore; - RenderJobsRenderWorkerTool.execMainLoopJobs( - sandbox, - (_) => customData |> expect == [|2, 1|] |> resolve - ) - } - ) + (), + ), + ); + }); + afterEach(() => restoreSandbox(refJsObjToSandbox(sandbox^))); + describe("test operate custom job", () => + describe("test operate main loop job", () => { + describe("addWorkerMainLoopJob", () => + describe("add job to main loop pipeline", () => { + describe("test add job after target job", () => + testPromise("test add job to group job", () => { + let customData = [||]; + let state = + state^ + |> JobAPI.addWorkerMainLoopJob( + ("customJob1", "copy_arraybuffer"), + AFTER, + stateData => { + let state = + StateDataMainService.unsafeGetState(stateData); + customData |> ArrayService.push(1) |> ignore; + }, + ); + MainStateTool.setState(state) |> ignore; + RenderJobsRenderWorkerTool.execMainLoopJobs(sandbox, _ => + customData |> expect == [|1|] |> resolve + ); + }) + ); + + testPromise("test add job to head", () => { + state := + TestMainWorkerTool.initWithJobConfig( + ~sandbox, + ~workerJobRecord= + WorkerJobTool.buildWorkerJobConfig( + ~mainLoopPipelines= + {| +[ + { + "name": "default", + "jobs": [ + { + "name": "loop", + "link": "concat", + "jobs": [ + { + "name": "send_render_data" } - ) - ); - describe( - "removeWorkerMainLoopJob", - () => - testPromise( - "test remove custom added job", - () => { - let customData = [||]; - let state = - state^ - |> JobAPI.addWorkerMainLoopJob( - ("customJob", "tick"), - AFTER, - (stateData) => { - let state = StateDataMainService.unsafeGetState(stateData); - customData |> ArrayService.push(1) |> ignore - } - ) - |> JobAPI.removeWorkerMainLoopJob("customJob"); - MainStateTool.setState(state) |> ignore; - RenderJobsRenderWorkerTool.execMainLoopJobs( - sandbox, - (_) => customData |> expect == [||] |> resolve - ) + ] + }, + { + "name": "begin_loop", + "link": "merge", + "jobs": [ + { + "name": "loop" + } + ] + }, + { + "name": "frame", + "link": "concat", + "jobs": [ + { + "name": "begin_loop" } - ) - ) - /* describe( - "test remove default job", - () => { - } - ) */ + ] } - ) - ) + ] } - ); \ No newline at end of file +] + |}, + (), + ), + (), + ); + + let customData = [||]; + let state = + state^ + |> JobAPI.addWorkerMainLoopJob( + ("customJob1", "send_render_data"), + BEFORE, + stateData => { + let state = + StateDataMainService.unsafeGetState(stateData); + customData |> ArrayService.push(1) |> ignore; + }, + ) + |> JobAPI.addWorkerMainLoopJob( + ("customJob2", "customJob1"), BEFORE, stateData => + customData |> ArrayService.push(2) |> ignore + ); + MainStateTool.setState(state) |> ignore; + RenderJobsRenderWorkerTool.execMainLoopJobsWithJobHandleMap( + sandbox, + RenderJobsRenderWorkerTool.createMainLoopJobHandleMap([ + ( + "send_render_data", + (flags, stateData) => + MostUtils.callFunc(() => { + customData |> ArrayService.push(10) |> ignore; + + None; + }), + ), + ]), + _ => + customData |> expect == [|2, 1, 10|] |> resolve + ); + }); + }) + ); + describe("removeWorkerMainLoopJob", () => + testPromise("test remove custom added job", () => { + let customData = [||]; + let state = + state^ + |> JobAPI.addWorkerMainLoopJob( + ("customJob", "tick"), + AFTER, + stateData => { + let state = + StateDataMainService.unsafeGetState(stateData); + customData |> ArrayService.push(1) |> ignore; + }, + ) + |> JobAPI.removeWorkerMainLoopJob("customJob"); + MainStateTool.setState(state) |> ignore; + RenderJobsRenderWorkerTool.execMainLoopJobs(sandbox, _ => + customData |> expect == [||] |> resolve + ); + }) + ); + /* describe( + "test remove default job", + () => { + } + ) */ + }) + ); + }); \ No newline at end of file