-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(refactor): Abstract state bootstrapping & subscriptions from Api (…
- Loading branch information
Showing
15 changed files
with
855 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import type { NetworkName } from 'types'; | ||
import type { ChainSubscriptions, Subscription } from './types'; | ||
|
||
// A class to manage subscriptions. | ||
|
||
export class SubscriptionsController { | ||
// ------------------------------------------------------ | ||
// Class members. | ||
// ------------------------------------------------------ | ||
|
||
// Subscription objects, keyed by an network. | ||
static #subs: Partial<Record<NetworkName, ChainSubscriptions>> = {}; | ||
|
||
// ------------------------------------------------------ | ||
// Getters. | ||
// ------------------------------------------------------ | ||
|
||
static get subs() { | ||
return this.#subs; | ||
} | ||
|
||
// Gets all subscriptions for a network. | ||
static getAll(network: NetworkName): ChainSubscriptions | undefined { | ||
return this.#subs[network]; | ||
} | ||
|
||
// Get a subscription by network and subscriptionId. | ||
static get( | ||
network: NetworkName, | ||
subscriptionId: string | ||
): Subscription | undefined { | ||
return this.#subs[network]?.[subscriptionId] || undefined; | ||
} | ||
|
||
// ------------------------------------------------------ | ||
// Setter. | ||
// ------------------------------------------------------ | ||
|
||
// Sets a new subscription for a network. | ||
static set( | ||
network: NetworkName, | ||
subscriptionId: string, | ||
subscription: Subscription | ||
): void { | ||
// Ignore if there is already a subscription for this network and subscriptionId. | ||
if (this.#subs?.[network]?.[subscriptionId]) { | ||
return; | ||
} | ||
|
||
// Create a new subscriptions record for the network if one doesn't exist. | ||
if (!this.#subs[network]) { | ||
this.#subs[network] = {}; | ||
} | ||
|
||
// NOTE: We know for certain that `this.#subs[network]` is defined here. | ||
this.#subs[network]![subscriptionId] = subscription; | ||
} | ||
|
||
// ------------------------------------------------------ | ||
// Unsubscribe. | ||
// ------------------------------------------------------ | ||
|
||
// Unsubscribe from a subscription and remove it from class state. | ||
static remove(network: NetworkName, subscriptionId: string): void { | ||
if (this.#subs[network]) { | ||
try { | ||
delete this.#subs[network]![subscriptionId]; | ||
} catch (e) { | ||
// Silently fail if the subscription doesn't exist. | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import type { AnyJson } from '@w3ux/types'; | ||
|
||
// Define all possible subscription classes. | ||
// TODO: Add subscription classes here. | ||
export type Subscription = AnyJson; | ||
|
||
// the record of subscriptions, keyed by tabId. | ||
export type ChainSubscriptions = Record<string, Subscription>; | ||
|
||
// Abstract class that ensures all subscription classes have an unsubscribe method. | ||
export abstract class Unsubscribable { | ||
// Unsubscribe from unsubs present in this class. | ||
abstract unsubscribe: () => void; | ||
} |
Oops, something went wrong.