GithubHelp home page GithubHelp logo

Comments (4)

knopp avatar knopp commented on July 24, 2024 3

This PR adds event channel support.

There are no examples yet, but the usage should be roughtly like this:

const CHANNEL_APP_NOTIFICATIONS: &str = "channel_name";

pub struct AppState {
    sinks: HashMap<i64, EventSink>,
}

impl AppState {
    pub fn new(context: Context) -> RegisteredEventChannel<Self> {
        Self {
            sinks: HashMap::new(),
        }
        .register(context, CHANNEL_APP_NOTIFICATIONS)
    }

    fn send_message(&self, message: &Value) {
        for sink in self.sinks.values() {
            sink.send_message(message).ok();
        }
    }
}

impl EventChannelHandler for AppState {
    fn register_event_sink(
        &mut self,
        sink: nativeshell::shell::EventSink,
        _listen_argument: Value,
    ) {
        self.sinks.insert(sink.id(), sink);
    }

    fn unregister_event_sink(&mut self, sink_id: i64) {
        self.sinks.remove(&sink_id);
    }
}

Dart side:

   const _appNotificationsChannel = EventChannel('channel_name');
   ...
    _appNotificationsChannel
        .receiveBroadcastStream()
        .listen((event) {...});

from nativeshell.

knopp avatar knopp commented on July 24, 2024

The low-ish level api for this would be shell.MessageManager, register_message_handler and get_message_sender. Those operate on messages instead of method calls. I played a bit with async executor that invokes futures on main loop but in the end decided it might not be worth it.

from nativeshell.

maxjoehnk avatar maxjoehnk commented on July 24, 2024

It's working like a charm. Thank you!
If you want I can add an example using an Async Executor to send stream events down an EventSink.

from nativeshell.

knopp avatar knopp commented on July 24, 2024

That'd be great! Examples will need some work right now to build with current master. I plan to do that over this weekend (as well as new nativeshell release).

from nativeshell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.