From d3c51b59a7876bd0f14a76d8ee40a8dade5c65f2 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Fri, 19 Jun 2020 12:53:42 +0800 Subject: [PATCH] feat: id generator, close #13 --- __tests__/__snapshots__/async-call.ts.snap | 14 ++++++------- __tests__/async-call.ts | 7 +++++-- ...c-call-rpc.asynccalloptions.idgenerator.md | 17 +++++++++++++++ docs/async-call-rpc.asynccalloptions.md | 6 ++++++ etc/async-call-rpc.api.md | 1 + src/Async-Call-Generator.ts | 5 +++-- src/Async-Call.ts | 21 ++++++++++++++++--- 7 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 docs/async-call-rpc.asynccalloptions.idgenerator.md diff --git a/__tests__/__snapshots__/async-call.ts.snap b/__tests__/__snapshots__/async-call.ts.snap index 26be9df..80b7a7b 100644 --- a/__tests__/__snapshots__/async-call.ts.snap +++ b/__tests__/__snapshots__/async-call.ts.snap @@ -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 [ @@ -24,7 +24,7 @@ Array [ [Error: impl error], ], Array [ - "E: impl error(-1) %c@4fzzzxjylrx + "E: impl error(-1) %c@-0.9899924966004454 %c", "color: gray", "", @@ -33,7 +33,7 @@ Array [ [Error: impl error], ], Array [ - "E: impl error(-1) @4fzzzxjylrx + "E: impl error(-1) @0.28366218546322625 ", ], ] @@ -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, @@ -54,7 +54,7 @@ Array [ ], Array [ "undefined.%cthrows%c(%c) -%o %c@4fzzzxjylrx", +%o %c@-0.9899924966004454", "color: #d2c057", "", "", @@ -80,10 +80,10 @@ Array [ "", ], Array [ - "undefined.add(1,2) @4fzzzxjylrx", + "undefined.add(1,2) @-0.6536436208636119", ], Array [ - "undefined.throws() @4fzzzxjylrx", + "undefined.throws() @0.28366218546322625", ], ] `; diff --git a/__tests__/async-call.ts b/__tests__/async-call.ts index 357653b..6d6ef36 100644 --- a/__tests__/async-call.ts +++ b/__tests__/async-call.ts @@ -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) @@ -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) diff --git a/docs/async-call-rpc.asynccalloptions.idgenerator.md b/docs/async-call-rpc.asynccalloptions.idgenerator.md new file mode 100644 index 0000000..4b88131 --- /dev/null +++ b/docs/async-call-rpc.asynccalloptions.idgenerator.md @@ -0,0 +1,17 @@ + + +[Home](./index.md) > [async-call-rpc](./async-call-rpc.md) > [AsyncCallOptions](./async-call-rpc.asynccalloptions.md) > [idGenerator](./async-call-rpc.asynccalloptions.idgenerator.md) + +## AsyncCallOptions.idGenerator() method + +The ID generator of each JSON RPC request + +Signature: + +```typescript +idGenerator?(): string | number; +``` +Returns: + +string \| number + diff --git a/docs/async-call-rpc.asynccalloptions.md b/docs/async-call-rpc.asynccalloptions.md index 4da74c6..9219163 100644 --- a/docs/async-call-rpc.asynccalloptions.md +++ b/docs/async-call-rpc.asynccalloptions.md @@ -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 | + diff --git a/etc/async-call-rpc.api.md b/etc/async-call-rpc.api.md index fb04c73..baab83b 100644 --- a/etc/async-call-rpc.api.md +++ b/etc/async-call-rpc.api.md @@ -20,6 +20,7 @@ export interface AsyncCallLogLevel { // @public export interface AsyncCallOptions { + idGenerator?(): string | number; key?: string; log?: AsyncCallLogLevel | boolean; logger?: Console; diff --git a/src/Async-Call-Generator.ts b/src/Async-Call-Generator.ts index b9036f3..5926f58 100644 --- a/src/Async-Call-Generator.ts +++ b/src/Async-Call-Generator.ts @@ -83,8 +83,9 @@ export function AsyncGeneratorCall( thisSideImplementation: object | Promise = {}, options: AsyncCallOptions, ): _AsyncGeneratorVersionOf { - const iterators = new Map() + const iterators = new Map() const strict = normalizeStrictOptions(options.strict || false) + const { idGenerator = generateRandomID } = options function findIterator( id: string, label: keyof Iter, @@ -107,7 +108,7 @@ export function AsyncGeneratorCall( else return AsyncCallIgnoreResponse } const iterator = iteratorGenerator(...args) - const id = generateRandomID() + const id = idGenerator() iterators.set(id, iterator) return Promise.resolve(id) }, diff --git a/src/Async-Call.ts b/src/Async-Call.ts index a5aa566..850495a 100644 --- a/src/Async-Call.ts +++ b/src/Async-Call.ts @@ -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 } /** @@ -193,7 +198,8 @@ const AsyncCallDefaultOptions = (, 'me parameterStructures: 'by-position', preferLocalImplementation: false, preservePauseOnException: false, -} as const) + idGenerator: generateRandomID, +}) /** * Create a RPC server & client. @@ -219,7 +225,16 @@ export function AsyncCall( ): _AsyncVersionOf { 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, } @@ -393,7 +408,7 @@ export function AsyncCall( } } return new Promise((resolve, reject) => { - const id = generateRandomID() + const id = idGenerator() const [param0] = params const sendingStack = sendLocalStack ? stack : '' const param =