webview.io 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +130 -129
package/dist/index.js
CHANGED
|
@@ -679,7 +679,7 @@ var WIO = /** @class */ (function () {
|
|
|
679
679
|
* Sets up the EMBEDDED side of the bridge
|
|
680
680
|
*/
|
|
681
681
|
WIO.prototype.getInjectedJavaScript = function () {
|
|
682
|
-
return "\n (function() {\n try {\n console.log('[EMBEDDED] Initializing WIO bridge...');\n \n const RESERVED_EVENTS = ['ping', 'pong', '__heartbeat', '__heartbeat_response', '__embedded_ready', '__connection_ack', '__webview_ready'];\n \n window._wio = {\n type: 'EMBEDDED',\n connected: false,\n Events: {},\n messageQueue: [],\n connectionToken: null,\n setupComplete: false,\n\n listen: function(){\n console.log('[EMBEDDED] Setting up message listeners...');\n\n // Listen to messages from React Native\n window.addEventListener('message', function( event ){\n try {\n const message = typeof event.data === 'string' ? JSON.parse( event.data ) : event.data;\n window._wio.handleMessage( message );\n }\n catch( error ){ \n console.error('[EMBEDDED] Parse error:', error); \n }\n });\n \n // Android support\n if( typeof document !== 'undefined' ){\n document.addEventListener('message', function( event ){\n try {\n const message = typeof event.data === 'string' ? JSON.parse( event.data ) : event.data;\n window._wio.handleMessage( message );\n }\n catch( error ){ \n console.error('[EMBEDDED] Parse error:', error); \n }\n });\n }\n\n this.setupComplete = true;\n console.log('[EMBEDDED] Setup complete');\n },\n \n ackId: function(){\n const\n rmin = 100000,\n rmax = 999999,\n timestamp = Date.now(),\n random = Math.floor( Math.random() * ( rmax - rmin + 1 ) + rmin );\n\n return timestamp + '_' + random;\n },\n \n fire: function( _event, payload, cid ){\n if( !this.Events[_event] && !this.Events[_event + '--@once'] ){\n console.log('[EMBEDDED] No listener for:', _event);\n return;\n }\n \n const ackFn = cid\n ? ( error, ...args ) => {\n this.emit( _event + '--' + cid + '--@ack', { error: error || false, args } );\n }\n : undefined;\n \n let listeners = [];\n if( this.Events[_event + '--@once'] ){\n _event += '--@once';\n listeners = this.Events[_event];\n delete this.Events[_event];\n }\n else listeners = this.Events[_event] || [];\n \n listeners.forEach( fn => {\n try { \n payload !== undefined ? fn( payload, ackFn ) : fn( ackFn ); \n }\n catch( error ){ \n console.error('[EMBEDDED] Listener error:', error); \n }\n });\n },\n \n emit: function( _event, payload, fn ){\n if( typeof payload === 'function' ){\n fn = payload;\n payload = undefined;\n }\n \n if( !this.connected && !RESERVED_EVENTS.includes(_event) ){\n this.messageQueue.push({ _event, payload, fn, timestamp: Date.now() });\n console.log('[EMBEDDED] Queued message:', _event);\n return;\n }\n \n try {\n let cid;\n if( typeof fn === 'function' ){\n cid = this.ackId();\n this.once( _event + '--' + cid + '--@ack', ({ error, args }) => fn( error, ...args ) );\n }\n \n const messageData = {\n _event,\n payload,\n cid,\n timestamp: Date.now(),\n token: RESERVED_EVENTS.includes(_event) ? this.connectionToken : undefined\n };\n \n if( typeof window.ReactNativeWebView !== 'undefined' ){\n window.ReactNativeWebView.postMessage( JSON.stringify( messageData ) );\n }\n else {\n console.error('[EMBEDDED] ReactNativeWebView not available');\n }\n }\n catch( error ){\n console.error('[EMBEDDED] Emit error:', error);\n typeof fn === 'function' && fn( String(error) );\n }\n },\n \n on: function( _event, fn ){\n if( !this.Events[_event] ) this.Events[_event] = [];\n this.Events[_event].push( fn );\n },\n \n once: function( _event, fn ){\n _event += '--@once';\n if( !this.Events[_event] ) this.Events[_event] = [];\n this.Events[_event].push( fn );\n },\n \n off: function( _event, fn ){\n if( fn && this.Events[_event] ){\n const index = this.Events[_event].indexOf( fn );\n if( index > -1 ){\n this.Events[_event].splice( index, 1 );\n if( this.Events[_event].length === 0 ) delete this.Events[_event];\n }\n }\n else delete this.Events[_event];\n },\n \n processMessageQueue: function(){\n if( !this.connected || this.messageQueue.length === 0 ) return;\n \n console.log('[EMBEDDED] Processing', this.messageQueue.length, 'queued messages');\n const queue = [...this.messageQueue];\n this.messageQueue = [];\n \n queue.forEach( msg => {\n try { \n this.emit( msg._event, msg.payload, msg.fn ); \n }\n catch( error ){ \n console.error('[EMBEDDED] Queue process error:', error); \n }\n });\n },\n \n handleMessage: function( data ){\n if( !data || !data._event ) return;\n \n const { _event, payload, cid, token } = data;\n \n console.log('[EMBEDDED] Received:', _event);\n \n // Handle heartbeat response\n if( _event === '__heartbeat_response' ){\n return;\n }\n \n // Handle heartbeat request\n if( _event === '__heartbeat' ){\n this.emit('__heartbeat_response', { timestamp: Date.now() });\n return;\n }\n \n // Handle webview ready signal\n if( _event === '__webview_ready' ){\n console.log('[EMBEDDED] WebView ready signal received');\n return;\n }\n \n // Handle ping from WEBVIEW\n if( _event === 'ping' ){\n console.log('[EMBEDDED] Received ping, sending pong');\n this.connectionToken = token;\n this.emit('pong', { token: this.connectionToken });\n return;\n }\n \n // Handle connection acknowledgment\n if( _event === '__connection_ack' ){\n if( token && token !== this.connectionToken ){\n console.error('[EMBEDDED] Invalid connection token in ack');\n return;\n }\n \n console.log('[EMBEDDED] Connection established (received ack)');\n this.connected = true;\n this.processMessageQueue();\n this.fire('connect');\n return;\n }\n \n // Fire event listeners\n this.fire( _event, payload, cid );\n },\n \n announceReady: function(){\n let attempts = 0;\n const maxAttempts = 5;\n const interval = 2000;\n \n console.log('[EMBEDDED] Starting ready announcements');\n \n const announce = () => {\n if( this.connected ){\n console.log('[EMBEDDED] Connected, stopping announcements');\n return;\n }\n \n attempts++;\n if( attempts > maxAttempts ){\n console.log('[EMBEDDED] Max announcement attempts reached');\n return;\n }\n \n console.log('[EMBEDDED] Announcing ready (attempt', attempts + '/' + maxAttempts + ')');\n this.emit('__embedded_ready');\n \n setTimeout( announce, interval );\n };\n \n announce();\n }\n };\n\n // Initialize\n window._wio.listen();\n \n // Start announcing readiness after a short delay\n setTimeout(() => {\n window._wio.announceReady();\n }, 100);\n \n console.log('[EMBEDDED] WIO bridge initialized successfully');\n }\n catch( error ){\n console.error('[EMBEDDED] Setup failed:', error);\n \n // Create minimal fallback\n window._wio = {\n error: error.toString(),\n emit: function(){ console.error('[EMBEDDED] WIO failed to initialize'); },\n on: function(){},\n once: function(){},\n off: function(){}\n };\n }\n\n true;\n })();\n ";
|
|
682
|
+
return "\n (function() {\n try {\n console.log('[EMBEDDED] Initializing WIO bridge...')\n \n const RESERVED_EVENTS = [\n 'ping',\n 'pong',\n '__heartbeat',\n '__heartbeat_response',\n '__embedded_ready',\n '__connection_ack',\n '__webview_ready'\n ]\n \n // Use closure variable to avoid 'this' binding issues\n window._wio = {\n type: 'EMBEDDED',\n connected: false,\n Events: {},\n messageQueue: [],\n connectionToken: null,\n setupComplete: false,\n\n listen: function(){\n console.log('[EMBEDDED] Setting up message listeners...')\n\n // Listen to messages from React Native\n window.addEventListener('message', function( event ){\n try {\n const message = typeof event.data === 'string' ? JSON.parse( event.data ) : event.data\n window._wio.handleMessage( message ) // Use window._wio instead of this\n }\n catch( error ){ console.error('[EMBEDDED] Parse error:', error ) }\n })\n \n // Android support\n if( typeof document !== 'undefined' ){\n document.addEventListener('message', function( event ){\n try {\n const message = typeof event.data === 'string' ? JSON.parse( event.data ) : event.data\n window._wio.handleMessage( message ) // Use window._wio instead of this\n }\n catch( error ){ console.error('[EMBEDDED] Parse error:', error ) }\n })\n }\n\n window._wio.setupComplete = true // Use window._wio instead of this\n console.log('[EMBEDDED] Setup complete')\n\n return window._wio\n },\n \n ackId: function(){\n const\n rmin = 100000,\n rmax = 999999,\n timestamp = Date.now(),\n random = Math.floor( Math.random() * ( rmax - rmin + 1 ) + rmin )\n\n return timestamp + '_' + random\n },\n \n fire: function( _event, payload, cid ){\n if( !window._wio.Events[_event] && !window._wio.Events[_event + '--@once'] ){\n console.log('[EMBEDDED] No listener for:', _event)\n return\n }\n \n const ackFn = cid\n ? ( error, ...args ) => window._wio.emit( _event + '--' + cid + '--@ack', { error: error || false, args } )\n : undefined\n \n let listeners = []\n if( window._wio.Events[_event + '--@once'] ){\n _event += '--@once'\n listeners = window._wio.Events[_event]\n\n delete window._wio.Events[_event]\n }\n else listeners = window._wio.Events[_event] || []\n \n listeners.forEach( fn => {\n try { payload !== undefined ? fn( payload, ackFn ) : fn( ackFn ) }\n catch( error ){ console.error('[EMBEDDED] Listener error:', error ) }\n })\n },\n \n emit: function( _event, payload, fn ){\n if( typeof payload === 'function' ){\n fn = payload\n payload = undefined\n }\n \n if( !window._wio.connected && !RESERVED_EVENTS.includes(_event) ){\n window._wio.messageQueue.push({ _event, payload, fn, timestamp: Date.now() })\n console.log('[EMBEDDED] Queued message:', _event)\n return\n }\n \n try {\n let cid\n if( typeof fn === 'function' ){\n cid = window._wio.ackId()\n window._wio.once( _event + '--' + cid + '--@ack', ({ error, args }) => fn( error, ...args ) )\n }\n \n const messageData = {\n _event,\n payload,\n cid,\n timestamp: Date.now(),\n token: RESERVED_EVENTS.includes(_event) ? window._wio.connectionToken : undefined\n }\n \n if( typeof window.ReactNativeWebView !== 'undefined' )\n window.ReactNativeWebView.postMessage( JSON.stringify( messageData ) )\n else console.error('[EMBEDDED] ReactNativeWebView not available')\n }\n catch( error ){\n console.error('[EMBEDDED] Emit error:', error )\n typeof fn === 'function' && fn( String(error) )\n }\n },\n \n on: function( _event, fn ){\n if( !window._wio.Events[_event] ) window._wio.Events[_event] = []\n window._wio.Events[_event].push( fn )\n\n return window._wio\n },\n \n once: function( _event, fn ){\n _event += '--@once'\n if( !window._wio.Events[_event] ) window._wio.Events[_event] = []\n\n window._wio.Events[_event].push( fn )\n\n return window._wio\n },\n \n off: function( _event, fn ){\n if( fn && window._wio.Events[_event] ){\n const index = window._wio.Events[_event].indexOf( fn )\n if( index > -1 ){\n window._wio.Events[_event].splice( index, 1 )\n if( window._wio.Events[_event].length === 0 ) delete window._wio.Events[_event]\n }\n }\n else delete window._wio.Events[_event]\n\n return window._wio\n },\n \n processMessageQueue: function(){\n if( !window._wio.connected || window._wio.messageQueue.length === 0 ) return\n \n console.log('[EMBEDDED] Processing', window._wio.messageQueue.length, 'queued messages')\n const queue = [ ...window._wio.messageQueue ]\n window._wio.messageQueue = []\n \n queue.forEach( msg => {\n try { window._wio.emit( msg._event, msg.payload, msg.fn ) }\n catch( error ){ console.error('[EMBEDDED] Queue process error:', error ) }\n })\n },\n \n handleMessage: function( data ){\n if( !data || !data._event ) return\n \n const { _event, payload, cid, token } = data\n \n console.log('[EMBEDDED] Received:', _event )\n \n // Handle heartbeat response\n if( _event === '__heartbeat_response' )\n return\n \n // Handle heartbeat request\n if( _event === '__heartbeat' ){\n window._wio.emit('__heartbeat_response', { timestamp: Date.now() })\n return\n }\n \n // Handle webview ready signal\n if( _event === '__webview_ready' ){\n console.log('[EMBEDDED] WebView ready signal received')\n return\n }\n \n // Handle ping from WEBVIEW\n if( _event === 'ping' ){\n console.log('[EMBEDDED] Received ping, sending pong')\n window._wio.connectionToken = token\n\n window._wio.emit('pong', { token: window._wio.connectionToken })\n return\n }\n \n // Handle connection acknowledgment\n if( _event === '__connection_ack' ){\n if( token && token !== window._wio.connectionToken ){\n console.error('[EMBEDDED] Invalid connection token in ack')\n return\n }\n \n console.log('[EMBEDDED] Connection established (received ack)')\n\n window._wio.connected = true\n window._wio.processMessageQueue()\n window._wio.fire('connect')\n\n return\n }\n \n // Fire event listeners\n window._wio.fire( _event, payload, cid )\n },\n \n announceReady: function(){\n let attempts = 0\n const maxAttempts = 5\n const interval = 2000\n \n console.log('[EMBEDDED] Starting ready announcements')\n \n const announce = () => {\n if( window._wio.connected ){\n console.log('[EMBEDDED] Connected, stopping announcements')\n return\n }\n \n attempts++\n if( attempts > maxAttempts ){\n console.log('[EMBEDDED] Max announcement attempts reached')\n return\n }\n \n console.log('[EMBEDDED] Announcing ready (attempt', attempts + '/' + maxAttempts + ')')\n window._wio.emit('__embedded_ready')\n \n setTimeout( announce, interval )\n }\n \n announce()\n }\n }\n\n // Initialize\n wio.listen()\n // Start announcing readiness after a short delay\n setTimeout(() => wio.announceReady(), 100)\n \n console.log('[EMBEDDED] WIO bridge initialized successfully')\n }\n catch( error ){\n console.error('[EMBEDDED] Setup failed:', error )\n \n // Create minimal fallback\n window._wio = {\n error: error.toString(),\n emit: function(){ console.error('[EMBEDDED] WIO failed to initialize') },\n on: function(){},\n once: function(){},\n off: function(){}\n }\n }\n\n true\n })()\n ";
|
|
683
683
|
};
|
|
684
684
|
return WIO;
|
|
685
685
|
}());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webview.io",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Easy and friendly API to connect and interact between React Native and WebView with enhanced security, reliability, and modern async/await support.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
package/src/index.ts
CHANGED
|
@@ -827,10 +827,19 @@ export default class WIO {
|
|
|
827
827
|
return `
|
|
828
828
|
(function() {
|
|
829
829
|
try {
|
|
830
|
-
console.log('[EMBEDDED] Initializing WIO bridge...')
|
|
830
|
+
console.log('[EMBEDDED] Initializing WIO bridge...')
|
|
831
831
|
|
|
832
|
-
const RESERVED_EVENTS = [
|
|
832
|
+
const RESERVED_EVENTS = [
|
|
833
|
+
'ping',
|
|
834
|
+
'pong',
|
|
835
|
+
'__heartbeat',
|
|
836
|
+
'__heartbeat_response',
|
|
837
|
+
'__embedded_ready',
|
|
838
|
+
'__connection_ack',
|
|
839
|
+
'__webview_ready'
|
|
840
|
+
]
|
|
833
841
|
|
|
842
|
+
// Use closure variable to avoid 'this' binding issues
|
|
834
843
|
window._wio = {
|
|
835
844
|
type: 'EMBEDDED',
|
|
836
845
|
connected: false,
|
|
@@ -840,34 +849,32 @@ export default class WIO {
|
|
|
840
849
|
setupComplete: false,
|
|
841
850
|
|
|
842
851
|
listen: function(){
|
|
843
|
-
console.log('[EMBEDDED] Setting up message listeners...')
|
|
852
|
+
console.log('[EMBEDDED] Setting up message listeners...')
|
|
844
853
|
|
|
845
854
|
// Listen to messages from React Native
|
|
846
855
|
window.addEventListener('message', function( event ){
|
|
847
856
|
try {
|
|
848
|
-
const message = typeof event.data === 'string' ? JSON.parse( event.data ) : event.data
|
|
849
|
-
window._wio.handleMessage( message )
|
|
857
|
+
const message = typeof event.data === 'string' ? JSON.parse( event.data ) : event.data
|
|
858
|
+
window._wio.handleMessage( message ) // Use window._wio instead of this
|
|
850
859
|
}
|
|
851
|
-
catch( error ){
|
|
852
|
-
|
|
853
|
-
}
|
|
854
|
-
});
|
|
860
|
+
catch( error ){ console.error('[EMBEDDED] Parse error:', error ) }
|
|
861
|
+
})
|
|
855
862
|
|
|
856
863
|
// Android support
|
|
857
864
|
if( typeof document !== 'undefined' ){
|
|
858
865
|
document.addEventListener('message', function( event ){
|
|
859
866
|
try {
|
|
860
|
-
const message = typeof event.data === 'string' ? JSON.parse( event.data ) : event.data
|
|
861
|
-
window._wio.handleMessage( message )
|
|
862
|
-
}
|
|
863
|
-
catch( error ){
|
|
864
|
-
console.error('[EMBEDDED] Parse error:', error);
|
|
867
|
+
const message = typeof event.data === 'string' ? JSON.parse( event.data ) : event.data
|
|
868
|
+
window._wio.handleMessage( message ) // Use window._wio instead of this
|
|
865
869
|
}
|
|
866
|
-
|
|
870
|
+
catch( error ){ console.error('[EMBEDDED] Parse error:', error ) }
|
|
871
|
+
})
|
|
867
872
|
}
|
|
868
873
|
|
|
869
|
-
|
|
870
|
-
console.log('[EMBEDDED] Setup complete')
|
|
874
|
+
window._wio.setupComplete = true // Use window._wio instead of this
|
|
875
|
+
console.log('[EMBEDDED] Setup complete')
|
|
876
|
+
|
|
877
|
+
return window._wio
|
|
871
878
|
},
|
|
872
879
|
|
|
873
880
|
ackId: function(){
|
|
@@ -875,58 +882,53 @@ export default class WIO {
|
|
|
875
882
|
rmin = 100000,
|
|
876
883
|
rmax = 999999,
|
|
877
884
|
timestamp = Date.now(),
|
|
878
|
-
random = Math.floor( Math.random() * ( rmax - rmin + 1 ) + rmin )
|
|
885
|
+
random = Math.floor( Math.random() * ( rmax - rmin + 1 ) + rmin )
|
|
879
886
|
|
|
880
|
-
return timestamp + '_' + random
|
|
887
|
+
return timestamp + '_' + random
|
|
881
888
|
},
|
|
882
889
|
|
|
883
890
|
fire: function( _event, payload, cid ){
|
|
884
|
-
if( !
|
|
885
|
-
console.log('[EMBEDDED] No listener for:', _event)
|
|
886
|
-
return
|
|
891
|
+
if( !window._wio.Events[_event] && !window._wio.Events[_event + '--@once'] ){
|
|
892
|
+
console.log('[EMBEDDED] No listener for:', _event)
|
|
893
|
+
return
|
|
887
894
|
}
|
|
888
895
|
|
|
889
896
|
const ackFn = cid
|
|
890
|
-
? ( error, ...args ) => {
|
|
891
|
-
|
|
892
|
-
}
|
|
893
|
-
: undefined;
|
|
897
|
+
? ( error, ...args ) => window._wio.emit( _event + '--' + cid + '--@ack', { error: error || false, args } )
|
|
898
|
+
: undefined
|
|
894
899
|
|
|
895
|
-
let listeners = []
|
|
896
|
-
if(
|
|
897
|
-
_event += '--@once'
|
|
898
|
-
listeners =
|
|
899
|
-
|
|
900
|
+
let listeners = []
|
|
901
|
+
if( window._wio.Events[_event + '--@once'] ){
|
|
902
|
+
_event += '--@once'
|
|
903
|
+
listeners = window._wio.Events[_event]
|
|
904
|
+
|
|
905
|
+
delete window._wio.Events[_event]
|
|
900
906
|
}
|
|
901
|
-
else listeners =
|
|
907
|
+
else listeners = window._wio.Events[_event] || []
|
|
902
908
|
|
|
903
909
|
listeners.forEach( fn => {
|
|
904
|
-
try {
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
catch( error ){
|
|
908
|
-
console.error('[EMBEDDED] Listener error:', error);
|
|
909
|
-
}
|
|
910
|
-
});
|
|
910
|
+
try { payload !== undefined ? fn( payload, ackFn ) : fn( ackFn ) }
|
|
911
|
+
catch( error ){ console.error('[EMBEDDED] Listener error:', error ) }
|
|
912
|
+
})
|
|
911
913
|
},
|
|
912
914
|
|
|
913
915
|
emit: function( _event, payload, fn ){
|
|
914
916
|
if( typeof payload === 'function' ){
|
|
915
|
-
fn = payload
|
|
916
|
-
payload = undefined
|
|
917
|
+
fn = payload
|
|
918
|
+
payload = undefined
|
|
917
919
|
}
|
|
918
920
|
|
|
919
|
-
if( !
|
|
920
|
-
|
|
921
|
-
console.log('[EMBEDDED] Queued message:', _event)
|
|
922
|
-
return
|
|
921
|
+
if( !window._wio.connected && !RESERVED_EVENTS.includes(_event) ){
|
|
922
|
+
window._wio.messageQueue.push({ _event, payload, fn, timestamp: Date.now() })
|
|
923
|
+
console.log('[EMBEDDED] Queued message:', _event)
|
|
924
|
+
return
|
|
923
925
|
}
|
|
924
926
|
|
|
925
927
|
try {
|
|
926
|
-
let cid
|
|
928
|
+
let cid
|
|
927
929
|
if( typeof fn === 'function' ){
|
|
928
|
-
cid =
|
|
929
|
-
|
|
930
|
+
cid = window._wio.ackId()
|
|
931
|
+
window._wio.once( _event + '--' + cid + '--@ack', ({ error, args }) => fn( error, ...args ) )
|
|
930
932
|
}
|
|
931
933
|
|
|
932
934
|
const messageData = {
|
|
@@ -934,165 +936,164 @@ export default class WIO {
|
|
|
934
936
|
payload,
|
|
935
937
|
cid,
|
|
936
938
|
timestamp: Date.now(),
|
|
937
|
-
token: RESERVED_EVENTS.includes(_event) ?
|
|
938
|
-
};
|
|
939
|
-
|
|
940
|
-
if( typeof window.ReactNativeWebView !== 'undefined' ){
|
|
941
|
-
window.ReactNativeWebView.postMessage( JSON.stringify( messageData ) );
|
|
942
|
-
}
|
|
943
|
-
else {
|
|
944
|
-
console.error('[EMBEDDED] ReactNativeWebView not available');
|
|
939
|
+
token: RESERVED_EVENTS.includes(_event) ? window._wio.connectionToken : undefined
|
|
945
940
|
}
|
|
941
|
+
|
|
942
|
+
if( typeof window.ReactNativeWebView !== 'undefined' )
|
|
943
|
+
window.ReactNativeWebView.postMessage( JSON.stringify( messageData ) )
|
|
944
|
+
else console.error('[EMBEDDED] ReactNativeWebView not available')
|
|
946
945
|
}
|
|
947
946
|
catch( error ){
|
|
948
|
-
console.error('[EMBEDDED] Emit error:', error)
|
|
949
|
-
typeof fn === 'function' && fn( String(error) )
|
|
947
|
+
console.error('[EMBEDDED] Emit error:', error )
|
|
948
|
+
typeof fn === 'function' && fn( String(error) )
|
|
950
949
|
}
|
|
951
950
|
},
|
|
952
951
|
|
|
953
952
|
on: function( _event, fn ){
|
|
954
|
-
if( !
|
|
955
|
-
|
|
953
|
+
if( !window._wio.Events[_event] ) window._wio.Events[_event] = []
|
|
954
|
+
window._wio.Events[_event].push( fn )
|
|
955
|
+
|
|
956
|
+
return window._wio
|
|
956
957
|
},
|
|
957
958
|
|
|
958
959
|
once: function( _event, fn ){
|
|
959
|
-
_event += '--@once'
|
|
960
|
-
if( !
|
|
961
|
-
|
|
960
|
+
_event += '--@once'
|
|
961
|
+
if( !window._wio.Events[_event] ) window._wio.Events[_event] = []
|
|
962
|
+
|
|
963
|
+
window._wio.Events[_event].push( fn )
|
|
964
|
+
|
|
965
|
+
return window._wio
|
|
962
966
|
},
|
|
963
967
|
|
|
964
968
|
off: function( _event, fn ){
|
|
965
|
-
if( fn &&
|
|
966
|
-
const index =
|
|
969
|
+
if( fn && window._wio.Events[_event] ){
|
|
970
|
+
const index = window._wio.Events[_event].indexOf( fn )
|
|
967
971
|
if( index > -1 ){
|
|
968
|
-
|
|
969
|
-
if(
|
|
972
|
+
window._wio.Events[_event].splice( index, 1 )
|
|
973
|
+
if( window._wio.Events[_event].length === 0 ) delete window._wio.Events[_event]
|
|
970
974
|
}
|
|
971
975
|
}
|
|
972
|
-
else delete
|
|
976
|
+
else delete window._wio.Events[_event]
|
|
977
|
+
|
|
978
|
+
return window._wio
|
|
973
979
|
},
|
|
974
980
|
|
|
975
981
|
processMessageQueue: function(){
|
|
976
|
-
if( !
|
|
982
|
+
if( !window._wio.connected || window._wio.messageQueue.length === 0 ) return
|
|
977
983
|
|
|
978
|
-
console.log('[EMBEDDED] Processing',
|
|
979
|
-
const queue = [...
|
|
980
|
-
|
|
984
|
+
console.log('[EMBEDDED] Processing', window._wio.messageQueue.length, 'queued messages')
|
|
985
|
+
const queue = [ ...window._wio.messageQueue ]
|
|
986
|
+
window._wio.messageQueue = []
|
|
981
987
|
|
|
982
988
|
queue.forEach( msg => {
|
|
983
|
-
try {
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
catch( error ){
|
|
987
|
-
console.error('[EMBEDDED] Queue process error:', error);
|
|
988
|
-
}
|
|
989
|
-
});
|
|
989
|
+
try { window._wio.emit( msg._event, msg.payload, msg.fn ) }
|
|
990
|
+
catch( error ){ console.error('[EMBEDDED] Queue process error:', error ) }
|
|
991
|
+
})
|
|
990
992
|
},
|
|
991
993
|
|
|
992
994
|
handleMessage: function( data ){
|
|
993
|
-
if( !data || !data._event ) return
|
|
995
|
+
if( !data || !data._event ) return
|
|
994
996
|
|
|
995
|
-
const { _event, payload, cid, token } = data
|
|
997
|
+
const { _event, payload, cid, token } = data
|
|
996
998
|
|
|
997
|
-
console.log('[EMBEDDED] Received:', _event)
|
|
999
|
+
console.log('[EMBEDDED] Received:', _event )
|
|
998
1000
|
|
|
999
1001
|
// Handle heartbeat response
|
|
1000
|
-
if( _event === '__heartbeat_response' )
|
|
1001
|
-
return
|
|
1002
|
-
}
|
|
1002
|
+
if( _event === '__heartbeat_response' )
|
|
1003
|
+
return
|
|
1003
1004
|
|
|
1004
1005
|
// Handle heartbeat request
|
|
1005
1006
|
if( _event === '__heartbeat' ){
|
|
1006
|
-
|
|
1007
|
-
return
|
|
1007
|
+
window._wio.emit('__heartbeat_response', { timestamp: Date.now() })
|
|
1008
|
+
return
|
|
1008
1009
|
}
|
|
1009
1010
|
|
|
1010
1011
|
// Handle webview ready signal
|
|
1011
1012
|
if( _event === '__webview_ready' ){
|
|
1012
|
-
console.log('[EMBEDDED] WebView ready signal received')
|
|
1013
|
-
return
|
|
1013
|
+
console.log('[EMBEDDED] WebView ready signal received')
|
|
1014
|
+
return
|
|
1014
1015
|
}
|
|
1015
1016
|
|
|
1016
1017
|
// Handle ping from WEBVIEW
|
|
1017
1018
|
if( _event === 'ping' ){
|
|
1018
|
-
console.log('[EMBEDDED] Received ping, sending pong')
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1019
|
+
console.log('[EMBEDDED] Received ping, sending pong')
|
|
1020
|
+
window._wio.connectionToken = token
|
|
1021
|
+
|
|
1022
|
+
window._wio.emit('pong', { token: window._wio.connectionToken })
|
|
1023
|
+
return
|
|
1022
1024
|
}
|
|
1023
1025
|
|
|
1024
1026
|
// Handle connection acknowledgment
|
|
1025
1027
|
if( _event === '__connection_ack' ){
|
|
1026
|
-
if( token && token !==
|
|
1027
|
-
console.error('[EMBEDDED] Invalid connection token in ack')
|
|
1028
|
-
return
|
|
1028
|
+
if( token && token !== window._wio.connectionToken ){
|
|
1029
|
+
console.error('[EMBEDDED] Invalid connection token in ack')
|
|
1030
|
+
return
|
|
1029
1031
|
}
|
|
1030
1032
|
|
|
1031
|
-
console.log('[EMBEDDED] Connection established (received ack)')
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1033
|
+
console.log('[EMBEDDED] Connection established (received ack)')
|
|
1034
|
+
|
|
1035
|
+
window._wio.connected = true
|
|
1036
|
+
window._wio.processMessageQueue()
|
|
1037
|
+
window._wio.fire('connect')
|
|
1038
|
+
|
|
1039
|
+
return
|
|
1036
1040
|
}
|
|
1037
1041
|
|
|
1038
1042
|
// Fire event listeners
|
|
1039
|
-
|
|
1043
|
+
window._wio.fire( _event, payload, cid )
|
|
1040
1044
|
},
|
|
1041
1045
|
|
|
1042
1046
|
announceReady: function(){
|
|
1043
|
-
let attempts = 0
|
|
1044
|
-
const maxAttempts = 5
|
|
1045
|
-
const interval = 2000
|
|
1047
|
+
let attempts = 0
|
|
1048
|
+
const maxAttempts = 5
|
|
1049
|
+
const interval = 2000
|
|
1046
1050
|
|
|
1047
|
-
console.log('[EMBEDDED] Starting ready announcements')
|
|
1051
|
+
console.log('[EMBEDDED] Starting ready announcements')
|
|
1048
1052
|
|
|
1049
1053
|
const announce = () => {
|
|
1050
|
-
if(
|
|
1051
|
-
console.log('[EMBEDDED] Connected, stopping announcements')
|
|
1052
|
-
return
|
|
1054
|
+
if( window._wio.connected ){
|
|
1055
|
+
console.log('[EMBEDDED] Connected, stopping announcements')
|
|
1056
|
+
return
|
|
1053
1057
|
}
|
|
1054
1058
|
|
|
1055
|
-
attempts
|
|
1059
|
+
attempts++
|
|
1056
1060
|
if( attempts > maxAttempts ){
|
|
1057
|
-
console.log('[EMBEDDED] Max announcement attempts reached')
|
|
1058
|
-
return
|
|
1061
|
+
console.log('[EMBEDDED] Max announcement attempts reached')
|
|
1062
|
+
return
|
|
1059
1063
|
}
|
|
1060
1064
|
|
|
1061
|
-
console.log('[EMBEDDED] Announcing ready (attempt', attempts + '/' + maxAttempts + ')')
|
|
1062
|
-
|
|
1065
|
+
console.log('[EMBEDDED] Announcing ready (attempt', attempts + '/' + maxAttempts + ')')
|
|
1066
|
+
window._wio.emit('__embedded_ready')
|
|
1063
1067
|
|
|
1064
|
-
setTimeout( announce, interval )
|
|
1065
|
-
}
|
|
1068
|
+
setTimeout( announce, interval )
|
|
1069
|
+
}
|
|
1066
1070
|
|
|
1067
|
-
announce()
|
|
1071
|
+
announce()
|
|
1068
1072
|
}
|
|
1069
|
-
}
|
|
1073
|
+
}
|
|
1070
1074
|
|
|
1071
1075
|
// Initialize
|
|
1072
|
-
|
|
1073
|
-
|
|
1076
|
+
wio.listen()
|
|
1074
1077
|
// Start announcing readiness after a short delay
|
|
1075
|
-
setTimeout(() =>
|
|
1076
|
-
window._wio.announceReady();
|
|
1077
|
-
}, 100);
|
|
1078
|
+
setTimeout(() => wio.announceReady(), 100)
|
|
1078
1079
|
|
|
1079
|
-
console.log('[EMBEDDED] WIO bridge initialized successfully')
|
|
1080
|
+
console.log('[EMBEDDED] WIO bridge initialized successfully')
|
|
1080
1081
|
}
|
|
1081
1082
|
catch( error ){
|
|
1082
|
-
console.error('[EMBEDDED] Setup failed:', error)
|
|
1083
|
+
console.error('[EMBEDDED] Setup failed:', error )
|
|
1083
1084
|
|
|
1084
1085
|
// Create minimal fallback
|
|
1085
1086
|
window._wio = {
|
|
1086
1087
|
error: error.toString(),
|
|
1087
|
-
emit: function(){ console.error('[EMBEDDED] WIO failed to initialize')
|
|
1088
|
+
emit: function(){ console.error('[EMBEDDED] WIO failed to initialize') },
|
|
1088
1089
|
on: function(){},
|
|
1089
1090
|
once: function(){},
|
|
1090
1091
|
off: function(){}
|
|
1091
|
-
}
|
|
1092
|
+
}
|
|
1092
1093
|
}
|
|
1093
1094
|
|
|
1094
|
-
true
|
|
1095
|
-
})()
|
|
1095
|
+
true
|
|
1096
|
+
})()
|
|
1096
1097
|
`
|
|
1097
1098
|
}
|
|
1098
1099
|
}
|