Skip to content

Commit

Permalink
feat(StoreRegistry): add StoreOf and PieceOf (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet authored Nov 16, 2023
1 parent 21a9be0 commit 7b0ae1d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/lib/structures/AliasPiece.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Piece } from './Piece';
import type { StoreRegistryEntries } from './StoreRegistry';

export interface AliasPieceOptions extends Piece.Options {
/**
Expand All @@ -11,7 +12,10 @@ export interface AliasPieceOptions extends Piece.Options {
/**
* The piece to be stored in {@link AliasStore} instances.
*/
export class AliasPiece<O extends AliasPieceOptions = AliasPieceOptions> extends Piece<O> {
export class AliasPiece<
Options extends AliasPieceOptions = AliasPieceOptions,
StoreName extends keyof StoreRegistryEntries = keyof StoreRegistryEntries
> extends Piece<Options, StoreName> {
/**
* The aliases for the piece.
*/
Expand Down
9 changes: 5 additions & 4 deletions src/lib/structures/Piece.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Awaitable } from '@sapphire/utilities';
import { container, type Container } from '../shared/Container';
import { PieceLocation, type PieceLocationJSON } from './PieceLocation';
import type { Store } from './Store';
import type { StoreOf, StoreRegistryEntries } from './StoreRegistry';

/**
* The context for the piece, contains extra information from the store,
Expand Down Expand Up @@ -49,11 +50,11 @@ export interface PieceOptions {
/**
* The piece to be stored in {@link Store} instances.
*/
export class Piece<O extends PieceOptions = PieceOptions> {
export class Piece<Options extends PieceOptions = PieceOptions, StoreName extends keyof StoreRegistryEntries = keyof StoreRegistryEntries> {
/**
* The store that contains the piece.
*/
public readonly store: Store<Piece>;
public readonly store: StoreOf<StoreName>;

/**
* The location metadata for the piece's file.
Expand All @@ -73,14 +74,14 @@ export class Piece<O extends PieceOptions = PieceOptions> {
/**
* The raw options passed to this {@link Piece}
*/
public readonly options: O;
public readonly options: Options;

public constructor(context: PieceContext, options: PieceOptions = {}) {
this.store = context.store;
this.location = new PieceLocation(context.path, context.root);
this.name = options.name ?? context.name;
this.enabled = options.enabled ?? true;
this.options = options as O;
this.options = options as Options;
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/lib/structures/StoreRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Collection } from '@discordjs/collection';
import { isClass } from '@sapphire/utilities';
import { join } from 'path';
import { LoaderError } from '../errors/LoaderError';
import { resolvePath, type Path } from '../internal/Path';
import { getRootData } from '../internal/RootScan';
import { ManuallyRegisteredPiecesSymbol, VirtualPath } from '../internal/constants';
import type { Piece } from './Piece';
import type { Store, StoreManuallyRegisteredPiece } from './Store';
import { isClass } from '@sapphire/utilities';

type Key = keyof StoreRegistryEntries;
type Value = StoreRegistryEntries[Key];
Expand Down Expand Up @@ -204,3 +204,19 @@ export interface StoreRegistryEntries {}
export interface StoreManagerManuallyRegisteredPiece<StoreName extends keyof StoreRegistryEntries> extends StoreManuallyRegisteredPiece<StoreName> {
store: StoreName;
}

/**
* Type utility to get the {@linkcode Store} given its name.
*/
export type StoreOf<StoreName extends keyof StoreRegistryEntries> = keyof StoreRegistryEntries extends never
? Store<Piece<Piece.Options, StoreName>>
: StoreRegistryEntries[StoreName];

/**
* Type utility to get the {@linkcode Piece} given its {@linkcode Store}'s name.
*/
export type PieceOf<StoreName extends keyof StoreRegistryEntries> = keyof StoreRegistryEntries extends never
? Piece<Piece.Options, StoreName>
: StoreRegistryEntries[StoreName] extends Store<infer PieceType>
? PieceType
: Piece<Piece.Options, StoreName>;

0 comments on commit 7b0ae1d

Please sign in to comment.