@@ -3,7 +3,7 @@ import { unwrap } from 'solid-js/store';
33
44import type { MainFn } from 'worker/ImageRecognition' ;
55
6- import { getImageData , log , throttle , wait } from 'helper' ;
6+ import { getImageData , log , onec , throttle , wait } from 'helper' ;
77import { showCanvas , showColorArea , showGrayList } from 'worker/helper' ;
88import * as worker from 'worker/ImageRecognition' ;
99
@@ -23,6 +23,7 @@ export const handleImgRecognition = async (
2323 imgEle ??= await wait ( ( ) => getImgEle ( url ) , 1000 ) ;
2424 if ( ! imgEle ) return log . warn ( '获取图片元素失败' ) ;
2525 const { data, width, height } = getImageData ( imgEle ) ;
26+ initWorker ( ) ;
2627 return worker . recognitionImg (
2728 Comlink . transfer ( data , [ data . buffer ] ) ,
2829 width ,
@@ -33,12 +34,14 @@ export const handleImgRecognition = async (
3334 }
3435} ;
3536
36- const mainFn = {
37- log,
38- updatePageData : throttle ( ( ) => setState ( updatePageData ) , 1000 ) ,
39- setImg : ( url , key , val ) =>
40- Reflect . has ( store . imgMap , url ) && setState ( 'imgMap' , url , key , val ) ,
41- } satisfies MainFn ;
42- if ( isDevMode )
43- Object . assign ( mainFn , { showCanvas, showColorArea, showGrayList } ) ;
44- worker . setMainFn ( Comlink . proxy ( mainFn ) , Object . keys ( mainFn ) ) ;
37+ const initWorker = onec ( ( ) => {
38+ const mainFn = {
39+ log,
40+ updatePageData : throttle ( ( ) => setState ( updatePageData ) , 1000 ) ,
41+ setImg : ( url , key , val ) =>
42+ Reflect . has ( store . imgMap , url ) && setState ( 'imgMap' , url , key , val ) ,
43+ } satisfies MainFn ;
44+ if ( isDevMode )
45+ Object . assign ( mainFn , { showCanvas, showColorArea, showGrayList } ) ;
46+ worker . setMainFn ( Comlink . proxy ( mainFn ) , Object . keys ( mainFn ) ) ;
47+ } ) ;
0 commit comments