Skip to content

Commit

Permalink
Merge pull request #658 from openkraken/refactor/add-method-channel-h…
Browse files Browse the repository at this point in the history
…andler

♻️ refactor: rename set to add
  • Loading branch information
wssgcg1213 committed Sep 13, 2021
2 parents 701ab9f + d7a7d57 commit eb817b0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
10 changes: 10 additions & 0 deletions bridge/polyfill/src/method-channel.ts
Expand Up @@ -7,8 +7,18 @@ let methodCallHandlers: MethodCallHandler[] = [];
// Like flutter platform channels
export const methodChannel = {
setMethodCallHandler(handler: MethodCallHandler) {
console.warn('Deprecated API, use addMethodCallHandler instead.');
methodChannel.addMethodCallHandler(handler);
},
addMethodCallHandler(handler: MethodCallHandler) {
methodCallHandlers.push(handler);
},
removeMethodCallHandler(handler: MethodCallHandler) {
let index = methodCallHandlers.indexOf(handler);
if (index != -1) {
methodCallHandlers.splice(index, 1);
}
},
clearMethodCallHandler() {
methodCallHandlers.length = 0;
},
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/runtime/kraken.d.ts
@@ -1,6 +1,7 @@
type MethodHandler = (method: string, args: any[]) => void;
interface MethodChannel {
setMethodCallHandler(handler: MethodHandler): void;
addMethodCallHandler(handler: MethodHandler): void;
removeMethodCallHandler(handler: MethodHandler): void;
invokeMethod(method: string, ...args: any[]): Promise<string>
}

Expand Down
26 changes: 19 additions & 7 deletions integration_tests/specs/method-channel/method-channel.ts
@@ -1,6 +1,6 @@
describe('MethodChannel', () => {
it('setMethodCallHandler multi params', async (done) => {
kraken.methodChannel.setMethodCallHandler((method: string, args: any[]) => {
it('addMethodCallHandler multi params', async (done) => {
kraken.methodChannel.addMethodCallHandler((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();
Expand All @@ -15,8 +15,8 @@ describe('MethodChannel', () => {
expect(result).toBe('method: helloworld');
});

it('setMethodHandler', async (done) => {
kraken.methodChannel.setMethodCallHandler((method: string, args: any[]) => {
it('addMethodCallHandler', async (done) => {
kraken.methodChannel.addMethodCallHandler((method: string, args: any[]) => {
expect(method).toBe('helloworld');
expect(args).toEqual(['abc']);
done();
Expand All @@ -25,17 +25,29 @@ describe('MethodChannel', () => {
expect(result).toBe('method: helloworld');
});

it('setMethodCallHandler multi params with multi handler', async (done) => {

it('removeMethodCallHandler', async (done: DoneFn) => {
var handler = (method: string, args: any[]) => {
done.fail('should not execute here.');
};
kraken.methodChannel.addMethodCallHandler(handler);
kraken.methodChannel.removeMethodCallHandler(handler);
let result = await kraken.methodChannel.invokeMethod('helloworld', 'abc');
expect(result).toBe('method: helloworld');
done();
});

it('addMethodCallHandler multi params with multi handler', async (done) => {
let handlerCount = 0;
kraken.methodChannel.setMethodCallHandler((method: string, args: any[]) => {
kraken.methodChannel.addMethodCallHandler((method: string, args: any[]) => {
handlerCount++;
expect(method).toBe('helloworld');
expect(args).toEqual(['abc', 1234, null, /* undefined will be converted to */ null, [], true, false, {name: 1}]);
if(handlerCount == 2) {
done();
}
});
kraken.methodChannel.setMethodCallHandler((method: string, args: any[]) => {
kraken.methodChannel.addMethodCallHandler((method: string, args: any[]) => {
handlerCount++;
expect(method).toBe('helloworld');
expect(args).toEqual(['abc', 1234, null, /* undefined will be converted to */ null, [], true, false, {name: 1}]);
Expand Down

0 comments on commit eb817b0

Please sign in to comment.