### File

`src/lib/socket-io.service.ts`

### Index

###### Properties
eventObservables$ ioSocket subscribersCounter
###### Methods
connect disconnect emit fromEvent fromOneTimeEvent of on once removeAllListeners removeListener

### Constructor

`constructor(url: string, options: SocketIoOptions)`
Defined in src/lib/socket-io.service.ts:21
Parameters :
Name | Type | Optional
url | `string` | No
options | `SocketIoOptions` | No

### Methods

connect
`connect()`
Defined in src/lib/socket-io.service.ts:46
Returns : `any`

disconnect
`disconnect(_close?: any)`
Defined in src/lib/socket-io.service.ts:50
Parameters :
Name | Type | Optional
_close | `any` | Yes
Returns : `any`

emit
`emit(_eventName: string, ..._args: any[])`
Defined in src/lib/socket-io.service.ts:54
Parameters :
Name | Type | Optional
_eventName | `string` | No
_args | `any[]` | No
Returns : `any`

fromEvent
`fromEvent(eventName: string)`
Defined in src/lib/socket-io.service.ts:66
Type parameters : T
Parameters :
Name | Type | Optional
eventName | `string` | No
Returns : `Observable<T>`

fromOneTimeEvent
`fromOneTimeEvent(eventName: string)`
Defined in src/lib/socket-io.service.ts:90
Type parameters : T
Parameters :
Name | Type | Optional
eventName | `string` | No
Returns : `Promise<T>`

of
`of(namespace: string)`
Defined in src/lib/socket-io.service.ts:34
Parameters :
Name | Type | Optional
namespace | `string` | No
Returns : `void`

on
`on(eventName: string, callback: (data: T) => void)`
Defined in src/lib/socket-io.service.ts:38
Type parameters : T
Parameters :
Name | Type | Optional
eventName | `string` | No
callback | `function` | No
Returns : `void`

once
`once(eventName: string, callback: (data: T) => void)`
Defined in src/lib/socket-io.service.ts:42
Type parameters : T
Parameters :
Name | Type | Optional
eventName | `string` | No
callback | `function` | No
Returns : `void`

removeAllListeners
`removeAllListeners(_eventName?: string)`
Defined in src/lib/socket-io.service.ts:62
Parameters :
Name | Type | Optional
_eventName | `string` | Yes
Returns : `any`

removeListener
`removeListener(_eventName: string, _callback?: () => void)`
Defined in src/lib/socket-io.service.ts:58
Parameters :
Name | Type | Optional
_eventName | `string` | No
_callback | `function` | Yes
Returns : `any`

### Properties

eventObservables$
Type : `Record<string | Observable<any>>`
Default value : `{}`
Defined in src/lib/socket-io.service.ts:20

ioSocket
Type : `any`
Defined in src/lib/socket-io.service.ts:21

subscribersCounter
Type : `Record<string | number>`
Default value : `{}`
Defined in src/lib/socket-io.service.ts:19

    import {
      Inject,
      Injectable,
      Optional,
    } from '@angular/core';
    import { Observable } from 'rxjs';
    import { share } from 'rxjs/operators';

    import * as io from 'socket.io-client';

    import { SocketIoOptions } from './socket-io.config';
    import {
      RXAP_SOCKET_IO_OPTIONS,
      RXAP_SOCKET_IO_URL,
    } from './tokens';

    @Injectable()
    export class WrappedSocket {
      subscribersCounter: Record<string, number> = {};
      eventObservables$: Record<string, Observable<any>> = {};
      ioSocket: any;

      constructor(
        @Inject(RXAP_SOCKET_IO_URL)
        url: string,
        @Optional()
        @Inject(RXAP_SOCKET_IO_OPTIONS)
        options: SocketIoOptions = {},
      ) {
        const ioFunc = (io as any).default ? (io as any).default : io;
        this.ioSocket = ioFunc(url, options);
      }

      of(namespace: string) {
        this.ioSocket.of(namespace);
      }

      on<T>(eventName: string, callback: (data: T) => void) {
        this.ioSocket.on(eventName, callback);
      }

      once<T>(eventName: string, callback: (data: T) => void) {
        this.ioSocket.once(eventName, callback);
      }

      connect() {
        return this.ioSocket.connect();
      }

      disconnect(_close?: any) {
        return this.ioSocket.disconnect.apply(this.ioSocket, [ _close ]);
      }

      emit(_eventName: string, ..._args: any[]) {
        return this.ioSocket.emit.apply(this.ioSocket, [ _eventName, ..._args ]);
      }

      removeListener(_eventName: string, _callback?: () => void) {
        return this.ioSocket.removeListener.apply(this.ioSocket, [ _eventName, _callback ]);
      }

      removeAllListeners(_eventName?: string) {
        return this.ioSocket.removeAllListeners.apply(this.ioSocket, [ _eventName ]);
      }

      fromEvent<T>(eventName: string): Observable<T> {
        if (!this.subscribersCounter[eventName]) {
          this.subscribersCounter[eventName] = 0;
        }
        this.subscribersCounter[eventName]++;

        if (!this.eventObservables$[eventName]) {
          this.eventObservables$[eventName] = new Observable((observer: any) => {
            const listener = (data: T) => {
              observer.next(data);
            };
            this.ioSocket.on(eventName, listener);
            return () => {
              this.subscribersCounter[eventName]--;
              if (this.subscribersCounter[eventName] === 0) {
                this.ioSocket.removeListener(eventName, listener);
                delete this.eventObservables$[eventName];
              }
            };
          }).pipe(share());
        }
        return this.eventObservables$[eventName];
      }

      fromOneTimeEvent<T>(eventName: string): Promise<T> {
        return new Promise<T>(resolve => this.once(eventName, resolve));
      }
    }

# results matching ""

# No results matching ""