@@ -4,6 +4,7 @@ import React, {
44 useReducer ,
55 useEffect ,
66 useCallback ,
7+ useRef ,
78} from 'react' ;
89import { get } from 'idb-keyval' ;
910
@@ -26,6 +27,7 @@ export interface WebZjsState {
2627 activeAccount ?: number | null ;
2728 syncInProgress : boolean ;
2829 loading : boolean ;
30+ initialized : boolean ;
2931}
3032
3133type Action =
@@ -35,7 +37,8 @@ type Action =
3537 | { type : 'set-chain-height' ; payload : bigint }
3638 | { type : 'set-active-account' ; payload : number }
3739 | { type : 'set-sync-in-progress' ; payload : boolean }
38- | { type : 'set-loading' ; payload : boolean } ;
40+ | { type : 'set-loading' ; payload : boolean }
41+ | { type : 'set-initialized' ; payload : boolean } ;
3942
4043const initialState : WebZjsState = {
4144 webWallet : null ,
@@ -45,7 +48,8 @@ const initialState: WebZjsState = {
4548 chainHeight : undefined ,
4649 activeAccount : null ,
4750 syncInProgress : false ,
48- loading : true ,
51+ loading : false ,
52+ initialized : false ,
4953} ;
5054
5155function reducer ( state : WebZjsState , action : Action ) : WebZjsState {
@@ -64,6 +68,8 @@ function reducer(state: WebZjsState, action: Action): WebZjsState {
6468 return { ...state , syncInProgress : action . payload } ;
6569 case 'set-loading' :
6670 return { ...state , loading : action . payload } ;
71+ case 'set-initialized' :
72+ return { ...state , initialized : action . payload } ;
6773 default :
6874 return state ;
6975 }
@@ -72,11 +78,13 @@ function reducer(state: WebZjsState, action: Action): WebZjsState {
7278interface WebZjsContextType {
7379 state : WebZjsState ;
7480 dispatch : React . Dispatch < Action > ;
81+ initWallet : ( ) => Promise < void > ;
7582}
7683
7784const WebZjsContext = createContext < WebZjsContextType > ( {
7885 state : initialState ,
7986 dispatch : ( ) => { } ,
87+ initWallet : async ( ) => { } ,
8088} ) ;
8189
8290export function useWebZjsContext ( ) : WebZjsContextType {
@@ -85,6 +93,7 @@ export function useWebZjsContext(): WebZjsContextType {
8593
8694export const WebZjsProvider = ( { children } : { children : React . ReactNode } ) => {
8795 const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
96+ const initializingRef = useRef ( false ) ;
8897
8998 const initAll = useCallback ( async ( ) => {
9099 try {
@@ -147,16 +156,27 @@ export const WebZjsProvider = ({ children }: { children: React.ReactNode }) => {
147156 }
148157
149158 dispatch ( { type : 'set-loading' , payload : false } ) ;
159+ dispatch ( { type : 'set-initialized' , payload : true } ) ;
150160 } catch ( err ) {
151161 console . error ( 'Initialization error:' , err ) ;
152162 dispatch ( { type : 'set-error' , payload : Error ( String ( err ) ) } ) ;
153163 dispatch ( { type : 'set-loading' , payload : false } ) ;
154164 }
155165 } , [ ] ) ;
156166
157- useEffect ( ( ) => {
158- initAll ( ) . catch ( console . error ) ;
159- } , [ initAll ] ) ;
167+ // Lazy-load WASM: call this when user wants to use wallet features
168+ const initWallet = useCallback ( async ( ) => {
169+ if ( state . initialized || initializingRef . current ) {
170+ return ; // Already initialized or in progress
171+ }
172+ initializingRef . current = true ;
173+ dispatch ( { type : 'set-loading' , payload : true } ) ;
174+ try {
175+ await initAll ( ) ;
176+ } finally {
177+ initializingRef . current = false ;
178+ }
179+ } , [ state . initialized , initAll ] ) ;
160180
161181 useEffect ( ( ) => {
162182 if ( state . error ) {
@@ -166,7 +186,7 @@ export const WebZjsProvider = ({ children }: { children: React.ReactNode }) => {
166186
167187
168188 return (
169- < WebZjsContext . Provider value = { { state, dispatch } } >
189+ < WebZjsContext . Provider value = { { state, dispatch, initWallet } } >
170190 < Toaster />
171191 { children }
172192 </ WebZjsContext . Provider >
0 commit comments