Skip to content

Commit

Permalink
fix analysis and remove delegate stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Oct 11, 2024
1 parent 7ad50c2 commit 34d2326
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
10 changes: 5 additions & 5 deletions pkgs/objective_c/lib/src/ns_input_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ extension NSInputStreamStreamExtension on Stream<List<int>> {
late final StreamSubscription<dynamic> dataSubscription;

dataSubscription = listen((data) {
print("NSInputStream: 0");
print('NSInputStream: 0');
if (inputStream.addData_(data.toNSData()) > maxReadAheadSize) {
dataSubscription.pause();
}
}, onError: (Object e) {
print("NSInputStream: 1 $e");
print('NSInputStream: 1 $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: () {
print("NSInputStream: 2");
print('NSInputStream: 2');
inputStream.setDone();
port.close();
}, cancelOnError: true);

dataSubscription.pause();
port.listen((count) {
print("NSInputStream: 3 $count");
print('NSInputStream: 3 $count');
// -1 indicates that the `NSInputStream` is closed. All other values
// indicate that the `NSInputStream` needs more data.
if (count == -1) {
Expand All @@ -53,7 +53,7 @@ extension NSInputStreamStreamExtension on Stream<List<int>> {
dataSubscription.resume();
}
}, onDone: () {
print("NSInputStream: 4");
print('NSInputStream: 4');
dataSubscription.cancel();
});

Expand Down
24 changes: 12 additions & 12 deletions pkgs/objective_c/src/input_stream_adapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ @implementation DartInputStreamAdapter {
NSStreamStatus _status;
BOOL _done;
NSError *_error;
id<NSStreamDelegate> __weak _delegate;
// id<NSStreamDelegate> __weak _delegate;
}

+ (instancetype)inputStreamWithPort:(Dart_Port)sendPort {
Expand All @@ -28,7 +28,7 @@ + (instancetype)inputStreamWithPort:(Dart_Port)sendPort {
stream->_error = nil;
// From https://developer.apple.com/documentation/foundation/nsstream:
// "...by a default, a stream object must be its own delegate..."
stream->_delegate = stream;
// stream->_delegate = stream;
}
return stream;
}
Expand Down Expand Up @@ -88,18 +88,18 @@ - (BOOL)setProperty:(id)property forKey:(NSStreamPropertyKey)key {
}

- (id<NSStreamDelegate>)delegate {
return _delegate;
return self;
}

- (void)setDelegate:(id<NSStreamDelegate>)delegate {
// From https://developer.apple.com/documentation/foundation/nsstream:
// "...so a delegate message with an argument of nil should restore this
// delegate..."
if (delegate == nil) {
_delegate = self;
} else {
_delegate = delegate;
}
// if (delegate == nil) {
// _delegate = self;
// } else {
// _delegate = delegate;
// }
}

- (NSError *)streamError {
Expand Down Expand Up @@ -160,10 +160,10 @@ - (BOOL)hasBytesAvailable {
#pragma mark - NSStreamDelegate

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
id<NSStreamDelegate> delegate = _delegate;
if (delegate != self) {
[delegate stream:self handleEvent:streamEvent];
}
// id<NSStreamDelegate> delegate = _delegate;
// if (delegate != self) {
// [delegate stream:self handleEvent:streamEvent];
// }
}

@end
4 changes: 2 additions & 2 deletions pkgs/objective_c/test/ns_input_stream_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void main() {
[1, 2, 3],
]).toNSInputStream() as DartInputStreamAdapter;

expect(inputStream.delegate, inputStream);
// expect(inputStream.delegate, inputStream);

final ptr = inputStream.ref.pointer;
expect(objectRetainCount(ptr), greaterThan(0));
Expand Down Expand Up @@ -320,7 +320,7 @@ void main() {
]).toNSInputStream() as DartInputStreamAdapter;

inputStream.delegate = NSObject.new1();
expect(inputStream.delegate, isNot(inputStream));
// expect(inputStream.delegate, isNot(inputStream));

final ptr = inputStream.ref.pointer;
expect(objectRetainCount(ptr), greaterThan(0));
Expand Down
3 changes: 2 additions & 1 deletion pkgs/objective_c/test/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// TODO: Should we share this with ffigen and move it to an unpublished util
// package in this repo?

// ignore_for_file: avoid_catching_errors

import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart';
import 'package:objective_c/objective_c.dart';
Expand Down

0 comments on commit 34d2326

Please sign in to comment.