Skip to content

Commit

Permalink
chore: refactor OperationGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Oct 18, 2023
1 parent 98fa4d6 commit a435434
Show file tree
Hide file tree
Showing 37 changed files with 677 additions and 581 deletions.
27 changes: 19 additions & 8 deletions packages/core/src/generators/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@
* Abstract class that contains the building blocks for plugins to create their own Generator
* @link idea based on https://github.com/colinhacks/zod/blob/master/src/types.ts#L137
*/
export abstract class Generator<TOptions extends object = object> {
private _options: TOptions = {} as TOptions
export abstract class Generator<TOptions = unknown, TContext = unknown> {
#options: TOptions = {} as TOptions
#context: TContext = {} as TContext

constructor(options?: TOptions, context?: TContext) {
if (context) {
this.#context = context
}

constructor(options: TOptions = {} as TOptions) {
if (options) {
this._options = {
...this._options,
...options,
}
this.#options = options
}

return this
}

get options(): TOptions {
return this._options
return this.#options
}

get context(): TContext {
return this.#context
}

set options(options: TOptions) {
this.#options = { ...this.#options, ...options }
}

abstract build(...params: unknown[]): unknown
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/managers/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ type PackageJSON = {
}

export class PackageManager {
private cwd: string
#cwd: string
constructor(workspace = process.cwd()) {
this.cwd = workspace
this.#cwd = workspace

return this
}

async getPackageJSON(): Promise<PackageJSON | undefined> {
const pkgPath = await findUp(['package.json'], {
cwd: this.cwd,
cwd: this.#cwd,
})
if (!pkgPath) {
return undefined
Expand All @@ -27,7 +27,7 @@ export class PackageManager {

getPackageJSONSync(): PackageJSON | undefined {
const pkgPath = findUpSync(['package.json'], {
cwd: this.cwd,
cwd: this.#cwd,
})
if (!pkgPath) {
return undefined
Expand Down
46 changes: 23 additions & 23 deletions packages/core/src/managers/fileManager/FileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import type { Queue, QueueJob } from '../../utils/index.ts'
import type { CacheItem, KubbFile } from './types.ts'

export class FileManager {
private cache: Map<KubbFile.Path, CacheItem[]> = new Map()
#cache: Map<KubbFile.Path, CacheItem[]> = new Map()

private task?: QueueJob<KubbFile.ResolvedFile>
#task?: QueueJob<KubbFile.ResolvedFile>

private queue?: Queue
#queue?: Queue

constructor(options?: { queue?: Queue; task?: QueueJob<KubbFile.ResolvedFile> }) {
if (options) {
this.task = options.task
this.queue = options.queue
this.#task = options.task
this.#queue = options.queue
}
}
get extensions(): Array<KubbFile.Extname> {
Expand All @@ -25,27 +25,27 @@ export class FileManager {

get files(): Array<KubbFile.File> {
const files: Array<KubbFile.File> = []
this.cache.forEach((item) => {
this.#cache.forEach((item) => {
files.push(...item.flat(1))
})

return files
}
get isExecuting(): boolean {
return this.queue?.hasJobs ?? false
return this.#queue?.hasJobs ?? false
}

async add(file: KubbFile.File): Promise<KubbFile.ResolvedFile> {
const controller = new AbortController()
const resolvedFile: KubbFile.ResolvedFile = { id: crypto.randomUUID(), ...file }

this.cache.set(resolvedFile.path, [{ cancel: () => controller.abort(), ...resolvedFile }])
this.#cache.set(resolvedFile.path, [{ cancel: () => controller.abort(), ...resolvedFile }])

if (this.queue) {
if (this.#queue) {
try {
await this.queue.run(
await this.#queue.run(
async () => {
return this.task?.(resolvedFile)
return this.#task?.(resolvedFile)
},
{ controller },
)
Expand All @@ -62,11 +62,11 @@ export class FileManager {
// console.warn(`Path ${file.path}(file.path) should end with the baseName ${file.baseName}(file.filename)`)
// }

const previousCaches = this.cache.get(file.path)
const previousCaches = this.#cache.get(file.path)
const previousCache = previousCaches ? previousCaches.at(previousCaches.length - 1) : undefined

if (previousCache) {
this.cache.delete(previousCache.path)
this.#cache.delete(previousCache.path)

return this.add({
...file,
Expand All @@ -79,23 +79,23 @@ export class FileManager {
return this.add(file)
}

private append(path: KubbFile.Path, file: KubbFile.ResolvedFile): void {
const previousFiles = this.cache.get(path) || []
#append(path: KubbFile.Path, file: KubbFile.ResolvedFile): void {
const previousFiles = this.#cache.get(path) || []

this.cache.set(path, [...previousFiles, file])
this.#cache.set(path, [...previousFiles, file])
}

getCacheByUUID(UUID: KubbFile.UUID): KubbFile.File | undefined {
let cache: KubbFile.File | undefined

this.cache.forEach((files) => {
this.#cache.forEach((files) => {
cache = files.find((item) => item.id === UUID)
})
return cache
}

get(path: KubbFile.Path): Array<KubbFile.File> | undefined {
return this.cache.get(path)
return this.#cache.get(path)
}

remove(path: KubbFile.Path): void {
Expand All @@ -104,12 +104,12 @@ export class FileManager {
return
}

this.cache.delete(path)
this.#cache.delete(path)
}

async write(...params: Parameters<typeof write>): Promise<void> {
if (this.queue) {
return this.queue.run(async () => {
if (this.#queue) {
return this.#queue.run(async () => {
return write(...params)
})
}
Expand All @@ -118,8 +118,8 @@ export class FileManager {
}

async read(...params: Parameters<typeof read>): Promise<string> {
if (this.queue) {
return this.queue.run(async () => {
if (this.#queue) {
return this.#queue.run(async () => {
return read(...params)
})
}
Expand Down
Loading

1 comment on commit a435434

@vercel
Copy link

@vercel vercel bot commented on a435434 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kubb – ./

kubb-kubb.vercel.app
kubb-git-main-kubb.vercel.app
kubb.dev
www.kubb.dev

Please sign in to comment.