Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit fd3da54

Browse files
committed
perf(service): remove onAny function
1 parent 53bb00b commit fd3da54

File tree

2 files changed

+5
-29
lines changed

2 files changed

+5
-29
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ export class YourModule { }
136136
| | .disconnect() | [https://socket.io/docs/v4/client-api/#socketdisconnect](https://socket.io/docs/v4/client-api/#socketdisconnect) |
137137
| | .send([...args][, ack]) | [https://socket.io/docs/v4/client-api/#socketsendargs](https://socket.io/docs/v4/client-api/#socketsendargs) |
138138
| | .emit(eventName[, ...args][, ack]) | [https://socket.io/docs/v4/client-api/#socketemiteventname-args](https://socket.io/docs/v4/client-api/#socketemiteventname-args) |
139-
| | .on() | [https://socket.io/docs/v4/client-api/#socketonanycallback](https://socket.io/docs/v4/client-api/#socketonanycallback) |
140139
| | .on(eventName) | [https://socket.io/docs/v4/client-api/#socketoneventname-callback](https://socket.io/docs/v4/client-api/#socketoneventname-callback) |
141140
| | .once(eventName) | Similar to `.on(eventName)`, but only responds once. |
142141
| | .compress(compress) | [https://socket.io/docs/v4/client-api/#socketcompressvalue](https://socket.io/docs/v4/client-api/#socketcompressvalue) |

projects/ngx-socketio2/src/lib/socketio.service.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Inject, Injectable, OnDestroy } from '@angular/core';
2-
import { Observable, Subject } from 'rxjs';
3-
import { filter, map, take } from 'rxjs/operators';
2+
import { fromEvent, Observable } from 'rxjs';
3+
import { FromEventTarget } from 'rxjs/internal/observable/fromEvent';
4+
import { take } from 'rxjs/operators';
45
import { io, Manager, Socket } from 'socket.io-client';
56
import { SocketioConfig } from './socketio.interface';
67
import { SOCKETIO_CONFIG } from './socketio.token';
78

89
@Injectable()
910
export class Socketio implements OnDestroy {
10-
private subject: Subject<{ eventName: string, args: any[] }> = new Subject();
1111
private socket: Socket;
1212

1313
/**
@@ -40,14 +40,10 @@ export class Socketio implements OnDestroy {
4040
@Inject(SOCKETIO_CONFIG) { url, options }: SocketioConfig
4141
) {
4242
this.socket = io(url, options);
43-
this.socket.onAny((eventName: string, ...args: any[]) => {
44-
this.subject.next({ eventName, args });
45-
});
4643
}
4744

4845
ngOnDestroy(): void {
4946
this.socket.offAny();
50-
this.subject.complete();
5147
}
5248

5349
/**
@@ -82,30 +78,11 @@ export class Socketio implements OnDestroy {
8278
return this;
8379
}
8480

85-
/**
86-
* @see {@link Socket.onAny}
87-
*/
88-
on<T>(): Observable<{ eventName: string, args: T }>
8981
/**
9082
* @see {@link Socket.on}
9183
*/
92-
on<T>(eventName?: string): Observable<T>
93-
on<T extends any>(eventName?: string): Observable<T> | Observable<{ eventName: string, args: T }> {
94-
const observable = this.subject.asObservable();
95-
96-
if (!eventName) {
97-
return observable.pipe(
98-
map(({ eventName, args }) => ({
99-
eventName,
100-
args: (args.length === 1 ? args[0] : args) as T
101-
}))
102-
);
103-
}
104-
105-
return observable.pipe(
106-
filter(o => o.eventName === eventName),
107-
map(({ args }) => (args.length === 1 ? args[0] : args) as T)
108-
);
84+
on<T>(eventName: string): Observable<T> {
85+
return fromEvent<T>(this.socket as FromEventTarget<T>, eventName);
10986
}
11087

11188
/**

0 commit comments

Comments
 (0)