feat: Only make the .const for the GetWrapperData only available on main

This commit is contained in:
BluJ
2023-04-27 12:59:27 -06:00
parent f992d6cb8f
commit 05260b858b
4 changed files with 42 additions and 13 deletions

View File

@@ -26,13 +26,13 @@ export const setupMain = <WrapperData>(
fn: (o: {
effects: Effects
started(onTerm: () => void): null
utils: Utils<WrapperData>
utils: Utils<WrapperData, {}>
}) => Promise<Daemons<any>>,
): ExpectedExports.main => {
return async (options) => {
const result = await fn({
...options,
utils: utils<WrapperData>(options.effects),
utils: utils<WrapperData, {}>(options.effects),
})
await result.build().then((x) => x.wait())
}

View File

@@ -43,21 +43,37 @@ describe("wrapperData", () => {
path: "/config/some2Value",
value: "someValueIn",
})
;(await utils<WrapperType, {}>(todo<T.Effects>())
.getOwnWrapperData("/config/someValue")
.const()) satisfies string
;(await utils<WrapperType, {}>(todo<T.Effects>())
.getOwnWrapperData("/config")
.const()) satisfies WrapperType["config"]
await utils<WrapperType, {}>(todo<T.Effects>())
// @ts-expect-error Path is wrong
.getOwnWrapperData("/config/somdsfeValue")
.const()
///
;(await utils<WrapperType>(todo<T.Effects>())
.getOwnWrapperData("/config/someValue")
// @ts-expect-error Const should normally not be callable
.const()) satisfies string
;(await utils<WrapperType>(todo<T.Effects>())
.getOwnWrapperData("/config")
// @ts-expect-error Const should normally not be callable
.const()) satisfies WrapperType["config"]
await utils<WrapperType>(todo<T.Effects>())
// @ts-expect-error Path is wrong
.getOwnWrapperData("/config/somdsfeValue")
// @ts-expect-error Const should normally not be callable
.const()
///
;(await utils<WrapperType>(todo<T.Effects>())
.getOwnWrapperData("/config/someValue")
// @ts-expect-error satisfies type is wrong
.const()) satisfies number
;(await utils<WrapperType>(todo<T.Effects>())
;(await utils<WrapperType, {}>(todo<T.Effects>())
// @ts-expect-error Path is wrong
.getOwnWrapperData("/config/")
.const()) satisfies WrapperType["config"]

View File

@@ -2,7 +2,7 @@ import { Parser } from "ts-matches"
import { Effects, EnsureWrapperDataPath, ExtractWrapperData } from "../types"
import { NoAny } from "."
export class WrapperData<WrapperData, Path extends string> {
export class GetWrapperData<WrapperData, Path extends string> {
constructor(
readonly effects: Effects,
readonly path: Path & EnsureWrapperDataPath<WrapperData, Path>,
@@ -12,6 +12,9 @@ export class WrapperData<WrapperData, Path extends string> {
} = {},
) {}
/** This should be used as the primary method in main since it allows the main to
* restart if the wrapper data changes
*/
const() {
return this.effects.getWrapperData<WrapperData, Path>({
...this.options,
@@ -19,6 +22,10 @@ export class WrapperData<WrapperData, Path extends string> {
callback: this.effects.restart,
})
}
/**
* Returns the wrapper data once and then never again
* Doesn't restart the server when the wrapper data changes
*/
once() {
return this.effects.getWrapperData<WrapperData, Path>({
...this.options,
@@ -26,6 +33,9 @@ export class WrapperData<WrapperData, Path extends string> {
callback: () => {},
})
}
/**
* Keeps giving the latest wrapper data as it changes
*/
async *watch() {
while (true) {
let callback: () => void
@@ -49,5 +59,5 @@ export function getWrapperData<WrapperData, Path extends string>(
packageId?: string | undefined
} = {},
) {
return new WrapperData<WrapperData, Path>(effects, path as any, options)
return new GetWrapperData<WrapperData, Path>(effects, path as any, options)
}

View File

@@ -2,7 +2,7 @@ import { Parser } from "ts-matches"
import * as T from "../types"
import FileHelper from "./fileHelper"
import nullIfEmpty from "./nullIfEmpty"
import { WrapperData, getWrapperData } from "./getWrapperData"
import { GetWrapperData, getWrapperData } from "./getWrapperData"
import {
CheckResult,
checkPortListening,
@@ -54,7 +54,7 @@ export type WrapperDataOptionals<WrapperData, Path extends string> = {
packageId?: string | undefined
}
export type Utils<WD> = {
export type Utils<WD, WrapperOverWrite = { const: never }> = {
readFile: <A>(fileHelper: FileHelper<A>) => ReturnType<FileHelper<A>["read"]>
writeFile: <A>(
fileHelper: FileHelper<A>,
@@ -63,10 +63,10 @@ export type Utils<WD> = {
getWrapperData: <Path extends string>(
packageId: string,
path: T.EnsureWrapperDataPath<WD, Path>,
) => WrapperData<WD, Path>
) => GetWrapperData<WD, Path> & WrapperOverWrite
getOwnWrapperData: <Path extends string>(
path: T.EnsureWrapperDataPath<WD, Path>,
) => WrapperData<WD, Path>
) => GetWrapperData<WD, Path> & WrapperOverWrite
setOwnWrapperData: <Path extends string | never>(
path: T.EnsureWrapperDataPath<WD, Path>,
value: ExtractWrapperData<WD, Path>,
@@ -94,9 +94,9 @@ export type Utils<WD> = {
exists: (props: { path: string; volumeId: string }) => Promise<boolean>
nullIfEmpty: typeof nullIfEmpty
}
export const utils = <WrapperData = never>(
export const utils = <WrapperData = never, WrapperOverWrite = { const: never }>(
effects: T.Effects,
): Utils<WrapperData> => ({
): Utils<WrapperData, WrapperOverWrite> => ({
readFile: <A>(fileHelper: FileHelper<A>) => fileHelper.read(effects),
writeFile: <A>(fileHelper: FileHelper<A>, data: A) =>
fileHelper.write(data, effects),
@@ -105,10 +105,13 @@ export const utils = <WrapperData = never>(
getWrapperData: <WrapperData = never, Path extends string = never>(
packageId: string,
path: T.EnsureWrapperDataPath<WrapperData, Path>,
) => getWrapperData<WrapperData, Path>(effects, path as any, { packageId }),
) =>
getWrapperData<WrapperData, Path>(effects, path as any, {
packageId,
}) as any,
getOwnWrapperData: <Path extends string>(
path: T.EnsureWrapperDataPath<WrapperData, Path>,
) => getWrapperData<WrapperData, Path>(effects, path as any),
) => getWrapperData<WrapperData, Path>(effects, path as any) as any,
setOwnWrapperData: <Path extends string | never>(
path: T.EnsureWrapperDataPath<WrapperData, Path>,
value: ExtractWrapperData<WrapperData, Path>,