rxprotoplex-rpc
Advanced tools
Comparing version 1.0.1 to 1.0.2
export * from "./lib/listenAndConnectionAndRpc$.js"; | ||
export * from "./lib/connectAndRpc$.js"; | ||
export * from "./lib/connectAndRpc$.js"; | ||
export * from "./lib/tapNotify.js"; | ||
export * from "./lib/switchRpcRequest.js"; | ||
export * from "./lib/tapExpose.js"; |
{ | ||
"name": "rxprotoplex-rpc", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A library for managing RPC connections using RxJS and Protoplex.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,8 +6,10 @@ # rxprotoplex-rpc | ||
Additionally, the module provides utility operators like `switchRpcRequest`, `tapNotify`, and `tapExpose` for handling RPC requests, notifications, and exposing methods within an observable stream. | ||
## Usage | ||
### Importing the Functions | ||
To use `connectAndRpc$` and `listenAndConnectionAndRpc$`, make sure to import them from your module: | ||
To use `connectAndRpc$`, `listenAndConnectionAndRpc$`, `switchRpcRequest`, `tapNotify`, and `tapExpose`, make sure to import them from your module: | ||
```javascript | ||
import { connectAndRpc$, listenAndConnectionAndRpc$ } from 'rxprotoplex-rpc'; | ||
import { connectAndRpc$, listenAndConnectionAndRpc$, switchRpcRequest, tapNotify, tapExpose } from 'rxprotoplex-rpc'; | ||
``` | ||
@@ -65,2 +67,86 @@ | ||
### `switchRpcRequest` | ||
#### Description | ||
An RxJS operator that switches to a new observable for each emission and makes an RPC request using the specified method and arguments. | ||
#### Syntax | ||
```javascript | ||
observable$.pipe(switchRpcRequest(method, ...args)); | ||
``` | ||
#### Parameters | ||
- **`method`** (string): The method name to be called on the RPC request object. | ||
- **`args`** (...any): Additional arguments to be passed to the method. | ||
#### Returns | ||
- **OperatorFunction**: An RxJS operator that maps the input observable to an observable that makes an RPC request and emits an object containing the RPC instance and acknowledgment response. | ||
#### Example | ||
```javascript | ||
const [p1, p2] = createPlexPair(); | ||
const rpcClient = connectAndRpc$(p2).pipe( | ||
switchRpcRequest('add', 5, 6), | ||
tap(({ rpc }) => rpc.stream.destroy()) | ||
).subscribe( | ||
({ ack: sum }) => { | ||
console.log('Sum:', sum); // Output: Sum: 11 | ||
} | ||
); | ||
``` | ||
### `tapNotify` | ||
#### Description | ||
An RxJS `tap` operator that calls a notification method on the RPC object as a side-effect. | ||
#### Syntax | ||
```javascript | ||
observable$.pipe(tapNotify(methodName, ...args)); | ||
``` | ||
#### Parameters | ||
- **`methodName`** (string): The name of the notification method to be called on the RPC object. | ||
- **`args`** (...any): Additional arguments to be passed to the notification method. | ||
#### Returns | ||
- **OperatorFunction**: An RxJS `tap` operator that performs a side-effect by calling the specified notification method with the provided arguments. | ||
#### Example | ||
```javascript | ||
observable$.pipe( | ||
tapNotify('notifyMethod', param1) | ||
).subscribe(); | ||
``` | ||
### `tapExpose` | ||
#### Description | ||
An RxJS `tap` operator that exposes an object containing RPC methods on the RPC instance. | ||
#### Syntax | ||
```javascript | ||
observable$.pipe(tapExpose(exposeObject)); | ||
``` | ||
#### Parameters | ||
- **`exposeObject`** (Object): The object containing RPC methods to be exposed on the RPC instance. | ||
#### Returns | ||
- **OperatorFunction**: An RxJS `tap` operator that performs a side-effect by calling the `expose` method on the RPC instance with the provided object. | ||
#### Example | ||
```javascript | ||
const [p1, p2] = createPlexPair(); | ||
const rpcServer = listenAndConnectionAndRpc$(p1).pipe( | ||
tapExpose({ | ||
add(a, b) { | ||
return a + b; | ||
} | ||
}) | ||
).subscribe(); | ||
``` | ||
## Constants | ||
@@ -78,2 +164,1 @@ ### `CHANNEL` | ||
For more details on how to extend or modify these functions, refer to the code comments or inline documentation. | ||
40
test.js
@@ -6,2 +6,4 @@ import {test} from "brittle"; | ||
import {finalize, from, map, switchMap, tap} from "rxjs"; | ||
import {switchRpcRequest} from "./lib/switchRpcRequest.js"; | ||
import {tapExpose} from "./lib/tapExpose.js"; | ||
@@ -16,31 +18,21 @@ test("add two numbers over rpc", t => { | ||
const rpcServer = listenAndConnectionAndRpc$(p1) | ||
.pipe(finalize(() => t.pass("rpc server completes."))) | ||
.subscribe( | ||
{ | ||
next: rpc => { | ||
rpc.expose({ | ||
add(a,b) { | ||
return a+b | ||
} | ||
}) | ||
.pipe( | ||
finalize(() => t.pass("rpc server completes.")), | ||
tapExpose({ | ||
add(a, b) { | ||
return a + b; | ||
} | ||
} | ||
); | ||
}) | ||
) | ||
.subscribe(); | ||
const rpcClient = connectAndRpc$(p2).pipe( | ||
finalize(() => t.pass("rpc client completes.")), | ||
switchMap(rpc => | ||
from(rpc.request.add(5, 6)) | ||
.pipe( | ||
tap(() => rpc.stream.destroy()), | ||
map((sum) => ({rpc, sum})) | ||
) | ||
) | ||
switchRpcRequest("add", 5, 6), | ||
tap(({rpc}) => rpc.stream.destroy()) | ||
).subscribe( | ||
{ | ||
next: ({sum, rpc}) => { | ||
t.is(sum, 11); | ||
t.ok(rpc.stream.destroying || rpc.stream.destroyed); | ||
fin(); | ||
} | ||
({ack: sum, rpc}) => { | ||
t.is(sum, 11); | ||
t.ok(rpc.stream.destroying || rpc.stream.destroyed); | ||
fin(); | ||
} | ||
@@ -47,0 +39,0 @@ ); |
14429
13
154
162