Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(StoreRegistry): add StoreOf and PieceOf #349

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;