@@ -32,6 +32,10 @@ class Provider {
3232 constructor ( { logger, appId } ) {
3333 this . chainId = 1 ;
3434 this . events = { } ;
35+ if ( Provider . instance ) {
36+ return Provider . instance ;
37+ }
38+ Provider . instance = this ;
3539 if ( ! logger ) {
3640 logger = console ;
3741 }
@@ -47,10 +51,38 @@ class Provider {
4751 // @ts -ignore
4852 window . anyweb = this ;
4953 }
54+ const messageHandler = ( event ) => {
55+ if ( event . data &&
56+ ( 0 , common_1 . isObject ) ( event . data ) &&
57+ 'type' in event . data &&
58+ event . data . type === 'anyweb' ) {
59+ const IframeData = event . data . data ;
60+ if ( IframeData . type == 'event' &&
61+ IframeData . data == 'ready' &&
62+ IframeData . success ) {
63+ console . debug ( '[AnyWeb] SDK初始化完成' ) ;
64+ Provider . ready = true ;
65+ this . events . onReady && this . events . onReady ( ) ;
66+ window . removeEventListener ( 'message' , messageHandler ) ;
67+ }
68+ }
69+ } ;
70+ window . addEventListener ( 'message' , messageHandler ) ;
5071 ( 0 , common_1 . createIframe ) ( 'pages/index/home' )
5172 . then ( )
5273 . catch ( ( e ) => console . error ( '[AnyWeb] createIframe error' , e ) ) ;
5374 }
75+ static getInstance ( params ) {
76+ if ( ! Provider . instance ) {
77+ if ( params ) {
78+ Provider . instance = new Provider ( params ) ;
79+ }
80+ else {
81+ throw new Error ( '[AnyWeb] Provider is not initialized' ) ;
82+ }
83+ }
84+ return Provider . instance ;
85+ }
5486 /**
5587 * Deprecated: use `request` instead
5688 * @param arg
@@ -119,19 +151,37 @@ class Provider {
119151 */
120152 rawRequest ( method , params ) {
121153 return __awaiter ( this , void 0 , void 0 , function * ( ) {
154+ if ( ! Provider . ready ) {
155+ throw new Error ( "[AnyWeb] Provider is not ready, please use on('ready', callback) to listen to ready event" ) ;
156+ }
122157 switch ( method ) {
123158 case 'cfx_requestAccounts' :
124159 return this . rawRequest ( 'cfx_accounts' ) ;
125160 case 'cfx_accounts' :
126161 console . debug ( '[AnyWeb]' , { params } ) ;
127162 const scopes = params [ 0 ] . scopes ;
128- const result = ( yield ( 0 , common_1 . callIframe ) ( 'pages/dapp/auth' , {
129- appId : this . appId ,
130- params : params ? JSON . stringify ( params [ 0 ] ) : '' ,
131- chainId : this . chainId ,
132- authType : 'account' ,
133- scopes : scopes ,
134- } , this ) ) ;
163+ let result ;
164+ try {
165+ result = ( yield ( 0 , common_1 . callIframe ) ( 'pages/dapp/auth' , {
166+ appId : this . appId ,
167+ params : params ? JSON . stringify ( params [ 0 ] ) : '' ,
168+ chainId : this . chainId ,
169+ authType : 'check_auth' ,
170+ scopes : scopes ,
171+ silence : true ,
172+ } , this ) ) ;
173+ console . debug ( '[AnyWeb]' , 'silent auth result' , result ) ;
174+ }
175+ catch ( e ) {
176+ console . debug ( '[AnyWeb]' , 'need to auth' , e ) ;
177+ result = ( yield ( 0 , common_1 . callIframe ) ( 'pages/dapp/auth' , {
178+ appId : this . appId ,
179+ params : params ? JSON . stringify ( params [ 0 ] ) : '' ,
180+ chainId : this . chainId ,
181+ authType : 'account' ,
182+ scopes : scopes ,
183+ } , this ) ) ;
184+ }
135185 result . scopes = scopes ;
136186 this . events . onAccountsChanged &&
137187 this . events . onAccountsChanged ( result . address ) ;
@@ -210,21 +260,53 @@ class Provider {
210260 chainId : this . chainId ,
211261 params : params ? JSON . stringify ( params ) : '' ,
212262 authType : 'exit_accounts' ,
263+ silence : true ,
213264 } , this ) ;
214265 case 'anyweb_identify' :
215- return yield ( 0 , common_1 . callIframe ) ( 'pages/user/identify' , {
216- appId : this . appId ,
217- chainId : this . chainId ,
218- params : params ? JSON . stringify ( params ) : '' ,
219- } , this ) ;
266+ let identifyResult ;
267+ try {
268+ identifyResult = yield ( 0 , common_1 . callIframe ) ( 'pages/user/identify' , {
269+ appId : this . appId ,
270+ chainId : this . chainId ,
271+ params : params ? JSON . stringify ( params ) : '' ,
272+ authType : 'check_identify' ,
273+ silence : true ,
274+ } , this ) ;
275+ console . debug ( '[AnyWeb]' , 'Check identify result' , identifyResult ) ;
276+ }
277+ catch ( e ) {
278+ console . debug ( '[AnyWeb]' , 'need to identify' , e ) ;
279+ identifyResult = yield ( 0 , common_1 . callIframe ) ( 'pages/user/identify' , {
280+ appId : this . appId ,
281+ chainId : this . chainId ,
282+ params : params ? JSON . stringify ( params ) : '' ,
283+ authType : 'identify' ,
284+ } , this ) ;
285+ }
286+ return identifyResult ;
220287 case 'anyweb_logout' :
221288 // Logout the account of AnyWeb
222289 return yield ( 0 , common_1 . callIframe ) ( 'pages/dapp/auth' , {
223290 appId : this . appId ,
224291 chainId : this . chainId ,
225292 params : params ? JSON . stringify ( params ) : '' ,
226293 authType : 'logout' ,
294+ silence : true ,
227295 } , this ) ;
296+ case 'anyweb_loginstate' :
297+ try {
298+ return yield ( 0 , common_1 . callIframe ) ( 'pages/dapp/auth' , {
299+ appId : this . appId ,
300+ params : '' ,
301+ chainId : this . chainId ,
302+ authType : 'check_login' ,
303+ silence : true ,
304+ } , this ) ;
305+ }
306+ catch ( e ) {
307+ console . debug ( '[AnyWeb]' , 'need to login' , e ) ;
308+ return false ;
309+ }
228310 default :
229311 return 'Unsupported method' ;
230312 }
@@ -261,9 +343,13 @@ class Provider {
261343 case 'networkChanged' :
262344 this . events . onNetworkChanged = listener ;
263345 break ;
346+ case 'ready' :
347+ this . events . onReady = listener ;
348+ break ;
264349 default :
265350 break ;
266351 }
267352 }
268353}
269354exports . Provider = Provider ;
355+ Provider . ready = false ;
0 commit comments