@unilogin/sdk
Version:
SDK is a JS library, that communicates with relayer. SDK allows managing contract, by creating basic contract-calling messages.
62 lines (52 loc) • 2 kB
text/typescript
import {RelayerApi} from '../../integration/http/RelayerApi';
import deepEqual from 'deep-equal';
import ObserverRunner from './ObserverRunner';
import {ensure, Notification, RelayerRequest} from '@unilogin/commons';
import {ConcurrentAuthorisation} from '../utils/errors';
class AuthorisationsObserver extends ObserverRunner {
private lastAuthorisations: Notification[] = [];
private authorisationRequest?: RelayerRequest;
private callbacks: Function[] = [];
constructor(private relayerApi: RelayerApi, tick: number) {
super();
this.tick = tick;
}
execute(): Promise<void> {
return this.checkAuthorisationsChangedFor(this.authorisationRequest!);
}
private async checkAuthorisationsChangedFor(authorisationRequest: RelayerRequest) {
const authorisations = await this.fetchPendingAuthorisations(authorisationRequest);
if (!deepEqual(authorisations, this.lastAuthorisations)) {
this.lastAuthorisations = authorisations;
for (const callback of this.callbacks) {
callback(authorisations);
}
}
}
async fetchPendingAuthorisations(authorisationRequest: RelayerRequest) {
const {response} = await this.relayerApi.getPendingAuthorisations(authorisationRequest);
return response;
}
subscribe(authorisationRequest: RelayerRequest, callback: Function) {
ensure(
!this.authorisationRequest ||
(this.authorisationRequest.contractAddress === authorisationRequest.contractAddress),
ConcurrentAuthorisation,
);
callback(this.lastAuthorisations);
this.authorisationRequest = authorisationRequest;
this.callbacks.push(callback);
if (this.isStopped()) {
this.start();
}
return () => {
this.callbacks = this.callbacks.filter((element) => callback !== element);
if (this.callbacks.length === 0) {
this.authorisationRequest = undefined;
this.lastAuthorisations = [];
this.stop();
}
};
}
}
export default AuthorisationsObserver;