Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Prev Previous commit
Next Next commit
Fix bug in light.js, dependsOn and frequency
  • Loading branch information
amaury1093 committed Nov 22, 2018
commit add8558f7d1a2f72994adeb79560c20d64d41eab
20 changes: 18 additions & 2 deletions packages/light.js/src/rpc/utils/createRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { merge, Observable, OperatorFunction } from 'rxjs';

import { createApiFromProvider, getApi } from '../../api';
import { distinctReplayRefCount } from '../../utils/operators';
import { Metadata, RpcObservableOptions } from '../../types';
import {
FrequencyObservable,
Metadata,
RpcObservableOptions
} from '../../types';

/**
* Add metadata to an RpcObservable, and transform it into a ReplaySubject(1).
Expand All @@ -25,12 +29,24 @@ import { Metadata, RpcObservableOptions } from '../../types';
*/
const createRpcWithApi = memoizeeWeak(
<Source, Out>(api: any, metadata: Metadata<Source, Out>, ...args: any[]) => {
if (!metadata.dependsOn && !metadata.frequency) {
throw new Error(
`Rpc$ '${
metadata.name
}' needs either a 'dependsOn' or a 'frequency' field.`
);
}

// The source Observable can either be another RpcObservable (in the
// `dependsOn` field), or anObservable built by merging all the
// FrequencyObservables
const source$ = metadata.dependsOn
? metadata.dependsOn(...args, { provider: api.provider })
: merge(...metadata.frequency.map(f => f({ provider: api.provider })));
: merge(
...(metadata.frequency as FrequencyObservable<Source>[]).map(f =>
f({ provider: api.provider })
)
);

// The pipes to add
const pipes: OperatorFunction<any, any>[] = [];
Expand Down