diff --git a/dist/preload/index.js b/dist/preload/index.js index e7983a0..f788ae3 100644 --- a/dist/preload/index.js +++ b/dist/preload/index.js @@ -38,23 +38,25 @@ electron_1.ipcRenderer.on('log', (event, { level, message, context }) => { } }); electron_1.ipcRenderer.on('native-event', (event, data) => { - if (!window.livewire) { - return; + if (window.Livewire) { + window.Livewire.dispatch('native:' + data.event, data.payload); } - window.livewire.components.components().forEach(component => { - if (Array.isArray(component.listeners)) { - component.listeners.forEach(event => { - if (event.startsWith('native')) { - let event_parts = event.split(/(native:|native-)|:|,/); - if (event_parts[1] == 'native:') { - event_parts.splice(2, 0, 'private', undefined, 'nativephp', undefined); + if (window.livewire) { + window.livewire.components.components().forEach(component => { + if (Array.isArray(component.listeners)) { + component.listeners.forEach(event => { + if (event.startsWith('native')) { + let event_parts = event.split(/(native:|native-)|:|,/); + if (event_parts[1] == 'native:') { + event_parts.splice(2, 0, 'private', undefined, 'nativephp', undefined); + } + let [s1, signature, channel_type, s2, channel, s3, event_name,] = event_parts; + if (data.event === event_name) { + window.livewire.emit(event, data.payload); + } } - let [s1, signature, channel_type, s2, channel, s3, event_name,] = event_parts; - if (data.event === event_name) { - window.livewire.emit(event, data.payload); - } - } - }); - } - }); + }); + } + }); + } }); diff --git a/src/preload/index.ts b/src/preload/index.ts index ad44972..f691e47 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -16,37 +16,45 @@ ipcRenderer.on('log', (event, {level, message, context}) => { // Add Livewire event listeners ipcRenderer.on('native-event', (event, data) => { + + // add support for livewire 3 // @ts-ignore - if (!window.livewire) { - return; + if (window.Livewire) { + // @ts-ignore + window.Livewire.dispatch('native:' + data.event, data.payload); } + + // add support for livewire 2 // @ts-ignore - window.livewire.components.components().forEach(component => { - if (Array.isArray(component.listeners)) { - component.listeners.forEach(event => { - if (event.startsWith('native')) { - let event_parts = event.split(/(native:|native-)|:|,/) + if (window.livewire) { + // @ts-ignore + window.livewire.components.components().forEach(component => { + if (Array.isArray(component.listeners)) { + component.listeners.forEach(event => { + if (event.startsWith('native')) { + let event_parts = event.split(/(native:|native-)|:|,/) - if (event_parts[1] == 'native:') { - event_parts.splice(2, 0, 'private', undefined, 'nativephp', undefined) - } + if (event_parts[1] == 'native:') { + event_parts.splice(2, 0, 'private', undefined, 'nativephp', undefined) + } - let [ - s1, - signature, - channel_type, - s2, - channel, - s3, - event_name, - ] = event_parts + let [ + s1, + signature, + channel_type, + s2, + channel, + s3, + event_name, + ] = event_parts - if (data.event === event_name) { - // @ts-ignore - window.livewire.emit(event, data.payload) + if (data.event === event_name) { + // @ts-ignore + window.livewire.emit(event, data.payload) + } } - } - }) - } - }) + }) + } + }) + } })