Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Add client side Event helper #38

Merged
merged 5 commits into from
Sep 13, 2024
Merged

Add client side Event helper #38

merged 5 commits into from
Sep 13, 2024

Conversation

gwleuverink
Copy link
Contributor

As mentioned in NativePHP/nativephp.com#44 (comment) there is an opportunity to flesh out the api for working with broadcasted server events on the client side.

This PR introduces a window.Native object that exposes a event listener for any server side events broadcasted over ICP

<script>
    Native.on('\\App\\Events\\BroadcastOverIpc', payload => {
        alert('Event caught in client via Native helper')
    })
</script>

I'm considering replacing the event name match in native.st:6 with event.endsWith(data.event) (or possibly strip trailing slashes before the conditional). This way the leading \\ in the passed event name.

I'll revisit this PR tomorrow.

@gwleuverink
Copy link
Contributor Author

Leading slashes can now be omitted

<script>
    Native.on('App\\Events\\BroadcastOverIpc', payload => {
        alert('Event caught with Native helper')
    })
</script>

@gwleuverink
Copy link
Contributor Author

Because the callback is returned when invoked you can do stuff like this

let podcastId = Native.on('App\\Events\\PodcastProcessed', payload => payload.podcastId)

@gwleuverink gwleuverink marked this pull request as ready for review September 13, 2024 08:36
src/preload/native.ts Outdated Show resolved Hide resolved
@simonhamp simonhamp merged commit 7bae295 into NativePHP:main Sep 13, 2024
1 check failed
@simonhamp
Copy link
Member

Do you fancy updating your docs PR with the detail of how to use this?

@gwleuverink
Copy link
Contributor Author

Done!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants