mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 05:43:41 +00:00
Remove unnecessary complex types (#7451)
#7290 introduced these types, but during development we switched over to specifying dependencies in type parameters instead of an object. This change meant we no longer needed these `Type` or `ShapeToInstance` types, greatly simplifying the types related to derived state.
This commit is contained in:
@@ -14,7 +14,7 @@ import {
|
||||
DerivedStateProvider,
|
||||
} from "../src/platform/state";
|
||||
import { UserId } from "../src/types/guid";
|
||||
import { ShapeToInstances, DerivedStateDependencies } from "../src/types/state";
|
||||
import { DerivedStateDependencies } from "../src/types/state";
|
||||
|
||||
import {
|
||||
FakeActiveUserState,
|
||||
@@ -92,7 +92,7 @@ export class FakeStateProvider implements StateProvider {
|
||||
getDerived<TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
||||
parentState$: Observable<TFrom>,
|
||||
deriveDefinition: DeriveDefinition<unknown, TTo, TDeps>,
|
||||
dependencies: ShapeToInstances<TDeps>,
|
||||
dependencies: TDeps,
|
||||
): DerivedState<TTo> {
|
||||
return this.derived.get(parentState$, deriveDefinition, dependencies);
|
||||
}
|
||||
@@ -108,7 +108,7 @@ export class FakeDerivedStateProvider implements DerivedStateProvider {
|
||||
get<TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
||||
parentState$: Observable<TFrom>,
|
||||
deriveDefinition: DeriveDefinition<TFrom, TTo, TDeps>,
|
||||
dependencies: ShapeToInstances<TDeps>,
|
||||
dependencies: TDeps,
|
||||
): DerivedState<TTo> {
|
||||
let result = this.states.get(deriveDefinition.buildCacheKey()) as DerivedState<TTo>;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { DerivedStateDependencies, ShapeToInstances, StorageKey } from "../../types/state";
|
||||
import { DerivedStateDependencies, StorageKey } from "../../types/state";
|
||||
|
||||
import { KeyDefinition } from "./key-definition";
|
||||
import { StateDefinition } from "./state-definition";
|
||||
@@ -19,7 +19,7 @@ type DeriveDefinitionOptions<TFrom, TTo, TDeps extends DerivedStateDependencies
|
||||
* These are constant for the lifetime of the derived state.
|
||||
* @returns The derived state value or a Promise that resolves to the derived state value.
|
||||
*/
|
||||
derive: (from: TFrom, deps: ShapeToInstances<TDeps>) => TTo | Promise<TTo>;
|
||||
derive: (from: TFrom, deps: TDeps) => TTo | Promise<TTo>;
|
||||
/**
|
||||
* A function to use to safely convert your type from json to your expected type.
|
||||
*
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import { ShapeToInstances, DerivedStateDependencies } from "../../types/state";
|
||||
import { DerivedStateDependencies } from "../../types/state";
|
||||
|
||||
import { DeriveDefinition } from "./derive-definition";
|
||||
import { DerivedState } from "./derived-state";
|
||||
@@ -20,6 +20,6 @@ export abstract class DerivedStateProvider {
|
||||
get: <TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
||||
parentState$: Observable<TFrom>,
|
||||
deriveDefinition: DeriveDefinition<TFrom, TTo, TDeps>,
|
||||
dependencies: ShapeToInstances<TDeps>,
|
||||
dependencies: TDeps,
|
||||
) => DerivedState<TTo>;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import { DerivedStateDependencies, ShapeToInstances } from "../../../types/state";
|
||||
import { DerivedStateDependencies } from "../../../types/state";
|
||||
import {
|
||||
AbstractStorageService,
|
||||
ObservableStorageService,
|
||||
@@ -19,7 +19,7 @@ export class DefaultDerivedStateProvider implements DerivedStateProvider {
|
||||
get<TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
||||
parentState$: Observable<TFrom>,
|
||||
deriveDefinition: DeriveDefinition<TFrom, TTo, TDeps>,
|
||||
dependencies: ShapeToInstances<TDeps>,
|
||||
dependencies: TDeps,
|
||||
): DerivedState<TTo> {
|
||||
const cacheKey = deriveDefinition.buildCacheKey();
|
||||
const existingDerivedState = this.cache[cacheKey];
|
||||
@@ -37,7 +37,7 @@ export class DefaultDerivedStateProvider implements DerivedStateProvider {
|
||||
protected buildDerivedState<TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
||||
parentState$: Observable<TFrom>,
|
||||
deriveDefinition: DeriveDefinition<TFrom, TTo, TDeps>,
|
||||
dependencies: ShapeToInstances<TDeps>,
|
||||
dependencies: TDeps,
|
||||
): DerivedState<TTo> {
|
||||
return new DefaultDerivedState<TFrom, TTo, TDeps>(
|
||||
parentState$,
|
||||
|
||||
@@ -14,7 +14,7 @@ import { DefaultDerivedState } from "./default-derived-state";
|
||||
let callCount = 0;
|
||||
const cleanupDelayMs = 10;
|
||||
const stateDefinition = new StateDefinition("test", "memory");
|
||||
const deriveDefinition = new DeriveDefinition<string, Date, { date: typeof Date }>(
|
||||
const deriveDefinition = new DeriveDefinition<string, Date, { date: Date }>(
|
||||
stateDefinition,
|
||||
"test",
|
||||
{
|
||||
@@ -30,7 +30,7 @@ const deriveDefinition = new DeriveDefinition<string, Date, { date: typeof Date
|
||||
describe("DefaultDerivedState", () => {
|
||||
let parentState$: Subject<string>;
|
||||
let memoryStorage: FakeStorageService;
|
||||
let sut: DefaultDerivedState<string, Date, { date: typeof Date }>;
|
||||
let sut: DefaultDerivedState<string, Date, { date: Date }>;
|
||||
const deps = {
|
||||
date: new Date(),
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Observable, ReplaySubject, Subject, concatMap, merge, share, timer } from "rxjs";
|
||||
|
||||
import { ShapeToInstances, DerivedStateDependencies } from "../../../types/state";
|
||||
import { DerivedStateDependencies } from "../../../types/state";
|
||||
import {
|
||||
AbstractStorageService,
|
||||
ObservableStorageService,
|
||||
@@ -23,7 +23,7 @@ export class DefaultDerivedState<TFrom, TTo, TDeps extends DerivedStateDependenc
|
||||
private parentState$: Observable<TFrom>,
|
||||
protected deriveDefinition: DeriveDefinition<TFrom, TTo, TDeps>,
|
||||
private memoryStorage: AbstractStorageService & ObservableStorageService,
|
||||
private dependencies: ShapeToInstances<TDeps>,
|
||||
private dependencies: TDeps,
|
||||
) {
|
||||
this.storageKey = deriveDefinition.storageKey;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import { ShapeToInstances, DerivedStateDependencies } from "../../../types/state";
|
||||
import { DerivedStateDependencies } from "../../../types/state";
|
||||
import { DeriveDefinition } from "../derive-definition";
|
||||
import { DerivedState } from "../derived-state";
|
||||
import { DerivedStateProvider } from "../derived-state.provider";
|
||||
@@ -26,6 +26,6 @@ export class DefaultStateProvider implements StateProvider {
|
||||
getDerived: <TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
||||
parentState$: Observable<TFrom>,
|
||||
deriveDefinition: DeriveDefinition<unknown, TTo, TDeps>,
|
||||
dependencies: ShapeToInstances<TDeps>,
|
||||
dependencies: TDeps,
|
||||
) => DerivedState<TTo> = this.derivedStateProvider.get.bind(this.derivedStateProvider);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import { UserId } from "../../types/guid";
|
||||
import { ShapeToInstances, DerivedStateDependencies } from "../../types/state";
|
||||
import { DerivedStateDependencies } from "../../types/state";
|
||||
|
||||
import { DeriveDefinition } from "./derive-definition";
|
||||
import { DerivedState } from "./derived-state";
|
||||
@@ -26,6 +26,6 @@ export abstract class StateProvider {
|
||||
getDerived: <TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
||||
parentState$: Observable<TFrom>,
|
||||
deriveDefinition: DeriveDefinition<unknown, TTo, TDeps>,
|
||||
dependencies: ShapeToInstances<TDeps>,
|
||||
dependencies: TDeps,
|
||||
) => DerivedState<TTo>;
|
||||
}
|
||||
|
||||
14
libs/common/src/types/state.d.ts
vendored
14
libs/common/src/types/state.d.ts
vendored
@@ -2,16 +2,4 @@ import { Opaque } from "type-fest";
|
||||
|
||||
type StorageKey = Opaque<string, "StorageKey">;
|
||||
|
||||
/**
|
||||
* A helper type defining Constructor types for javascript and `typeof T` types for Typescript
|
||||
*/
|
||||
type Type<T> = abstract new (...args: unknown[]) => T;
|
||||
|
||||
type DerivedStateDependencies = Record<string, Type<unknown>>;
|
||||
|
||||
/**
|
||||
* Converts an object of types to an object of instances
|
||||
*/
|
||||
type ShapeToInstances<T> = {
|
||||
[P in keyof T]: T[P] extends Type<infer R> ? R : never;
|
||||
};
|
||||
type DerivedStateDependencies = Record<string, unknown>;
|
||||
|
||||
Reference in New Issue
Block a user