Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8a59985
FrequencyObservable as a function returning a observable
amaury1093 Sep 7, 2018
f113991
Fix tests
amaury1093 Sep 7, 2018
5e613d4
Test a new api
amaury1093 Sep 12, 2018
9ecae21
Make frequency observables work
amaury1093 Sep 13, 2018
660c5ef
Fix tests
amaury1093 Sep 13, 2018
6e99b67
Remove overview
amaury1093 Sep 13, 2018
e4298fa
Don't cover index.ts
amaury1093 Sep 13, 2018
bb5509c
Fix a lot of stuff
amaury1093 Sep 13, 2018
1008d5a
Remove useless import
amaury1093 Sep 13, 2018
3aa360b
Change testRegex for jest
amaury1093 Sep 13, 2018
ea75324
Add ambient for packages with no typigns
amaury1093 Sep 13, 2018
7c03c5c
Generate docs
amaury1093 Sep 13, 2018
80b283f
Remove NullProvider
amaury1093 Sep 13, 2018
a46b19a
Remove NullProvider
amaury1093 Sep 13, 2018
3447e95
Regenerate docs
amaury1093 Sep 13, 2018
1f0c25a
Silent jest on CI
amaury1093 Sep 13, 2018
b4404e2
Fix makeContract and post
amaury1093 Sep 13, 2018
165df16
Regen docs
amaury1093 Sep 13, 2018
9b8edd8
Update summary
amaury1093 Sep 13, 2018
fb89a94
Update ambient
amaury1093 Sep 13, 2018
439d3bd
Export post$ too
amaury1093 Sep 13, 2018
18e640f
Fix makeContract
amaury1093 Sep 13, 2018
431fe51
Fix memoization for frequency observables
amaury1093 Sep 17, 2018
44dde4a
Don't lint lib
amaury1093 Sep 17, 2018
15caa62
Fix bugs with rpc$
amaury1093 Sep 17, 2018
0cbe22a
Remove useless packages
amaury1093 Sep 17, 2018
85f8fb9
Generate docs
amaury1093 Sep 17, 2018
c5e0c56
Update withoutLoading syntax
amaury1093 Sep 17, 2018
82fca45
Use json.stringify for normalizer
amaury1093 Sep 17, 2018
4893e97
Fix bug normalizer
amaury1093 Sep 17, 2018
e449a57
Remove withApi in docs
amaury1093 Sep 17, 2018
3a885fe
Fix bug memoization
amaury1093 Sep 17, 2018
c6ee32e
Remove onEvery2Blocks
amaury1093 Sep 17, 2018
041441b
Options then args
amaury1093 Sep 17, 2018
8a066db
CreateRpc in makeContract fix
amaury1093 Sep 17, 2018
d2bb3c7
Fix getContract memoization
amaury1093 Sep 17, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix a lot of stuff
  • Loading branch information
amaury1093 committed Sep 13, 2018
commit bb5509c623919087dd21c4fc296135c6271091c5
26 changes: 4 additions & 22 deletions packages/light.js/example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,10 @@ import React from 'react';
import ReactDOM from 'react-dom';
import 'symbol-observable'; // TODO Remove this once https://github.com/acdlite/recompose/pull/660 is merged

// import App from './App';
import light, { balanceOf$, frequency } from './light.js';
import provider, { localProvider } from './provider';

const api = new Api(localProvider);
import App from './App';
import light from './light.js';
import provider from './provider';

light.setProvider(provider);
const a = balanceOf$('0x73e41c8efb30c2d18a21df05dacb8ed91d3e0bbe');
const b = balanceOf$('0x73e41c8efb30c2d18a21df05dacb8ed91d3e0bbe');
const c = balanceOf$(
'0x73e41c8efb30c2d18a21df05dacb8ed91d3e0bbe',
localProvider
);
const d = balanceOf$(
'0x73e41c8efb30c2d18a21df05dacb8ed91d3e0bbe',
localProvider
);

// a.subscribe(console.log);
c.subscribe(console.log);
d.subscribe(console.log);

console.log('a === b', a === b, 'a === c', a === c, 'c === d', c === d);

// ReactDOM.render(<App />, document.getElementById('root'));
ReactDOM.render(<App />, document.getElementById('root'));
9 changes: 3 additions & 6 deletions packages/light.js/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import * as debug from 'debug';
import * as EventEmitter from 'eventemitter3';
import * as memoizee from 'memoizee';

import frequency from './frequency';

// This is our global api object, to be used if no provider is passed to RpcObservables.
let api: any; // TODO @parity/api

Expand Down Expand Up @@ -55,12 +57,7 @@ export const setApi = (newApi: any) => {
* @param provider - An Ethereum provider object.
*/
export const setProvider = (provider?: any) => {
api = createApiFromProvider(provider);
if (!api.isPubSub) {
console.warn(
`Current provider does not support pubsub. @parity/light.js will poll every second to listen to changes.`
);
}
setApi(createApiFromProvider(provider));
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/light.js/src/frequency/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// SPDX-License-Identifier: MIT

import BigNumber from 'bignumber.js';
import { filter } from 'rxjs/operators';
import { filter, tap } from 'rxjs/operators';

import createPubsubObservable from './utils/createPubsubObservable';
import { FrequencyObservable, FrequencyObservableOptions } from '../types';
Expand Down
36 changes: 32 additions & 4 deletions packages/light.js/src/frequency/frequency.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@
//
// SPDX-License-Identifier: MIT

import { createApiFromProvider } from '../api';
import { take } from 'rxjs/operators';

import frequency from './frequency';
import { FrequencyObservable, FrequencyKey, FrequencyMap } from '../types';
import isObservable from '../utils/isObservable';
import { resolveApi } from '../utils/testHelpers/mockApi';
import { setApi } from '@parity/light.js/src/api';

/**
* Helper function to make basic tests for frequency$ observables.
*
* @ignore
*/
const testFrequency = (name: string, frequency$: FrequencyObservable<any>) =>
describe(`${name} rpc`, () => {
const testFrequency = (
name: string,
frequency$: FrequencyObservable<any>,
resolveWith: any = 'foo'
) =>
describe(`${name} frequency`, () => {
beforeAll(() => {
setApi(resolveApi(resolveWith));
});

it('should be an Observable', () => {
expect(isObservable(frequency$())).toBe(true);
});
Expand All @@ -32,8 +42,26 @@ const testFrequency = (name: string, frequency$: FrequencyObservable<any>) =>
const { metadata } = frequency$;
expect(metadata.name).toBeTruthy();
});

it('function should return the same Observable upon re-running (memoization)', () => {
const initial$ = frequency$();
expect(frequency$()).toBe(initial$);
});

it('should return values', done => {
frequency$()
.pipe(take(1))
.subscribe(data => {
expect(data).not.toBeNull();
done();
});
});
});

Object.keys(frequency).forEach(key =>
testFrequency(key, (frequency as FrequencyMap)[key as FrequencyKey])
testFrequency(
key,
(frequency as FrequencyMap)[key as FrequencyKey],
key.includes('Account') ? ['foo'] : 4 // Give string[] for accounts pubsub, or number elsewhere
)
);
7 changes: 2 additions & 5 deletions packages/light.js/src/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
//
// SPDX-License-Identifier: MIT

import { memoizeAll } from '../utils/memoizeAll';
import * as eth from './eth';
import rpc from './rpc';

const rpc = { ...eth };

export default memoizeAll(rpc);
export default rpc;
4 changes: 2 additions & 2 deletions packages/light.js/src/rpc/other/makeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Address } from '../../types';
import createRpc from '../utils/createRpc';
import { switchMapPromise } from '../../utils/operators';
import api from '../../api';
import { onEveryBlock$ } from '../../frequency';
import frequency from '../../frequency';
import { post$ } from './post';

interface MakeContract {
Expand Down Expand Up @@ -75,7 +75,7 @@ export const makeContract = memoizee(

if (method.constant) {
return createRpc({
frequency: [onEveryBlock$],
frequency: [frequency.onEveryBlock$],
name,
pipes: () => [
switchMapPromise(() =>
Expand Down
63 changes: 63 additions & 0 deletions packages/light.js/src/rpc/rpc.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: MIT

import isObservable from '../utils/isObservable';
import { resolveApi } from '../utils/testHelpers/mockApi';
import rpc from './rpc';
import { RpcKey, RpcMap, RpcObservable } from '../types';
import { RPC_LOADING } from '../utils/isLoading';
import { setApi } from '../api';

/**
* Helper function to make basic tests for RpcObservables.
*
* @ignore
*/
const testRpc = (name: string, rpc$: RpcObservable<any, any>) =>
describe(`${name} rpc`, () => {
beforeAll(() => {
setApi(resolveApi());
});

it('should be a function', () => {
expect(typeof rpc$).toBe('function');
});

it('function should return an Observable', () => {
expect(isObservable(rpc$())).toBe(true);
});

it('function result Observable should be subscribable', () => {
expect(() => rpc$().subscribe()).not.toThrow();
});

it('function result Observable should return values', done => {
rpc$().subscribe(data => {
// The first value is either 'foo' (defined in mockApi), or the
// RPC_LOADING symbole.
// In the case of defaultAccount$ (which is accounts$[0]), the returned
// value is 'f'. TODO not clean.
expect(['foo', 'f', RPC_LOADING]).toContain(data);
done();
});
});

it('function should return the same Observable upon re-running (memoization)', () => {
const initial$ = rpc$();
expect(rpc$()).toBe(initial$);
});

it('function should not return the same Observable if options are passed', () => {
const initial$ = rpc$();
expect(rpc$({ withoutLoading: true })).not.toBe(initial$);
});

it('function should return the same Observable if same options are passed', () => {
const initial$ = rpc$({ withoutLoading: true });
expect(rpc$({ withoutLoading: true })).toBe(initial$);
});
});

Object.keys(rpc).forEach(key => testRpc(key, (rpc as RpcMap)[key as RpcKey]));
6 changes: 4 additions & 2 deletions packages/light.js/src/rpc/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import * as eth from './eth';
import { memoizeAll } from '../utils/memoizeAll';
import * as net from './net';
import * as parity from './parity';

const rpc = memoizeAll({ ...eth });
const rpc = { ...eth, ...net, ...parity };

export default rpc;
export default memoizeAll(rpc);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove memoization here (see previous comments)

45 changes: 0 additions & 45 deletions packages/light.js/src/rpc/rpc2.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/light.js/src/utils/memoizeAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const memoizeAll = <T extends FunctionMap>(rpcMap: T) =>
Object.keys(rpcMap).reduce(
(result, key) => {
const metadata = rpcMap[key].metadata;
result[key] = memoizee(rpcMap[key]); // This operation does not copy the metadata of rpcMap[key].
result[key] = memoizee(rpcMap[key], { primitive: true }); // This operation does not copy the metadata of rpcMap[key].
result[key].metadata = metadata;
return result;
},
Expand Down