mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 20:14:49 +00:00
refactor: consolidate SDK Watchable with generic map/eq and rename call to fetch
This commit is contained in:
@@ -1,156 +0,0 @@
|
||||
import { Effects } from '../../../base/lib/Effects'
|
||||
import { Manifest, PackageId } from '../../../base/lib/osBindings'
|
||||
import { AbortedError } from '../../../base/lib/util/AbortedError'
|
||||
import { DropGenerator, DropPromise } from '../../../base/lib/util/Drop'
|
||||
import { deepEqual } from '../../../base/lib/util/deepEqual'
|
||||
|
||||
export class GetServiceManifest<Mapped = Manifest> {
|
||||
constructor(
|
||||
readonly effects: Effects,
|
||||
readonly packageId: PackageId,
|
||||
readonly map: (manifest: Manifest | null) => Mapped,
|
||||
readonly eq: (a: Mapped, b: Mapped) => boolean,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Returns the manifest of a service. Reruns the context from which it has been called if the underlying value changes
|
||||
*/
|
||||
async const() {
|
||||
let abort = new AbortController()
|
||||
const watch = this.watch(abort.signal)
|
||||
const res = await watch.next()
|
||||
if (this.effects.constRetry) {
|
||||
watch
|
||||
.next()
|
||||
.then(() => {
|
||||
abort.abort()
|
||||
this.effects.constRetry && this.effects.constRetry()
|
||||
})
|
||||
.catch()
|
||||
}
|
||||
return res.value
|
||||
}
|
||||
/**
|
||||
* Returns the manifest of a service. Does nothing if it changes
|
||||
*/
|
||||
async once() {
|
||||
const manifest = await this.effects.getServiceManifest({
|
||||
packageId: this.packageId,
|
||||
})
|
||||
return this.map(manifest)
|
||||
}
|
||||
|
||||
private async *watchGen(abort?: AbortSignal) {
|
||||
let prev = null as { value: Mapped } | null
|
||||
const resolveCell = { resolve: () => {} }
|
||||
this.effects.onLeaveContext(() => {
|
||||
resolveCell.resolve()
|
||||
})
|
||||
abort?.addEventListener('abort', () => resolveCell.resolve())
|
||||
while (this.effects.isInContext && !abort?.aborted) {
|
||||
let callback: () => void = () => {}
|
||||
const waitForNext = new Promise<void>((resolve) => {
|
||||
callback = resolve
|
||||
resolveCell.resolve = resolve
|
||||
})
|
||||
const next = this.map(
|
||||
await this.effects.getServiceManifest({
|
||||
packageId: this.packageId,
|
||||
callback: () => callback(),
|
||||
}),
|
||||
)
|
||||
if (!prev || !this.eq(prev.value, next)) {
|
||||
prev = { value: next }
|
||||
yield next
|
||||
}
|
||||
await waitForNext
|
||||
}
|
||||
return new Promise<never>((_, rej) => rej(new AbortedError()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the manifest of a service. Returns an async iterator that yields whenever the value changes
|
||||
*/
|
||||
watch(abort?: AbortSignal): AsyncGenerator<Mapped, never, unknown> {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
return DropGenerator.of(this.watchGen(ctrl.signal), () => ctrl.abort())
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the manifest of a service. Takes a custom callback function to run whenever it changes
|
||||
*/
|
||||
onChange(
|
||||
callback: (
|
||||
value: Mapped | null,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
) {
|
||||
;(async () => {
|
||||
const ctrl = new AbortController()
|
||||
for await (const value of this.watch(ctrl.signal)) {
|
||||
try {
|
||||
const res = await callback(value)
|
||||
if (res.cancel) {
|
||||
ctrl.abort()
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'callback function threw an error @ GetServiceManifest.onChange',
|
||||
e,
|
||||
)
|
||||
}
|
||||
}
|
||||
})()
|
||||
.catch((e) => callback(null, e))
|
||||
.catch((e) =>
|
||||
console.error(
|
||||
'callback function threw an error @ GetServiceManifest.onChange',
|
||||
e,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the manifest of a service. Returns when the predicate is true
|
||||
*/
|
||||
waitFor(pred: (value: Mapped) => boolean): Promise<Mapped> {
|
||||
const ctrl = new AbortController()
|
||||
return DropPromise.of(
|
||||
Promise.resolve().then(async () => {
|
||||
for await (const next of this.watchGen(ctrl.signal)) {
|
||||
if (pred(next)) {
|
||||
return next
|
||||
}
|
||||
}
|
||||
throw new Error('context left before predicate passed')
|
||||
}),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export function getServiceManifest(
|
||||
effects: Effects,
|
||||
packageId: PackageId,
|
||||
): GetServiceManifest<Manifest>
|
||||
export function getServiceManifest<Mapped>(
|
||||
effects: Effects,
|
||||
packageId: PackageId,
|
||||
map: (manifest: Manifest | null) => Mapped,
|
||||
eq?: (a: Mapped, b: Mapped) => boolean,
|
||||
): GetServiceManifest<Mapped>
|
||||
export function getServiceManifest<Mapped>(
|
||||
effects: Effects,
|
||||
packageId: PackageId,
|
||||
map?: (manifest: Manifest | null) => Mapped,
|
||||
eq?: (a: Mapped, b: Mapped) => boolean,
|
||||
): GetServiceManifest<Mapped> {
|
||||
return new GetServiceManifest(
|
||||
effects,
|
||||
packageId,
|
||||
map ?? ((a) => a as Mapped),
|
||||
eq ?? ((a, b) => deepEqual(a, b)),
|
||||
)
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
import { T } from '..'
|
||||
import { Effects } from '../../../base/lib/Effects'
|
||||
import { AbortedError } from '../../../base/lib/util/AbortedError'
|
||||
import { DropGenerator, DropPromise } from '../../../base/lib/util/Drop'
|
||||
|
||||
export class GetSslCertificate {
|
||||
constructor(
|
||||
readonly effects: Effects,
|
||||
readonly hostnames: string[],
|
||||
readonly algorithm?: T.Algorithm,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Returns the an SSL Certificate for the given hostnames if permitted. Restarts the service if it changes
|
||||
*/
|
||||
const() {
|
||||
return this.effects.getSslCertificate({
|
||||
hostnames: this.hostnames,
|
||||
algorithm: this.algorithm,
|
||||
callback:
|
||||
this.effects.constRetry &&
|
||||
(() => this.effects.constRetry && this.effects.constRetry()),
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Returns the an SSL Certificate for the given hostnames if permitted. Does nothing if it changes
|
||||
*/
|
||||
once() {
|
||||
return this.effects.getSslCertificate({
|
||||
hostnames: this.hostnames,
|
||||
algorithm: this.algorithm,
|
||||
})
|
||||
}
|
||||
|
||||
private async *watchGen(abort?: AbortSignal) {
|
||||
const resolveCell = { resolve: () => {} }
|
||||
this.effects.onLeaveContext(() => {
|
||||
resolveCell.resolve()
|
||||
})
|
||||
abort?.addEventListener('abort', () => resolveCell.resolve())
|
||||
while (this.effects.isInContext && !abort?.aborted) {
|
||||
let callback: () => void = () => {}
|
||||
const waitForNext = new Promise<void>((resolve) => {
|
||||
callback = resolve
|
||||
resolveCell.resolve = resolve
|
||||
})
|
||||
yield await this.effects.getSslCertificate({
|
||||
hostnames: this.hostnames,
|
||||
algorithm: this.algorithm,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
return new Promise<never>((_, rej) => rej(new AbortedError()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the SSL Certificate for the given hostnames if permitted. Returns an async iterator that yields whenever the value changes
|
||||
*/
|
||||
watch(
|
||||
abort?: AbortSignal,
|
||||
): AsyncGenerator<[string, string, string], never, unknown> {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
return DropGenerator.of(this.watchGen(ctrl.signal), () => ctrl.abort())
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the SSL Certificate for the given hostnames if permitted. Takes a custom callback function to run whenever it changes
|
||||
*/
|
||||
onChange(
|
||||
callback: (
|
||||
value: [string, string, string] | null,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
) {
|
||||
;(async () => {
|
||||
const ctrl = new AbortController()
|
||||
for await (const value of this.watch(ctrl.signal)) {
|
||||
try {
|
||||
const res = await callback(value)
|
||||
if (res.cancel) {
|
||||
ctrl.abort()
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'callback function threw an error @ GetSslCertificate.onChange',
|
||||
e,
|
||||
)
|
||||
}
|
||||
}
|
||||
})()
|
||||
.catch((e) => callback(null, e))
|
||||
.catch((e) =>
|
||||
console.error(
|
||||
'callback function threw an error @ GetSslCertificate.onChange',
|
||||
e,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the SSL Certificate for the given hostnames if permitted. Returns when the predicate is true
|
||||
*/
|
||||
waitFor(
|
||||
pred: (value: [string, string, string] | null) => boolean,
|
||||
): Promise<[string, string, string] | null> {
|
||||
const ctrl = new AbortController()
|
||||
return DropPromise.of(
|
||||
Promise.resolve().then(async () => {
|
||||
for await (const next of this.watchGen(ctrl.signal)) {
|
||||
if (pred(next)) {
|
||||
return next
|
||||
}
|
||||
}
|
||||
return null
|
||||
}),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@ import * as TOML from '@iarna/toml'
|
||||
import * as INI from 'ini'
|
||||
import * as T from '../../../base/lib/types'
|
||||
import * as fs from 'node:fs/promises'
|
||||
import { AbortedError, asError, deepEqual } from '../../../base/lib/util'
|
||||
import { DropGenerator, DropPromise } from '../../../base/lib/util/Drop'
|
||||
import { asError, deepEqual } from '../../../base/lib/util'
|
||||
import { Watchable } from '../../../base/lib/util/Watchable'
|
||||
import { PathBase } from './Volume'
|
||||
|
||||
const previousPath = /(.+?)\/([^/]*)$/
|
||||
@@ -228,132 +228,72 @@ export class FileHelper<A> {
|
||||
return map(this.validate(data))
|
||||
}
|
||||
|
||||
private async readConst<B>(
|
||||
private createFileWatchable<B>(
|
||||
effects: T.Effects,
|
||||
map: (value: A) => B,
|
||||
eq: (left: B | null | undefined, right: B | null) => boolean,
|
||||
): Promise<B | null> {
|
||||
const watch = this.readWatch(effects, map, eq)
|
||||
const res = await watch.next()
|
||||
if (effects.constRetry) {
|
||||
const record: (typeof this.consts)[number] = [
|
||||
effects.constRetry,
|
||||
res.value,
|
||||
map,
|
||||
eq,
|
||||
]
|
||||
this.consts.push(record)
|
||||
watch
|
||||
.next()
|
||||
.then(() => {
|
||||
this.consts = this.consts.filter((r) => r !== record)
|
||||
effects.constRetry && effects.constRetry()
|
||||
})
|
||||
.catch()
|
||||
}
|
||||
return res.value
|
||||
}
|
||||
|
||||
private async *readWatch<B>(
|
||||
effects: T.Effects,
|
||||
map: (value: A) => B,
|
||||
eq: (left: B | null | undefined, right: B | null) => boolean,
|
||||
abort?: AbortSignal,
|
||||
eq: (left: B | null, right: B | null) => boolean,
|
||||
) {
|
||||
let prev: { value: B | null } | null = null
|
||||
while (effects.isInContext && !abort?.aborted) {
|
||||
if (await exists(this.path)) {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
const watch = fs.watch(this.path, {
|
||||
persistent: false,
|
||||
signal: ctrl.signal,
|
||||
})
|
||||
const newRes = await this.readOnce(map)
|
||||
const listen = Promise.resolve()
|
||||
.then(async () => {
|
||||
for await (const _ of watch) {
|
||||
ctrl.abort()
|
||||
return null
|
||||
}
|
||||
})
|
||||
.catch((e) => console.error(asError(e)))
|
||||
if (!prev || !eq(prev.value, newRes)) {
|
||||
console.error('yielding', JSON.stringify({ prev: prev, newRes }))
|
||||
yield newRes
|
||||
}
|
||||
prev = { value: newRes }
|
||||
await listen
|
||||
} else {
|
||||
yield null
|
||||
await onCreated(this.path).catch((e) => console.error(asError(e)))
|
||||
}
|
||||
const doRead = async (): Promise<A | null> => {
|
||||
const data = await this.readFile()
|
||||
if (!data) return null
|
||||
return this.validate(data)
|
||||
}
|
||||
return new Promise<never>((_, rej) => rej(new AbortedError()))
|
||||
}
|
||||
const filePath = this.path
|
||||
const fileHelper = this
|
||||
|
||||
private readOnChange<B>(
|
||||
effects: T.Effects,
|
||||
callback: (
|
||||
value: B | null,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
map: (value: A) => B,
|
||||
eq: (left: B | null | undefined, right: B | null) => boolean,
|
||||
) {
|
||||
;(async () => {
|
||||
const ctrl = new AbortController()
|
||||
for await (const value of this.readWatch(effects, map, eq, ctrl.signal)) {
|
||||
try {
|
||||
const res = await callback(value)
|
||||
if (res.cancel) ctrl.abort()
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'callback function threw an error @ FileHelper.read.onChange',
|
||||
e,
|
||||
)
|
||||
}
|
||||
const wrappedMap = (raw: A | null): B | null => {
|
||||
if (raw === null) return null
|
||||
return map(raw)
|
||||
}
|
||||
|
||||
return new (class extends Watchable<A | null, B | null> {
|
||||
protected readonly label = 'FileHelper'
|
||||
|
||||
protected async fetch() {
|
||||
return doRead()
|
||||
}
|
||||
})()
|
||||
.catch((e) => callback(null, e))
|
||||
.catch((e) =>
|
||||
console.error(
|
||||
'callback function threw an error @ FileHelper.read.onChange',
|
||||
e,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private readWaitFor<B>(
|
||||
effects: T.Effects,
|
||||
pred: (value: B | null, error?: Error) => boolean,
|
||||
map: (value: A) => B,
|
||||
): Promise<B | null> {
|
||||
const ctrl = new AbortController()
|
||||
return DropPromise.of(
|
||||
Promise.resolve().then(async () => {
|
||||
const watch = this.readWatch(effects, map, (_) => false, ctrl.signal)
|
||||
while (true) {
|
||||
try {
|
||||
const res = await watch.next()
|
||||
if (pred(res.value)) {
|
||||
ctrl.abort()
|
||||
return res.value
|
||||
}
|
||||
if (res.done) {
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
if (pred(null, e as Error)) {
|
||||
break
|
||||
}
|
||||
protected async *produce(
|
||||
abort: AbortSignal,
|
||||
): AsyncGenerator<A | null, void> {
|
||||
while (this.effects.isInContext && !abort.aborted) {
|
||||
if (await exists(filePath)) {
|
||||
const ctrl = new AbortController()
|
||||
abort.addEventListener('abort', () => ctrl.abort())
|
||||
const watch = fs.watch(filePath, {
|
||||
persistent: false,
|
||||
signal: ctrl.signal,
|
||||
})
|
||||
yield await doRead()
|
||||
await Promise.resolve()
|
||||
.then(async () => {
|
||||
for await (const _ of watch) {
|
||||
ctrl.abort()
|
||||
return null
|
||||
}
|
||||
})
|
||||
.catch((e) => console.error(asError(e)))
|
||||
} else {
|
||||
yield null
|
||||
await onCreated(filePath).catch((e) => console.error(asError(e)))
|
||||
}
|
||||
}
|
||||
ctrl.abort()
|
||||
return null
|
||||
}),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
}
|
||||
|
||||
protected onConstRegistered(value: B | null): (() => void) | void {
|
||||
if (!this.effects.constRetry) return
|
||||
const record: (typeof fileHelper.consts)[number] = [
|
||||
this.effects.constRetry,
|
||||
value,
|
||||
wrappedMap,
|
||||
eq,
|
||||
]
|
||||
fileHelper.consts.push(record)
|
||||
return () => {
|
||||
fileHelper.consts = fileHelper.consts.filter((r) => r !== record)
|
||||
}
|
||||
}
|
||||
})(effects, { map: wrappedMap, eq })
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -372,7 +312,7 @@ export class FileHelper<A> {
|
||||
read(): ReadType<A>
|
||||
read<B>(
|
||||
map: (value: A) => B,
|
||||
eq?: (left: B | null | undefined, right: B | null) => boolean,
|
||||
eq?: (left: B | null, right: B | null) => boolean,
|
||||
): ReadType<B>
|
||||
read(
|
||||
map?: (value: A) => any,
|
||||
@@ -382,24 +322,19 @@ export class FileHelper<A> {
|
||||
eq = eq ?? deepEqual
|
||||
return {
|
||||
once: () => this.readOnce(map),
|
||||
const: (effects: T.Effects) => this.readConst(effects, map, eq),
|
||||
watch: (effects: T.Effects, abort?: AbortSignal) => {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
return DropGenerator.of(
|
||||
this.readWatch(effects, map, eq, ctrl.signal),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
},
|
||||
const: (effects: T.Effects) =>
|
||||
this.createFileWatchable(effects, map, eq).const(),
|
||||
watch: (effects: T.Effects, abort?: AbortSignal) =>
|
||||
this.createFileWatchable(effects, map, eq).watch(abort),
|
||||
onChange: (
|
||||
effects: T.Effects,
|
||||
callback: (
|
||||
value: A | null,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
) => this.readOnChange(effects, callback, map, eq),
|
||||
) => this.createFileWatchable(effects, map, eq).onChange(callback),
|
||||
waitFor: (effects: T.Effects, pred: (value: A | null) => boolean) =>
|
||||
this.readWaitFor(effects, pred, map),
|
||||
this.createFileWatchable(effects, map, eq).waitFor(pred),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
export * from '../../../base/lib/util'
|
||||
export { GetSslCertificate } from './GetSslCertificate'
|
||||
export { GetServiceManifest, getServiceManifest } from './GetServiceManifest'
|
||||
|
||||
export { Drop } from '../../../base/lib/util/Drop'
|
||||
export { Volume, Volumes } from './Volume'
|
||||
|
||||
Reference in New Issue
Block a user