UNPKG

834 BPlain TextView Raw
1// Copyright 2017-2018 @polkadot/api-provider authors & contributors
2// This software may be modified and distributed under the terms
3// of the ISC license. See the LICENSE file for details.
4
5import { MockState, MockState$Subscription$Callback } from './types';
6
7export default async function subscribe (self: MockState, type: string, method: string, params: Array<any>): Promise<number> {
8 self.l.debug(() => ['subscribe', method, params]);
9
10 if (self.subscriptions[method]) {
11 const callback: MockState$Subscription$Callback = params.pop();
12 const id = ++self.subscriptionId;
13
14 self.subscriptions[method].callbacks[id] = callback;
15 self.subscriptionMap[id] = method;
16
17 callback(null, self.subscriptions[method].lastValue);
18
19 return id;
20 }
21
22 throw new Error(`provider.subscribe: Invalid method '${method}'`);
23}