Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix kraken.methodChannel.setMethodCallHandler did't get called before kraken.invokeMethod called. #289

Merged
merged 3 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions integration_tests/specs/method-channel/method-channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
describe('MethodChannel', () => {
it('setMethodCallHandler multi params', async (done) => {
kraken.methodChannel.setMethodCallHandler((method: string, args: any[]) => {
expect(method).toBe('helloworld');
expect(args).toEqual(['abc', 1234, null, /* undefined will be converted to */ null, [], true, false, {name: 1}]);
done();
});
let result = await kraken.methodChannel.invokeMethod('helloworld', 'abc', 1234, null, undefined, [], true, false, {name: 1});
expect(result).toBe('method: helloworld');
});

it('invokeMethod', async () => {
let result = await kraken.methodChannel.invokeMethod('helloworld', 'abc');
// TEST App will return method string
Expand All @@ -15,16 +25,6 @@ describe('MethodChannel', () => {
expect(result).toBe('method: helloworld');
});

it('setMethodCallHandler multi params', async (done) => {
kraken.methodChannel.setMethodCallHandler((method: string, args: any[]) => {
expect(method).toBe('helloworld');
expect(args).toEqual(['abc', 1234, null, /* undefined will be converted to */ null, [], true, false, {name: 1}]);
done();
});
let result = await kraken.methodChannel.invokeMethod('helloworld', 'abc', 1234, null, undefined, [], true, false, {name: 1});
expect(result).toBe('method: helloworld');
});

it('setMethodCallHandler multi params with multi handler', async (done) => {
let handlerCount = 0;
kraken.methodChannel.setMethodCallHandler((method: string, args: any[]) => {
Expand Down
2 changes: 2 additions & 0 deletions kraken/lib/src/launcher/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ class KrakenController {
}

_methodChannel = methodChannel;
KrakenMethodChannel.setJSMethodCallCallback(this);

_view = KrakenViewController(viewportWidth, viewportHeight,
background: background,
showPerformanceOverlay: showPerformanceOverlay,
Expand Down
19 changes: 12 additions & 7 deletions kraken/lib/src/module/method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ Future<dynamic> _invokeMethodFromJavaScript(KrakenController controller, String
return controller.methodChannel._invokeMethodFromJavaScript(method, args);
}

const METHOD_CHANNEL_NAME = 'MethodChannel';

class MethodChannelModule extends BaseModule {
@override
String get name => 'MethodChannel';
MethodChannelModule(ModuleManager moduleManager) : super(moduleManager) {
if (moduleManager == null) return;
moduleManager.controller.methodChannel._onJSMethodCall = (String method, dynamic arguments) async {
moduleManager.emitModuleEvent(name, data: [method, arguments]);
};
}
String get name => METHOD_CHANNEL_NAME;
MethodChannelModule(ModuleManager moduleManager) : super(moduleManager);

@override
void dispose() {}
Expand All @@ -50,6 +47,14 @@ class KrakenMethodChannel {
}

Future<dynamic> _invokeMethodFromJavaScript(String method, List arguments) async {}

static void setJSMethodCallCallback(KrakenController controller) {
if (controller.methodChannel == null) return;

controller.methodChannel._onJSMethodCall = (String method, dynamic arguments) async {
controller.module.moduleManager.emitModuleEvent(METHOD_CHANNEL_NAME, data: [method, arguments]);
};
}
}

class KrakenJavaScriptChannel extends KrakenMethodChannel {
Expand Down