Skip to content

Commit

Permalink
feat: id generator, close #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Jun 19, 2020
1 parent adfb8bb commit d3c51b5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
14 changes: 7 additions & 7 deletions __tests__/__snapshots__/async-call.ts.snap
Expand Up @@ -6,7 +6,7 @@ Array [
"Receive remote call, but not implemented.",
"add2",
Object {
"id": "4fzzzxjylrx",
"id": 0.9601702866503661,
"jsonrpc": "2.0",
"method": "add2",
"params": Array [
Expand All @@ -24,7 +24,7 @@ Array [
[Error: impl error],
],
Array [
"E: impl error(-1) %c@4fzzzxjylrx
"E: impl error(-1) %c@-0.9899924966004454
%c<mocked stack>",
"color: gray",
"",
Expand All @@ -33,7 +33,7 @@ Array [
[Error: impl error],
],
Array [
"E: impl error(-1) @4fzzzxjylrx
"E: impl error(-1) @0.28366218546322625
<mocked stack>",
],
]
Expand All @@ -43,7 +43,7 @@ exports[`AsyncCall logs: normal log console.groupCollapsed 1`] = `
Array [
Array [
"undefined.%cadd%c(%o, %o%c)
%o %c@4fzzzxjylrx",
%o %c@-0.4161468365471424",
"color: #d2c057",
"",
1,
Expand All @@ -54,7 +54,7 @@ Array [
],
Array [
"undefined.%cthrows%c(%c)
%o %c@4fzzzxjylrx",
%o %c@-0.9899924966004454",
"color: #d2c057",
"",
"",
Expand All @@ -80,10 +80,10 @@ Array [
"<mocked stack>",
],
Array [
"undefined.add(1,2) @4fzzzxjylrx",
"undefined.add(1,2) @-0.6536436208636119",
],
Array [
"undefined.throws() @4fzzzxjylrx",
"undefined.throws() @0.28366218546322625",
],
]
`;
Expand Down
7 changes: 5 additions & 2 deletions __tests__/async-call.ts
Expand Up @@ -23,7 +23,8 @@ test('AsyncCall strict JSON RPC', async () => {
})

test('AsyncCall logs', async () => {
Math.random = () => 0.123456789
let i = 2
const idGen = () => Math.cos(i++)
globalThis.Error = class E extends Error {
constructor(msg: string) {
super(msg)
Expand All @@ -33,17 +34,19 @@ test('AsyncCall logs', async () => {
const snapshot = mockConsoleLog('normal log')
const s = createServer({
log: { beCalled: true, localError: true, remoteError: true, sendLocalStack: true, type: 'pretty' },
idGenerator: idGen,
})
await s.add(1, 2)
await s.throws().catch((e) => e)

const s2 = createServer({
log: { beCalled: true, localError: true, remoteError: true, sendLocalStack: true, type: 'basic' },
idGenerator: idGen,
})
await s2.add(1, 2)
await s2.throws().catch((e) => e)

const s3 = createServer({ log: true, strict: { methodNotFound: false } })
const s3 = createServer({ log: true, strict: { methodNotFound: false }, idGenerator: idGen })
// @ts-expect-error
s3.add2(1, 2)
await sleep(200)
Expand Down
17 changes: 17 additions & 0 deletions docs/async-call-rpc.asynccalloptions.idgenerator.md
@@ -0,0 +1,17 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [async-call-rpc](./async-call-rpc.md) &gt; [AsyncCallOptions](./async-call-rpc.asynccalloptions.md) &gt; [idGenerator](./async-call-rpc.asynccalloptions.idgenerator.md)

## AsyncCallOptions.idGenerator() method

The ID generator of each JSON RPC request

<b>Signature:</b>

```typescript
idGenerator?(): string | number;
```
<b>Returns:</b>
string \| number
6 changes: 6 additions & 0 deletions docs/async-call-rpc.asynccalloptions.md
Expand Up @@ -26,3 +26,9 @@ export interface AsyncCallOptions
| [serializer](./async-call-rpc.asynccalloptions.serializer.md) | [Serialization](./async-call-rpc.serialization.md) | How to serialization and deserialization JSON RPC payload |
| [strict](./async-call-rpc.asynccalloptions.strict.md) | [AsyncCallStrictJSONRPC](./async-call-rpc.asynccallstrictjsonrpc.md) \| boolean | Strict options. See [AsyncCallStrictJSONRPC](./async-call-rpc.asynccallstrictjsonrpc.md) |

## Methods

| Method | Description |
| --- | --- |
| [idGenerator()](./async-call-rpc.asynccalloptions.idgenerator.md) | The ID generator of each JSON RPC request |

1 change: 1 addition & 0 deletions etc/async-call-rpc.api.md
Expand Up @@ -20,6 +20,7 @@ export interface AsyncCallLogLevel {

// @public
export interface AsyncCallOptions {
idGenerator?(): string | number;
key?: string;
log?: AsyncCallLogLevel | boolean;
logger?: Console;
Expand Down
5 changes: 3 additions & 2 deletions src/Async-Call-Generator.ts
Expand Up @@ -83,8 +83,9 @@ export function AsyncGeneratorCall<OtherSideImplementedFunctions = {}>(
thisSideImplementation: object | Promise<object> = {},
options: AsyncCallOptions,
): _AsyncGeneratorVersionOf<OtherSideImplementedFunctions> {
const iterators = new Map<string, Iter>()
const iterators = new Map<string | number, Iter>()
const strict = normalizeStrictOptions(options.strict || false)
const { idGenerator = generateRandomID } = options
function findIterator(
id: string,
label: keyof Iter,
Expand All @@ -107,7 +108,7 @@ export function AsyncGeneratorCall<OtherSideImplementedFunctions = {}>(
else return AsyncCallIgnoreResponse
}
const iterator = iteratorGenerator(...args)
const id = generateRandomID()
const id = idGenerator()
iterators.set(id, iterator)
return Promise.resolve(id)
},
Expand Down
21 changes: 18 additions & 3 deletions src/Async-Call.ts
Expand Up @@ -171,6 +171,11 @@ export interface AsyncCallOptions {
* @defaultValue false
*/
preservePauseOnException?: boolean
/**
* The ID generator of each JSON RPC request
* @defaultValue () => Math.random().toString(36).slice(2)
*/
idGenerator?(): string | number
}

/**
Expand All @@ -193,7 +198,8 @@ const AsyncCallDefaultOptions = (<T extends Omit<Required<AsyncCallOptions>, 'me
parameterStructures: 'by-position',
preferLocalImplementation: false,
preservePauseOnException: false,
} as const)
idGenerator: generateRandomID,
})

/**
* Create a RPC server & client.
Expand All @@ -219,7 +225,16 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
): _AsyncVersionOf<OtherSideImplementedFunctions> {
let resolvedThisSideImplementation: object | undefined = undefined
Promise.resolve(thisSideImplementation).then((x) => (resolvedThisSideImplementation = x))
const { serializer, key, strict, log, parameterStructures, preferLocalImplementation, preservePauseOnException } = {
const {
serializer,
key,
strict,
log,
parameterStructures,
preferLocalImplementation,
preservePauseOnException,
idGenerator,
} = {
...AsyncCallDefaultOptions,
...options,
}
Expand Down Expand Up @@ -393,7 +408,7 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
}
}
return new Promise((resolve, reject) => {
const id = generateRandomID()
const id = idGenerator()
const [param0] = params
const sendingStack = sendLocalStack ? stack : ''
const param =
Expand Down

0 comments on commit d3c51b5

Please sign in to comment.