-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(client): add callback/event mechanism between TypeScript and Go #2330
base: master
Are you sure you want to change the base?
Conversation
client/electron/go_plugin.ts
Outdated
* @param name The name of the event to unsubscribe from. | ||
* @returns A Promise that resolves when the unsubscription is successful. | ||
*/ | ||
export async function unsubscribeEvent(name: string): Promise<void> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you link the unsubscribe action to the actual subscription instead of the event name? It's a cleaner way to manage subscriptions and allows the TypeScript code to subscribe to the same event twice if needed.
Looks like Koffi already gives you a unique callback object, so you could wrap that in some dedicated Subscription
type with an unsubscribe()
method. Or assign it a unique ID and pass that around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I also decided to differentiate a Callback
and an Event
handler. A Callback
can be used as an Event
handler, it can also be used in other MethodChannel
functions as well (for example, calling TestConnectivity
and have an ResultCallback
to receive the result). Then we will have a general way to call TypeScript functions from Go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored, now it accepts multiple callbacks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you still need to update the PR to decouple the callback from the event handling. Let me know when I should take a look
client/electron/go_plugin.ts
Outdated
* @param data The event data string passed from the source. | ||
* @param param The param string provided during registration. | ||
*/ | ||
export type CallbackFunction = (data: string, param: string) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand you are decoupling callbacks from events like we discussed. So this would have a single data parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
client/electron/go_plugin.ts
Outdated
* | ||
* @remarks Subscribing to an event will replace any previously subscribed callback for that event. | ||
*/ | ||
export async function subscribeEvent( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that this will become a regular MethodChannel call that takes a callback id?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, done.
f617f0b
to
3ced93e
Compare
This PR introduces a callback/event mechanism that allows TypeScript code to subscribe to events triggered in the Go code.
To demonstrate the functionality, this PR implements a VPN connection status change event. The TypeScript code can now subscribe to this event and receive updates on the VPN connection status.
This mechanism can be leveraged for both electron and the Cordova code.