Skip to content

Commit

Permalink
add instance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Mar 5, 2024
1 parent ce3958c commit d7f7a43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
25 changes: 24 additions & 1 deletion src/controllers/ApiController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2024 @rossbulat/console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { Api } from 'model/Api';
import type { ChainId } from 'config/networks';
import { Api } from 'model/Api';

export class ApiController {
// ------------------------------------------------------
Expand All @@ -10,4 +11,26 @@ export class ApiController {

// The currently instantiated API instances.
static instances: Record<string, Api> = {};

// ------------------------------------------------------
// Api instance methods.
// ------------------------------------------------------

// Instantiate a new Api instance with the supplied chain id and endpoint.
static async instantiate(chainId: ChainId, endpoint: string) {
if (!ApiController.instances[chainId]) {
ApiController.instances[chainId] = new Api(chainId, endpoint);
await ApiController.instances[chainId].initialize();
}
}

// Gracefully disconnect and then destroy an api instance.
static async destroy(chainId: ChainId) {
const api = ApiController.instances[chainId];

if (api) {
await api.disconnect();
delete ApiController.instances[chainId];
}
}
}
4 changes: 0 additions & 4 deletions src/model/Api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export class Api {
constructor(chainId: ChainId, endpoint: string) {
this.#rpcEndpoint = endpoint;
this.#chainId = chainId;
this.initialize();
}

// ------------------------------------------------------
Expand All @@ -69,9 +68,6 @@ export class Api {
// Tell UI api is connecting.
this.dispatchEvent(this.ensureEventStatus('connecting'));

// Tell UI api is connecting.
this.dispatchEvent(this.ensureEventStatus('connecting'));

// Initialise provider events.
this.initProviderEvents();

Expand Down

0 comments on commit d7f7a43

Please sign in to comment.