Mock events emitter (#79)

This commit is contained in:
Jaco Greeff
2018-04-25 10:01:33 +02:00
committed by GitHub
parent a50b8ed9f5
commit 8df1228e6f
4 changed files with 16 additions and 2 deletions
+11 -1
View File
@@ -6,6 +6,7 @@
// FIME: This file is way too long and way too messy
import type { KeyringPair } from '@polkadot/util-keyring/types';
import type { ProviderInterface$Emitted } from '../types';
import type { MockState, MockState$Subscription$Callback } from './types';
const BN = require('bn.js');
@@ -18,6 +19,9 @@ const storageKey = require('./storageKey');
const keyring = testKeyring();
const emitEvents: Array<ProviderInterface$Emitted> = ['connected', 'disconnected'];
let emitIndex = 0;
function makeBlockHeader (prevNumber: BN) {
const blockNumber = prevNumber.addn(1);
@@ -53,10 +57,16 @@ function setStorageBn (storage, prefix: string, key: Uint8Array, value: BN): voi
storage[storageKey(prefix, key)] = bnToU8a(value, 64, true);
}
module.exports = function mocks ({ storage, subscriptions }: MockState): void {
module.exports = function mocks ({ emitter, storage, subscriptions }: MockState): void {
let newHead = makeBlockHeader(new BN(-1));
setInterval(() => {
if (++emitIndex === emitEvents.length) {
emitIndex = 0;
}
emitter.emit(emitEvents[emitIndex]);
newHead = makeBlockHeader(newHead.number);
Object.values(keyring).forEach((pair, index) => {
+1 -1
View File
@@ -7,5 +7,5 @@ import type { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../typ
import type { MockState } from './types';
module.exports = function on (self: MockState, type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
self.l.error(`Provider does not have 'on' emitters for type '${type}'`);
self.emitter.on(type, sub);
};
+2
View File
@@ -5,6 +5,7 @@
import type { MockState, MockState$Storage } from './types';
const EventEmitter = require('eventemitter3');
const interfaces = require('@polkadot/api-jsonrpc');
const u8aToHex = require('@polkadot/util/u8a/toHex');
const l = require('@polkadot/util/logger')('api-mock');
@@ -39,6 +40,7 @@ module.exports = function state (): MockState {
}, {});
return {
emitter: new EventEmitter(),
l,
requests: Object.assign({}, REQUESTS),
storage,
+2
View File
@@ -3,6 +3,7 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type EventEmitter from 'eventemitter3';
import type { Logger } from '@polkadot/util/types';
export type MockState$Subscription$Callback = (error: ?Error, value: mixed) => void;
@@ -25,6 +26,7 @@ export type MockState$Requests = {
};
export type MockState = {
emitter: EventEmitter,
l: Logger,
requests: MockState$Requests,
storage: MockState$Storage,