feat: Save uses dependencies, which ensure the id is from manifest

This commit is contained in:
BluJ
2023-04-27 09:27:03 -06:00
parent d55cc40b33
commit 353692bf55
3 changed files with 34 additions and 16 deletions

View File

@@ -1,15 +1,25 @@
import { GenericManifest } from "../manifest/ManifestTypes";
import { Dependency, PackageId } from "../types"; import { Dependency, PackageId } from "../types";
export function exists(id: PackageId) { export type Dependencies<T extends GenericManifest> = {
return { exists(id: keyof T["dependencies"]): Dependency;
id, running(id: keyof T["dependencies"]): Dependency;
kind: "exists", };
} as Dependency;
}
export function running(id: PackageId) { export const dependenciesSet = <
return { T extends GenericManifest,
id, >(): Dependencies<T> => ({
kind: "running", exists(id: keyof T["dependencies"]) {
} as Dependency; return {
} id,
kind: "exists",
} as Dependency;
},
running(id: keyof T["dependencies"]) {
return {
id,
kind: "running",
} as Dependency;
},
});

View File

@@ -3,16 +3,19 @@ import { DeepPartial, Dependencies, Effects, ExpectedExports } from "../types";
import { InputSpec } from "./configTypes"; import { InputSpec } from "./configTypes";
import { Utils, nullIfEmpty, once, utils } from "../util"; import { Utils, nullIfEmpty, once, utils } from "../util";
import { TypeFromProps } from "../util/propertiesMatcher"; import { TypeFromProps } from "../util/propertiesMatcher";
import { GenericManifest } from "../manifest/ManifestTypes";
import * as D from "./dependencies";
declare const dependencyProof: unique symbol; declare const dependencyProof: unique symbol;
export type DependenciesReceipt = void & { export type DependenciesReceipt = void & {
[dependencyProof]: never; [dependencyProof]: never;
}; };
export type Save<WD, A> = (options: { export type Save<WD, A, Manifest extends GenericManifest> = (options: {
effects: Effects; effects: Effects;
input: A; input: A;
utils: Utils<WD>; utils: Utils<WD>;
dependencies: D.Dependencies<Manifest>;
}) => Promise<DependenciesReceipt>; }) => Promise<DependenciesReceipt>;
export type Read<WD, A> = (options: { export type Read<WD, A> = (options: {
effects: Effects; effects: Effects;
@@ -25,9 +28,13 @@ export type Read<WD, A> = (options: {
* @param options * @param options
* @returns * @returns
*/ */
export function setupConfig<WD, A extends Config<InputSpec>>( export function setupConfig<
WD,
A extends Config<InputSpec>,
Manifest extends GenericManifest,
>(
spec: A, spec: A,
write: Save<WD, TypeFromProps<A>>, write: Save<WD, TypeFromProps<A>, Manifest>,
read: Read<WD, TypeFromProps<A>>, read: Read<WD, TypeFromProps<A>>,
) { ) {
const validator = once(() => spec.validator()); const validator = once(() => spec.validator());
@@ -41,6 +48,7 @@ export function setupConfig<WD, A extends Config<InputSpec>>(
input: JSON.parse(JSON.stringify(input)), input: JSON.parse(JSON.stringify(input)),
effects, effects,
utils: utils<WD>(effects), utils: utils<WD>(effects),
dependencies: D.dependenciesSet<Manifest>(),
}); });
}) as ExpectedExports.setConfig, }) as ExpectedExports.setConfig,
getConfig: (async ({ effects, config }) => { getConfig: (async ({ effects, config }) => {

View File

@@ -26,7 +26,7 @@ export class WrapperData<WrapperData, Path extends string> {
callback: () => {}, callback: () => {},
}); });
} }
async *overTime() { async *watch() {
while (true) { while (true) {
let callback: () => void; let callback: () => void;
const waitForNext = new Promise<void>((resolve) => { const waitForNext = new Promise<void>((resolve) => {