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: { fn: (o: {
effects: Effects effects: Effects
started(onTerm: () => void): null started(onTerm: () => void): null
utils: Utils<WrapperData> utils: Utils<WrapperData, {}>
}) => Promise<Daemons<any>>, }) => Promise<Daemons<any>>,
): ExpectedExports.main => { ): ExpectedExports.main => {
return async (options) => { return async (options) => {
const result = await fn({ const result = await fn({
...options, ...options,
utils: utils<WrapperData>(options.effects), utils: utils<WrapperData, {}>(options.effects),
}) })
await result.build().then((x) => x.wait()) await result.build().then((x) => x.wait())
} }

View File

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

View File

@@ -2,7 +2,7 @@ import { Parser } from "ts-matches"
import { Effects, EnsureWrapperDataPath, ExtractWrapperData } from "../types" import { Effects, EnsureWrapperDataPath, ExtractWrapperData } from "../types"
import { NoAny } from "." import { NoAny } from "."
export class WrapperData<WrapperData, Path extends string> { export class GetWrapperData<WrapperData, Path extends string> {
constructor( constructor(
readonly effects: Effects, readonly effects: Effects,
readonly path: Path & EnsureWrapperDataPath<WrapperData, Path>, 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() { const() {
return this.effects.getWrapperData<WrapperData, Path>({ return this.effects.getWrapperData<WrapperData, Path>({
...this.options, ...this.options,
@@ -19,6 +22,10 @@ export class WrapperData<WrapperData, Path extends string> {
callback: this.effects.restart, callback: this.effects.restart,
}) })
} }
/**
* Returns the wrapper data once and then never again
* Doesn't restart the server when the wrapper data changes
*/
once() { once() {
return this.effects.getWrapperData<WrapperData, Path>({ return this.effects.getWrapperData<WrapperData, Path>({
...this.options, ...this.options,
@@ -26,6 +33,9 @@ export class WrapperData<WrapperData, Path extends string> {
callback: () => {}, callback: () => {},
}) })
} }
/**
* Keeps giving the latest wrapper data as it changes
*/
async *watch() { async *watch() {
while (true) { while (true) {
let callback: () => void let callback: () => void
@@ -49,5 +59,5 @@ export function getWrapperData<WrapperData, Path extends string>(
packageId?: string | undefined 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 * as T from "../types"
import FileHelper from "./fileHelper" import FileHelper from "./fileHelper"
import nullIfEmpty from "./nullIfEmpty" import nullIfEmpty from "./nullIfEmpty"
import { WrapperData, getWrapperData } from "./getWrapperData" import { GetWrapperData, getWrapperData } from "./getWrapperData"
import { import {
CheckResult, CheckResult,
checkPortListening, checkPortListening,
@@ -54,7 +54,7 @@ export type WrapperDataOptionals<WrapperData, Path extends string> = {
packageId?: string | undefined packageId?: string | undefined
} }
export type Utils<WD> = { export type Utils<WD, WrapperOverWrite = { const: never }> = {
readFile: <A>(fileHelper: FileHelper<A>) => ReturnType<FileHelper<A>["read"]> readFile: <A>(fileHelper: FileHelper<A>) => ReturnType<FileHelper<A>["read"]>
writeFile: <A>( writeFile: <A>(
fileHelper: FileHelper<A>, fileHelper: FileHelper<A>,
@@ -63,10 +63,10 @@ export type Utils<WD> = {
getWrapperData: <Path extends string>( getWrapperData: <Path extends string>(
packageId: string, packageId: string,
path: T.EnsureWrapperDataPath<WD, Path>, path: T.EnsureWrapperDataPath<WD, Path>,
) => WrapperData<WD, Path> ) => GetWrapperData<WD, Path> & WrapperOverWrite
getOwnWrapperData: <Path extends string>( getOwnWrapperData: <Path extends string>(
path: T.EnsureWrapperDataPath<WD, Path>, path: T.EnsureWrapperDataPath<WD, Path>,
) => WrapperData<WD, Path> ) => GetWrapperData<WD, Path> & WrapperOverWrite
setOwnWrapperData: <Path extends string | never>( setOwnWrapperData: <Path extends string | never>(
path: T.EnsureWrapperDataPath<WD, Path>, path: T.EnsureWrapperDataPath<WD, Path>,
value: ExtractWrapperData<WD, Path>, value: ExtractWrapperData<WD, Path>,
@@ -94,9 +94,9 @@ export type Utils<WD> = {
exists: (props: { path: string; volumeId: string }) => Promise<boolean> exists: (props: { path: string; volumeId: string }) => Promise<boolean>
nullIfEmpty: typeof nullIfEmpty nullIfEmpty: typeof nullIfEmpty
} }
export const utils = <WrapperData = never>( export const utils = <WrapperData = never, WrapperOverWrite = { const: never }>(
effects: T.Effects, effects: T.Effects,
): Utils<WrapperData> => ({ ): Utils<WrapperData, WrapperOverWrite> => ({
readFile: <A>(fileHelper: FileHelper<A>) => fileHelper.read(effects), readFile: <A>(fileHelper: FileHelper<A>) => fileHelper.read(effects),
writeFile: <A>(fileHelper: FileHelper<A>, data: A) => writeFile: <A>(fileHelper: FileHelper<A>, data: A) =>
fileHelper.write(data, effects), fileHelper.write(data, effects),
@@ -105,10 +105,13 @@ export const utils = <WrapperData = never>(
getWrapperData: <WrapperData = never, Path extends string = never>( getWrapperData: <WrapperData = never, Path extends string = never>(
packageId: string, packageId: string,
path: T.EnsureWrapperDataPath<WrapperData, Path>, 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>( getOwnWrapperData: <Path extends string>(
path: T.EnsureWrapperDataPath<WrapperData, Path>, 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>( setOwnWrapperData: <Path extends string | never>(
path: T.EnsureWrapperDataPath<WrapperData, Path>, path: T.EnsureWrapperDataPath<WrapperData, Path>,
value: ExtractWrapperData<WrapperData, Path>, value: ExtractWrapperData<WrapperData, Path>,