1
1
import type { App } from 'vue'
2
2
import defu from 'defu'
3
- import { onBeforeUnmount , onMounted , ref } from 'vue'
3
+ import { onBeforeUnmount , onMounted , ref , watch } from 'vue'
4
4
5
5
declare global {
6
6
interface Window {
@@ -229,8 +229,7 @@ export function createChatWoot(options: OptionPlugin) {
229
229
230
230
export function useChatWoot ( ) {
231
231
const observer = ref < any > ( null )
232
- const start = ref ( 1 )
233
- let timer : ReturnType < typeof setTimeout >
232
+ const isReady = ref ( false )
234
233
const isModalVisible = ref ( false )
235
234
236
235
function observerStart ( data : any ) {
@@ -256,79 +255,94 @@ export function useChatWoot() {
256
255
}
257
256
}
258
257
258
+ function onReady ( ) {
259
+ const data = document . querySelector ( '.woot-widget-holder' )
260
+ isReady . value = true
261
+ observerStart ( data )
262
+ }
263
+
264
+ function waitForReady ( callback : ( ) => void ) {
265
+ if ( isReady . value ) {
266
+ callback ( )
267
+ }
268
+ else {
269
+ const stop = watch ( isReady , ( value ) => {
270
+ if ( ! value )
271
+ return
272
+ callback ( )
273
+ stop ( )
274
+ } )
275
+ }
276
+ }
277
+
259
278
onMounted ( ( ) => {
260
- timer = setInterval ( ( ) => {
261
- start . value += 1
262
- const data = document . querySelector ( '.woot-widget-holder' )
263
- if ( data || start . value > 100 ) {
264
- clearInterval ( timer )
265
- observerStart ( data )
266
- }
267
- } , 100 )
279
+ window . addEventListener ( 'chatwoot:ready' , onReady , { once : true } )
268
280
} )
269
281
270
282
onBeforeUnmount ( ( ) => {
283
+ window . removeEventListener ( 'chatwoot:ready' , onReady )
284
+
271
285
if ( observer . value )
272
286
observer . value . disconnect ( )
273
287
} )
274
288
275
289
const toggle = ( state : Parameters < Chatwoot [ 'toggle' ] > [ 0 ] ) => {
276
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . toggle ( state ) )
290
+ waitForReady ( ( ) => window . $chatwoot . toggle ( state ) )
277
291
}
278
292
279
293
const setUser = (
280
294
key : Parameters < Chatwoot [ 'setUser' ] > [ 0 ] ,
281
295
args : Parameters < Chatwoot [ 'setUser' ] > [ 1 ] ,
282
296
) => {
283
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . setUser ( key , args ) )
297
+ waitForReady ( ( ) => window . $chatwoot . setUser ( key , args ) )
284
298
}
285
299
286
300
const setCustomAttributes = (
287
301
attributes : Parameters < Chatwoot [ 'setCustomAttributes' ] > [ 0 ] ,
288
302
) => {
289
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . setCustomAttributes ( attributes ) )
303
+ waitForReady ( ( ) => window . $chatwoot . setCustomAttributes ( attributes ) )
290
304
}
291
305
292
306
const setConversationCustomAttributes = (
293
307
attributes : Parameters < Chatwoot [ 'setConversationCustomAttributes' ] > [ 0 ] ,
294
308
) => {
295
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . setConversationCustomAttributes ( attributes ) )
309
+ waitForReady ( ( ) => window . $chatwoot . setConversationCustomAttributes ( attributes ) )
296
310
}
297
311
298
312
const deleteCustomAttribute = (
299
313
attributes : Parameters < Chatwoot [ 'deleteCustomAttribute' ] > [ 0 ] ,
300
314
) => {
301
- isLoadTimer ( ) . then ( ( ) =>
315
+ waitForReady ( ( ) =>
302
316
window . $chatwoot . deleteCustomAttribute ( attributes ) ,
303
317
)
304
318
}
305
319
306
320
const setLocale = ( local : Parameters < Chatwoot [ 'setLocale' ] > [ 0 ] ) => {
307
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . setLocale ( local ) )
321
+ waitForReady ( ( ) => window . $chatwoot . setLocale ( local ) )
308
322
}
309
323
310
324
const setLabel = ( label : Parameters < Chatwoot [ 'setLabel' ] > [ 0 ] ) => {
311
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . setLabel ( label ) )
325
+ waitForReady ( ( ) => window . $chatwoot . setLabel ( label ) )
312
326
}
313
327
314
328
const removeLabel = ( label : Parameters < Chatwoot [ 'removeLabel' ] > [ 0 ] ) => {
315
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . removeLabel ( label ) )
329
+ waitForReady ( ( ) => window . $chatwoot . removeLabel ( label ) )
316
330
}
317
331
318
332
const reset = ( ) => {
319
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . reset ( ) )
333
+ waitForReady ( ( ) => window . $chatwoot . reset ( ) )
320
334
}
321
335
322
336
const toggleBubbleVisibility = (
323
337
visibility : Parameters < Chatwoot [ 'toggleBubbleVisibility' ] > [ 0 ] ,
324
338
) => {
325
- isLoadTimer ( ) . then ( ( ) => {
339
+ waitForReady ( ( ) => {
326
340
window . $chatwoot . toggleBubbleVisibility ( visibility )
327
341
} )
328
342
}
329
343
330
344
const popoutChatWindow = ( ) => {
331
- isLoadTimer ( ) . then ( ( ) => window . $chatwoot . popoutChatWindow ( ) )
345
+ waitForReady ( ( ) => window . $chatwoot . popoutChatWindow ( ) )
332
346
}
333
347
334
348
return {
0 commit comments