Search code examples
dartdart-native-extension

Dart native extension: How to create "Push" data stream


I'll refer conceptually to "Pushed" vs "Polled" streams to help describe my question's objective which is to "*Create a Pushed data stream emulating the data flow behavior of Dart's stdin stream*"

"Polled": In my previous question, Menzoni's answer is a great example of a "Polled" stream. The stream is always initiated and data-requested from the Dart console app to the native-extension. The request is fulfilled and a response returns to the app. This is typical of Dart stream usage from my experience.

"Pushed": Data arriving from an external source pushed down to the console app. Data origin and transport technology doesn't matter, it could be stdin keyboard/mouse events,PLC interrupts, database async notifications, etc.

I'll use the stdin stream for purpose of this illustration, its a good example of the data flow I'm trying to achieve. Data in the form of virtualkeycodes/ints arrive within the native extension, buffered, then need to be "Pushed" (sent/posted) to the Dart app without requiring any pull/poll requests for data from the app. This is the objective I'm trying to achieve in my native-extension's custom stream!

Dart console app pseudo-code:

Stream<List<int>> virtKeyCodes = NativeCreatePushedStream_Keycodes();   
virtKeyCodes.listen(processInts);
void processInts(List<int> kbinput) {
    print("processInts: found ${kbinput.length} kbinput chars");
    for (int i = 0; i < kbinput.length; i++) {
        print("...kbinput:${kbinput[i]}");
    }
}

Native extension pseudo-code(I'm guessing here!)

Initial call create persistant stream
Return to console app where it starts to listen
onDataReady somehow post ints to stream
DartVM pushes to console app

Thanks for your help.


Solution

  • Implementing a "Pushed" data-stream via asynchronous native extension

    Created dxConsole "Dart Console Library for Windows" project, now hosted on Github.