Skip to content

Commit

Permalink
chore(requestable): test work.
Browse files Browse the repository at this point in the history
  • Loading branch information
radiorz committed Aug 2, 2024
1 parent f1481b4 commit 0c3e78f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/requestable/lib/Emitter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Callback {
(message: any): void;
(topic: string, message: any): void;
}
export interface Emitter {
on(topic: string, callback: Callback): void;
Expand Down
7 changes: 5 additions & 2 deletions packages/requestable/lib/Requestable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ export class Requestable implements Peer {
}
this.options.emitter.on(this.options.protocol.getWatchResponseTopic(this), this.onMessage.bind(this));
}
private onMessage(message: any) {
// console.log(`message`, message);
private onMessage(topic: string, message: any) {
// console.log(`message`, topic, message);
if (!this.options.protocol.isResponseTopic(topic)) {
return;
}
if (!this.options.protocol.isResponseMessage(message)) {
return;
}
Expand Down
11 changes: 7 additions & 4 deletions packages/requestable/lib/Responsive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export class Responsive implements Peer {
return;
}
// 开启监听
this.options.emitter.on(this.options.protocol.getWatchRequestTopic(this), (data: any) => {
this.onMessage(data);
this.options.emitter.on(this.options.protocol.getWatchRequestTopic(this), (topic: string, message: any) => {
this.onMessage(topic, message);
});
}
private routes = new Map<string, Handler>();
Expand All @@ -58,8 +58,11 @@ export class Responsive implements Peer {
}

// 监听
private async onMessage(message: RequestMessage) {
// console.log(`onRequest`, request);
private async onMessage(topic: string, message: RequestMessage) {
// console.log(`onMessage`, topic,message);
if (!this.options.protocol.isRequestTopic(topic)) {
return;
}
if (!this.options.protocol.isRequestMessage(message)) {
return;
}
Expand Down
29 changes: 10 additions & 19 deletions packages/requestable/lib/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { it, expect } from 'vitest';
import { Message, Requestable, Responsive } from './index';
import { Requestable, Responsive } from './index';
import { Emitter } from './index';
import { faker } from '@faker-js/faker';
it('request response work', async () => {
// 模拟一下
function getEmitter() {
const emitter: Emitter = {
callbacks: [] as any[],
on(topic: string, callback: any) {
Expand All @@ -12,12 +11,17 @@ it('request response work', async () => {
off(callback: any) {
this.callbacks = this.callbacks.filter((c: any) => c === callback);
},
emit(topic: string, message: Message) {
emit(topic: string, message: any) {
this.callbacks.forEach((callback: any) => {
callback(message);
callback(topic, message);
});
},
};
return emitter;
}
it('request response work', async () => {
// 模拟一下
const emitter = getEmitter();
const responsive = new Responsive({ emitter: emitter });
function handler(data: any) {
return data;
Expand All @@ -40,20 +44,7 @@ it('request response work', async () => {
});
it('response 404', async () => {
// 模拟一下
const emitter = {
callbacks: [] as any[],
on(topic: string, callback: any) {
this.callbacks.push(callback);
},
off(callback: any) {
this.callbacks = this.callbacks.filter((c) => c === callback);
},
emit(topic: string, message: Message) {
this.callbacks.forEach((callback) => {
callback(message);
});
},
};
const emitter = getEmitter();
const responsive = new Responsive({ emitter: emitter });
responsive.init();
const requestable = new Requestable({
Expand Down
14 changes: 7 additions & 7 deletions packages/requestable/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Requestable, Responsive } from '../lib';
import { Emitter, Callback, Requestable, Responsive } from '../lib';

// 模拟一下
const emitter = {
callbacks: [] as any[],
on(topic: string, callback: any) {
const emitter: Emitter = {
callbacks: [] as Callback[],
on(topic: string, callback: Callback) {
this.callbacks.push(callback);
},
off(callback: any) {
this.callbacks = this.callbacks.filter((c) => c === callback);
this.callbacks = this.callbacks.filter((c: Callback) => c === callback);
},
emit(topic: string, message: any) {
this.callbacks.forEach((callback) => {
callback(message);
this.callbacks.forEach((callback: Callback) => {
callback(topic, message);
});
},
};
Expand Down

0 comments on commit 0c3e78f

Please sign in to comment.