-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nestjs-json-rpc-sdk): add takeUntil for disconnect socket
- Loading branch information
Showing
14 changed files
with
389 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/angular/factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { inject, InjectionToken } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Subject } from 'rxjs'; | ||
import { WebSocketSubject } from 'rxjs/internal/observable/dom/WebSocketSubject'; | ||
import { Socket } from 'socket.io-client'; | ||
|
||
import { | ||
LoopFunc, | ||
PayloadRpc, | ||
RpcResult, | ||
RpcReturnList, | ||
RpcConfig, | ||
TransportType, | ||
} from '../types'; | ||
import { transportFactory } from '../factory'; | ||
import { webSocketFactory, WsResponse } from '../factory/ws-transport.factory'; | ||
|
||
import { JSON_RPC_SDK_CONFIG, JSON_RPC_SDK_TRANSPORT } from './tokens'; | ||
import { RpcBatchFactory, rpcProxy } from '../utils'; | ||
|
||
export function rpcBatchFactory() { | ||
return RpcBatchFactory(inject(JSON_RPC_SDK_TRANSPORT)); | ||
} | ||
|
||
export function rpcFactory() { | ||
return rpcProxy<RpcReturnList<any, true>>( | ||
inject(JSON_RPC_SDK_TRANSPORT), | ||
false | ||
); | ||
} | ||
|
||
export function angularTransportFactory() { | ||
const angularConfig = inject(JSON_RPC_SDK_CONFIG); | ||
const httpClient = inject(HttpClient); | ||
|
||
if (angularConfig.transport === TransportType.HTTP) { | ||
const rpcConfig: RpcConfig = { | ||
transport: angularConfig.transport, | ||
httpAgentFactory: (url: string) => (body: PayloadRpc<LoopFunc>) => | ||
httpClient.post<RpcResult<LoopFunc>>(url, body), | ||
rpcPath: angularConfig.rpcPath, | ||
rpcHost: angularConfig.rpcHost, | ||
}; | ||
return transportFactory(rpcConfig); | ||
} | ||
|
||
const destroySubject = | ||
(angularConfig.destroySubjectToken && | ||
inject<Subject<boolean>>(angularConfig.destroySubjectToken, { | ||
optional: true, | ||
})) || | ||
new Subject<boolean>(); | ||
|
||
if (angularConfig.useWsNativeSocket) { | ||
let socketInst: | ||
| WebSocketSubject<WsResponse<PayloadRpc<LoopFunc> | RpcResult<LoopFunc>>> | ||
| undefined = undefined; | ||
if ('tokenSocketInst' in angularConfig) { | ||
socketInst = | ||
inject< | ||
WebSocketSubject< | ||
WsResponse<PayloadRpc<LoopFunc> | RpcResult<LoopFunc>> | ||
> | ||
>(angularConfig['tokenSocketInst'], { optional: true }) || undefined; | ||
} else { | ||
const url = new URL( | ||
angularConfig.rpcPath, | ||
angularConfig.rpcHost | ||
).toString(); | ||
socketInst = webSocketFactory( | ||
url, | ||
angularConfig.nativeSocketImplementation | ||
); | ||
} | ||
|
||
if (socketInst === undefined) throw new Error('Cant create socket inst'); | ||
const rpcConfig: RpcConfig = { | ||
transport: angularConfig.transport, | ||
useWsNativeSocket: angularConfig.useWsNativeSocket, | ||
nativeSocketInstance: socketInst, | ||
destroySubject, | ||
}; | ||
|
||
return transportFactory(rpcConfig); | ||
} | ||
const ioSocketInstance = inject<Socket>(angularConfig['tokenSocketInst']); | ||
const rpcConfig: RpcConfig = { | ||
transport: angularConfig.transport, | ||
useWsNativeSocket: angularConfig.useWsNativeSocket, | ||
ioSocketInstance: ioSocketInstance, | ||
destroySubject, | ||
}; | ||
return transportFactory(rpcConfig); | ||
} |
24 changes: 24 additions & 0 deletions
24
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/angular/json-rpc-angular.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ModuleWithProviders, NgModule } from '@angular/core'; | ||
import { HttpClientModule } from '@angular/common/http'; | ||
|
||
import { JSON_RPC_SDK_CONFIG } from './tokens'; | ||
import { JsonRpcAngularConfig } from '../types'; | ||
|
||
@NgModule({ | ||
imports: [HttpClientModule], | ||
}) | ||
export class JsonRpcAngular { | ||
static forRoot( | ||
config: JsonRpcAngularConfig | ||
): ModuleWithProviders<JsonRpcAngular> { | ||
return { | ||
ngModule: JsonRpcAngular, | ||
providers: [ | ||
{ | ||
useValue: config, | ||
provide: JSON_RPC_SDK_CONFIG, | ||
}, | ||
], | ||
}; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/angular/tokens.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { LoopFunc, RpcBatch, RpcReturnList, Transport } from '../types'; | ||
|
||
import { JsonRpcAngularConfig } from '@klerick/nestjs-json-rpc-sdk/json-rpc-sdk.module'; | ||
import { | ||
angularTransportFactory, | ||
rpcBatchFactory, | ||
rpcFactory, | ||
} from './factory'; | ||
|
||
export const JSON_RPC_SDK_CONFIG = new InjectionToken<JsonRpcAngularConfig>( | ||
'Main config object for sdk' | ||
); | ||
|
||
export const JSON_RPC_SDK_TRANSPORT = new InjectionToken<Transport<LoopFunc>>( | ||
'Transport for RPC', | ||
{ | ||
factory: angularTransportFactory, | ||
} | ||
); | ||
|
||
export const JSON_RPC = new InjectionToken<RpcReturnList<object, false>>( | ||
'Rpc client', | ||
{ | ||
factory: rpcFactory, | ||
} | ||
); | ||
|
||
export const RPC_BATCH = new InjectionToken<RpcBatch>('Rpc client for batch', { | ||
factory: rpcBatchFactory, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.