Skip to content

feat: EventChannel #178

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
Jul 16, 2019
Merged

feat: EventChannel #178

merged 4 commits into from
Jul 16, 2019

Conversation

pchampio
Copy link
Member

@pchampio pchampio commented Jul 2, 2019

fixes #103
WIP.

Everything is good except for the EndOfStream impl flutter/flutter#35434.

@pchampio
Copy link
Member Author

pchampio commented Jul 3, 2019

I'm making progress on the implementation. I had a hard time figuring out what to do when the user triggers a hot restart.
The implementation survive hot restart, without wasting too mush resources!

Here is a example of a Golang EventChannel (Clickable), matching the sample found here
package main

import (
	"time"

	"github.com/go-flutter-desktop/go-flutter"
	"github.com/go-flutter-desktop/go-flutter/plugin"
)

var options = []flutter.Option{
	flutter.PopBehavior(flutter.PopBehaviorClose), // on SystemNavigator.pop() closes the app

	flutter.AddPlugin(&test{}),
}

const textinputChannelName = "com.yourcompany.eventchannelsample/stream"

type test struct {
	stop chan bool
}

var _ flutter.Plugin = &test{}

func (p *test) InitPlugin(messenger plugin.BinaryMessenger) error {
	p.stop = make(chan bool)

	channel := plugin.NewEventChannel(messenger, textinputChannelName, plugin.StandardMethodCodec{})
	// channel := plugin.NewEventChannel(messenger, textinputChannelName, plugin.JSONMethodCodec{})

	channel.Handle(p)
	print(channel)
	return nil
}

func (p *test) OnListen(arguments interface{}, sink *plugin.EventSink) {
	var i int32
	i = 0
	for {
		select {
		case <-p.stop:
			return
		default:
			i++
			time.Sleep(100 * time.Millisecond)
			// print("\nsend\n")
			sink.Success(i)
		}
	}
}
func (p *test) OnCancel(arguments interface{}) {
	// I choose to use channels to Cancel events. 
	// Mutex can also work.
	// I found that channels are bit more reliable than Mutex during hot restart. 
	p.stop <- true
}

@pchampio
Copy link
Member Author

Waiting for a PR to be merged on flutter/engine #9781
The golang implementation is good to be reviewed.

@pchampio pchampio marked this pull request as ready for review July 11, 2019 14:05
Copy link
Member

@GeertJohan GeertJohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a spelling mistake and some formatting, otherwise looks good and can be merged!

@pchampio pchampio requested a review from GeertJohan July 15, 2019 14:40
@pchampio
Copy link
Member Author

pchampio commented Jul 15, 2019

I'll merge this PR once flutter/engine#9781 gets merged into master

@pchampio pchampio merged commit 632cb1c into master Jul 16, 2019
@pchampio pchampio deleted the feature/eventchannel branch July 16, 2019 19:05
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add support for EventChannel
2 participants