1 |
|
2 |
|
3 |
|
4 |
|
5 | import { MockState, MockState$Subscription$Callback } from './types';
|
6 |
|
7 | export 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 | }
|