-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7cdac4
commit 9836208
Showing
19 changed files
with
1,686 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'dart:async'; | ||
import 'dart:ffi'; | ||
import 'dart:isolate'; | ||
|
||
import 'ns_data.dart'; | ||
import 'ns_string.dart'; | ||
import 'objective_c_bindings_generated.dart'; | ||
|
||
extension NSInputStreamStreamExtension on Stream<List<int>> { | ||
/// Return a [NSInputStream] that, when read, will contain the contents of | ||
/// the [Stream]. | ||
/// | ||
/// > [!IMPORTANT] | ||
/// > [NSInputStream.read_maxLength_] must be called from a different thread | ||
/// > or [Isolate] than the one that calls [toNSInputStream]. Otherwise, | ||
/// > [NSInputStream.read_maxLength_] will deadlock waiting for data to be | ||
/// > added from the [Stream]. | ||
NSInputStream toNSInputStream() { | ||
// Eagerly add data until `maxReadAheadSize` is buffered. | ||
const maxReadAheadSize = 4096; | ||
|
||
final port = ReceivePort(); | ||
final inputStream = | ||
DartInputStreamAdapter.inputStreamWithPort_(port.sendPort.nativePort); | ||
late final StreamSubscription<dynamic> dataSubscription; | ||
|
||
dataSubscription = listen((data) { | ||
if (inputStream.addData_(data.toNSData()) > maxReadAheadSize) { | ||
dataSubscription.pause(); | ||
} | ||
}, onError: (Object e) { | ||
final d = NSMutableDictionary.new1(); | ||
d.setObject_forKey_(e.toString().toNSString(), NSLocalizedDescriptionKey); | ||
inputStream.setError_(NSError.errorWithDomain_code_userInfo_( | ||
'DartError'.toNSString(), 0, d)); | ||
port.close(); | ||
}, onDone: () { | ||
inputStream.setDone(); | ||
port.close(); | ||
}, cancelOnError: true); | ||
|
||
dataSubscription.pause(); | ||
port.listen((count) { | ||
// -1 indicates that the `NSInputStream` is closed. All other values | ||
// indicate that the `NSInputStream` needs more data. | ||
if (count == -1) { | ||
dataSubscription.cancel(); | ||
} else { | ||
dataSubscription.resume(); | ||
} | ||
}, onDone: () { | ||
dataSubscription.cancel(); | ||
}); | ||
|
||
return inputStream; | ||
} | ||
} |
Oops, something went wrong.