Skip to content

Commit

Permalink
feat(AsyncCall): new option preferLocalImplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Sep 2, 2019
1 parent face6c3 commit 6a8182f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions api-documents/kit.asynccall.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Async call between different context.
<b>Signature:</b>

```typescript
export declare function AsyncCall<OtherSideImplementedFunctions = {}>(implementation?: Record<string, (...args: any[]) => any>, options?: Partial<AsyncCallOptions>): OtherSideImplementedFunctions;
export declare function AsyncCall<OtherSideImplementedFunctions = {}>(implementation?: Partial<Record<string, (...args: any[]) => unknown>>, options?: Partial<AsyncCallOptions>): OtherSideImplementedFunctions;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| implementation | <code>Record&lt;string, (...args: any[]) =&gt; any&gt;</code> | Implementation of this side. |
| implementation | <code>Partial&lt;Record&lt;string, (...args: any[]) =&gt; unknown&gt;&gt;</code> | Implementation of this side. |
| options | <code>Partial&lt;AsyncCallOptions&gt;</code> | Define your own serializer, MessageCenter or other options. |

<b>Returns:</b>
Expand Down
1 change: 1 addition & 0 deletions api-documents/kit.asynccalloptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface AsyncCallOptions
| [log](./kit.asynccalloptions.log.md) | <code>{</code><br/><code> beCalled?: boolean;</code><br/><code> localError?: boolean;</code><br/><code> remoteError?: boolean;</code><br/><code> type?: 'basic' &#124; 'pretty';</code><br/><code> } &#124; boolean</code> | Log what to console |
| [messageChannel](./kit.asynccalloptions.messagechannel.md) | <code>{</code><br/><code> on(event: string, callback: (data: unknown) =&gt; void): void;</code><br/><code> emit(event: string, data: unknown): void;</code><br/><code> }</code> | A class that can let you transfer messages between two sides |
| [parameterStructures](./kit.asynccalloptions.parameterstructures.md) | <code>'by-position' &#124; 'by-name'</code> | How parameters passed to remote https://www.jsonrpc.org/specification\#parameter\_structures |
| [preferLocalImplementation](./kit.asynccalloptions.preferlocalimplementation.md) | <code>boolean</code> | If <code>implementation</code> has the function required, call it directly instead of send it to remote. |
| [serializer](./kit.asynccalloptions.serializer.md) | <code>Serialization</code> | How to serialization and deserialization parameters and return values |
| [strict](./kit.asynccalloptions.strict.md) | <code>{</code><br/><code> methodNotFound?: boolean;</code><br/><code> noUndefined?: boolean;</code><br/><code> unknownMessage?: boolean;</code><br/><code> } &#124; boolean</code> | Strict options |

13 changes: 13 additions & 0 deletions api-documents/kit.asynccalloptions.preferlocalimplementation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@holoflows/kit](./kit.md) &gt; [AsyncCallOptions](./kit.asynccalloptions.md) &gt; [preferLocalImplementation](./kit.asynccalloptions.preferlocalimplementation.md)

## AsyncCallOptions.preferLocalImplementation property

If `implementation` has the function required, call it directly instead of send it to remote.

<b>Signature:</b>

```typescript
preferLocalImplementation: boolean;
```
3 changes: 2 additions & 1 deletion doc/holoflows-kit.api.report.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import mitt from 'mitt';

// @public
export function AsyncCall<OtherSideImplementedFunctions = {}>(implementation?: Record<string, (...args: any[]) => any>, options?: Partial<AsyncCallOptions>): OtherSideImplementedFunctions;
export function AsyncCall<OtherSideImplementedFunctions = {}>(implementation?: Partial<Record<string, (...args: any[]) => unknown>>, options?: Partial<AsyncCallOptions>): OtherSideImplementedFunctions;

// @public
export interface AsyncCallOptions {
Expand All @@ -23,6 +23,7 @@ export interface AsyncCallOptions {
emit(event: string, data: unknown): void;
};
parameterStructures: 'by-position' | 'by-name';
preferLocalImplementation: boolean;
serializer: Serialization;
strict: {
methodNotFound?: boolean;
Expand Down
20 changes: 19 additions & 1 deletion src/util/AsyncCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,19 @@ export interface AsyncCallOptions {
* @defaultValue "by-position"
*/
parameterStructures: 'by-position' | 'by-name'
/**
* If `implementation` has the function required, call it directly instead of send it to remote.
* @defaultValue false
*/
preferLocalImplementation: boolean
}
const AsyncCallDefaultOptions = (<T extends Partial<AsyncCallOptions>>(a: T) => a)({
serializer: NoSerialization,
key: 'default-jsonrpc',
strict: false,
log: true,
parameterStructures: 'by-position',
preferLocalImplementation: false,
} as const)
/**
* Async call between different context.
Expand Down Expand Up @@ -176,7 +182,7 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
implementation: Partial<Record<string, (...args: any[]) => unknown>> = {},
options: Partial<AsyncCallOptions> = {},
): OtherSideImplementedFunctions {
const { serializer, key, strict, log, parameterStructures } = {
const { serializer, key, strict, log, parameterStructures, preferLocalImplementation } = {
...AsyncCallDefaultOptions,
...options,
}
Expand Down Expand Up @@ -325,6 +331,18 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
if (typeof method !== 'string') return Promise.reject('Only string can be keys')
if (method.startsWith('rpc.'))
return Promise.reject('You cannot call JSON RPC internal methods directly')
if (preferLocalImplementation) {
const localImpl = implementation[method]
if (localImpl) {
return new Promise((resolve, reject) => {
try {
resolve(localImpl(...params))
} catch (e) {
reject(e)
}
})
}
}
return new Promise((resolve, reject) => {
const id = Math.random()
.toString(36)
Expand Down

0 comments on commit 6a8182f

Please sign in to comment.