diff --git a/integration_tests/specs/method-channel/method-channel.ts b/integration_tests/specs/method-channel/method-channel.ts index 9e370c95c4..31ad120137 100644 --- a/integration_tests/specs/method-channel/method-channel.ts +++ b/integration_tests/specs/method-channel/method-channel.ts @@ -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 @@ -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[]) => { diff --git a/kraken/lib/src/launcher/controller.dart b/kraken/lib/src/launcher/controller.dart index 37b0510ec1..6af7c1532a 100644 --- a/kraken/lib/src/launcher/controller.dart +++ b/kraken/lib/src/launcher/controller.dart @@ -471,6 +471,8 @@ class KrakenController { } _methodChannel = methodChannel; + KrakenMethodChannel.setJSMethodCallCallback(this); + _view = KrakenViewController(viewportWidth, viewportHeight, background: background, showPerformanceOverlay: showPerformanceOverlay, diff --git a/kraken/lib/src/module/method_channel.dart b/kraken/lib/src/module/method_channel.dart index 7d4137158a..828f2890bf 100644 --- a/kraken/lib/src/module/method_channel.dart +++ b/kraken/lib/src/module/method_channel.dart @@ -15,15 +15,12 @@ Future _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() {} @@ -50,6 +47,14 @@ class KrakenMethodChannel { } Future _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 {