Skip to content

Commit

Permalink
Implement better state metadata constraint types
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Aug 1, 2024
1 parent 36f9cf4 commit 2ca9038
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/composable-controller/src/ComposableController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
ControllerStateChangeEvent,
} from '@metamask/base-controller';
import { BaseController } from '@metamask/base-controller';
import type { PublicInterface } from '@metamask/utils';
import type { Json, PublicInterface } from '@metamask/utils';
import type { Patch } from 'immer';

export const controllerName = 'ComposableController';
Expand Down Expand Up @@ -39,6 +39,23 @@ type BaseControllerV1Instance = PublicInterface<
BaseControllerV1<ConfigConstraintV1, StateConstraintV1>
>;

/**
* A universal supertype of functions that accept a piece of controller state and return some derivation of that state.
*/
type StateDeriverConstraint = (value: never) => Json;

/**
* A universal supertype of metadata objects for individual state properties.
*/
type StatePropertyMetadataConstraint = {
[P in 'anonymous' | 'persist']: boolean | StateDeriverConstraint;
};

/**
* A universal supertype of state metadata objects.
*/
type StateMetadataConstraint = Record<string, StatePropertyMetadataConstraint>;

/**
* A universal subtype of all controller instances that extend from `BaseController` (formerly `BaseControllerV2`).
* Any `BaseController` instance can be assigned to this type.
Expand All @@ -57,7 +74,9 @@ type BaseControllerInstance = Omit<
>
>,
'metadata'
> & { metadata: Record<string, unknown> };
> & {
metadata: StateMetadataConstraint;
};

/**
* A universal subtype of all controller instances that extend from `BaseController` (formerly `BaseControllerV2`) or `BaseControllerV1`.
Expand Down

0 comments on commit 2ca9038

Please sign in to comment.