Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[webview_flutter_wkwebview] Adds the dispose method to NSObjectFlutte…
Browse files Browse the repository at this point in the history
…rApi (#5970)
  • Loading branch information
bparrishMines committed Jun 15, 2022
1 parent ae3f9d6 commit 0b29924
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
Expand Up @@ -451,6 +451,8 @@ NSObject<FlutterMessageCodec> *FWFNSObjectFlutterApiGetCodec(void);
changeKeys:(NSArray<FWFNSKeyValueChangeKeyEnumData *> *)changeKeys
changeValues:(NSArray<id> *)changeValues
completion:(void (^)(NSError *_Nullable))completion;
- (void)disposeObjectWithIdentifier:(NSNumber *)identifier
completion:(void (^)(NSError *_Nullable))completion;
@end
/// The codec used by FWFWKWebViewHostApi.
NSObject<FlutterMessageCodec> *FWFWKWebViewHostApiGetCodec(void);
Expand Down
Expand Up @@ -1989,6 +1989,17 @@ - (void)observeValueForObjectWithIdentifier:(NSNumber *)arg_identifier
completion(nil);
}];
}
- (void)disposeObjectWithIdentifier:(NSNumber *)arg_identifier
completion:(void (^)(NSError *_Nullable))completion {
FlutterBasicMessageChannel *channel = [FlutterBasicMessageChannel
messageChannelWithName:@"dev.flutter.pigeon.NSObjectFlutterApi.dispose"
binaryMessenger:self.binaryMessenger
codec:FWFNSObjectFlutterApiGetCodec()];
[channel sendMessage:@[ arg_identifier ?: [NSNull null] ]
reply:^(id reply) {
completion(nil);
}];
}
@end
@interface FWFWKWebViewHostApiCodecReader : FlutterStandardReader
@end
Expand Down
Expand Up @@ -1770,6 +1770,7 @@ abstract class NSObjectFlutterApi {
int objectIdentifier,
List<NSKeyValueChangeKeyEnumData?> changeKeys,
List<Object?> changeValues);
void dispose(int identifier);
static void setup(NSObjectFlutterApi? api,
{BinaryMessenger? binaryMessenger}) {
{
Expand Down Expand Up @@ -1806,6 +1807,25 @@ abstract class NSObjectFlutterApi {
});
}
}
{
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.NSObjectFlutterApi.dispose', codec,
binaryMessenger: binaryMessenger);
if (api == null) {
channel.setMessageHandler(null);
} else {
channel.setMessageHandler((Object? message) async {
assert(message != null,
'Argument for dev.flutter.pigeon.NSObjectFlutterApi.dispose was null.');
final List<Object?> args = (message as List<Object?>?)!;
final int? arg_identifier = (args[0] as int?);
assert(arg_identifier != null,
'Argument for dev.flutter.pigeon.NSObjectFlutterApi.dispose was null, expected non-null int.');
api.dispose(arg_identifier!);
return;
});
}
}
}
}

Expand Down
Expand Up @@ -183,4 +183,9 @@ class NSObjectFlutterApiImpl extends NSObjectFlutterApi {
), changeValues),
);
}

@override
void dispose(int identifier) {
instanceManager.remove(identifier);
}
}
Expand Up @@ -510,6 +510,9 @@ abstract class NSObjectFlutterApi {
List<NSKeyValueChangeKeyEnumData?> changeKeys,
List<Object?> changeValues,
);

@ObjCSelector('disposeObjectWithIdentifier:')
void dispose(int identifier);
}

/// Mirror of WKWebView.
Expand Down
Expand Up @@ -90,7 +90,7 @@ void main() {
));
});

test('dispose', () async {
test('NSObjectHostApi.dispose', () async {
int? callbackIdentifier;
final InstanceManager instanceManager =
InstanceManager(onWeakReferenceRemoved: (int identifier) {
Expand Down Expand Up @@ -145,6 +145,20 @@ void main() {
]),
);
});

test('NSObjectFlutterApi.dispose', () {
FoundationFlutterApis.instance = FoundationFlutterApis(
instanceManager: instanceManager,
);

object = NSObject(instanceManager: instanceManager);
instanceManager.addHostCreatedInstance(object, 1);

instanceManager.removeWeakReference(object);
FoundationFlutterApis.instance.object.dispose(1);

expect(instanceManager.containsIdentifier(1), isFalse);
});
});
});
}

0 comments on commit 0b29924

Please sign in to comment.