Skip to content

Commit

Permalink
rename TreeLeaf to TreeAbst
Browse files Browse the repository at this point in the history
  • Loading branch information
uenoB committed Mar 28, 2024
1 parent b5d971c commit 4e5918a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/page/src/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AssetNode {

export type { Asset, AssetNode }

export class AssetLeaf<Base extends SomeInternal> {
export class AssetAbst<Base extends SomeInternal> {
constructor(private readonly load: (() => Awaitable<AssetModule>) | string) {}

instantiate(parent: Base): PromiseLike<AssetNode> {
Expand Down
20 changes: 10 additions & 10 deletions packages/page/src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ class FileNameTransition<Next> extends Transition<Next> {

export type { Transition }
type NextPath = Readonly<RelPath>
export type Next<Leaf> = Readonly<{ leaf: Leaf; relPath: NextPath }>
export type Next<Abst> = Readonly<{ abst: Abst; relPath: NextPath }>

export class Indices<Leaf, Asset> {
readonly moduleNameMap = new ModuleNameTransition<Next<Leaf>>()
readonly stemMap = new ModuleNameTransition<Next<Leaf>>()
readonly fileNameMap = new FileNameTransition<Next<Leaf | Asset>>()
export class Indices<Abst, Asset> {
readonly moduleNameMap = new ModuleNameTransition<Next<Abst>>()
readonly stemMap = new ModuleNameTransition<Next<Abst>>()
readonly fileNameMap = new FileNameTransition<Next<Abst | Asset>>()

addRoute(leaf: Leaf, relPath: NextPath): void {
const next = { leaf, relPath }
addRoute(abst: Abst, relPath: NextPath): void {
const next = { abst, relPath }
const moduleSteps = PathSteps.fromRelativeModuleName(relPath.moduleName)
const stemSteps = PathSteps.fromRelativeModuleName(relPath.stem)
const fileSteps = PathSteps.fromRelativeFileName(relPath.fileName)
Expand All @@ -144,7 +144,7 @@ export class Indices<Leaf, Asset> {
}
}

export interface TreeLeaf<Tree, Inst = Tree> {
export interface TreeAbst<Tree, Inst = Tree> {
readonly instantiate: (parent: Tree, path: NextPath) => PromiseLike<Inst>
}

Expand All @@ -156,7 +156,7 @@ interface TreeNode<Tree, Content> {

type Content<Tree, Inst, Key extends string> =
| ((...a: never) => unknown)
| PromiseLike<{ [P in Key]: Transition<Next<TreeLeaf<Tree, Inst>>> }>
| PromiseLike<{ [P in Key]: Transition<Next<TreeAbst<Tree, Inst>>> }>

const undef = Promise.resolve(undefined)

Expand Down Expand Up @@ -206,7 +206,7 @@ export const find = <
trie.value?.reduceRight<Cont>((cont, { next, epsilon }) => {
return r =>
r ??
next.leaf.instantiate(node, next.relPath).then(inst => {
next.abst.instantiate(node, next.relPath).then(inst => {
return epsilon
? search(inst, key, final).then(cont)
: check(inst, key, final ?? true).then(cont)
Expand Down
50 changes: 25 additions & 25 deletions packages/page/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { type Pairs, type List, iteratePairs, listItems } from './items'
import { constProp } from './util'
import { type Delay, delay } from './delay'
import { type RelPath, PathSteps, concatFileName } from './filename'
import { type Asset, type AssetModule, AssetLeaf } from './asset'
import { type Asset, type AssetModule, AssetAbst } from './asset'
import type { Loaded, MainModule, Dir, TreeNode, Inst } from './tree'
import { Tree, TreeLeafImpl } from './tree'
import { Tree, TreeAbstImpl } from './tree'

const dummyRelPath: Readonly<RelPath> = {
moduleName: '',
Expand Down Expand Up @@ -64,13 +64,13 @@ class PageFactory<
Args extends unknown[]
> {
readonly basis: This
readonly leaf: TreeLeafImpl<Base, This, Impl>
readonly abst: TreeAbstImpl<Base, This, Impl>

constructor(
This: PageConstructor<Base, This, Args>,
arg: NewArg<Base, This, Impl>,
args: Args,
content: TreeLeafImpl<Base, This, Impl>['content']
content: TreeAbstImpl<Base, This, Impl>['content']
) {
this.basis = new This(...args)
if (arg.parsePath != null) {
Expand All @@ -87,35 +87,35 @@ class PageFactory<
}
const init = {
rootURL: arg.url != null ? new URL(arg.url) : undefined,
fileName: TreeLeafImpl.currentFileName(),
fileName: TreeAbstImpl.currentFileName(),
basis: this.basis,
content,
Base: This.Base
}
this.leaf = TreeLeafImpl.decorate<Base, This, Impl>(init)
this.abst = TreeAbstImpl.decorate<Base, This, Impl>(init)
}

createSubpage(
dir: Dir<Base>,
content: TreeLeafImpl<Base, This, Impl>['content'],
content: TreeAbstImpl<Base, This, Impl>['content'],
relPath: Readonly<RelPath>
): TreeLeafImpl<Base, This, Impl> {
): TreeAbstImpl<Base, This, Impl> {
const arg = {
rootURL: this.leaf.rootURL,
fileName: concatFileName(this.leaf.fileName, relPath?.fileName),
rootURL: this.abst.rootURL,
fileName: concatFileName(this.abst.fileName, relPath?.fileName),
basis: this.basis,
content,
Base: this.leaf.Base
Base: this.abst.Base
}
const leaf = TreeLeafImpl.decorate(arg)
dir.addRoute(leaf, relPath)
return leaf
const abst = TreeAbstImpl.decorate(arg)
dir.addRoute(abst, relPath)
return abst
}

async createModuleDirectory(
arg: Readonly<ModuleArg<Base, This, Impl>>
): Promise<Dir<Base>> {
const dir = TreeLeafImpl.createDirectory<Base>()
const dir = TreeAbstImpl.createDirectory<Base>()
const substPath = (path: string): Awaitable<string> =>
arg.substitutePath != null
? Reflect.apply(arg.substitutePath, this.basis, [path])
Expand All @@ -135,9 +135,9 @@ class PageFactory<
fileName: PathSteps.normalize(path)
}
}
const leaf = this.leaf.getTreeLeaf(load)
if (leaf != null) {
dir.addRoute(leaf, relPath)
const abst = this.abst.getTreeAbst(load)
if (abst != null) {
dir.addRoute(abst, relPath)
} else if (typeof load === 'function') {
this.createSubpage(dir, load, relPath)
} else {
Expand All @@ -148,8 +148,8 @@ class PageFactory<
if (arg.assets != null) {
for await (const [path, load] of iteratePairs(arg.assets, this.basis)) {
const loader = load ?? (await substPath(path))
const leaf = new AssetLeaf<TreeNode<Base>>(loader)
const pair = { leaf, relPath: dummyRelPath }
const abst = new AssetAbst<TreeNode<Base>>(loader)
const pair = { abst, relPath: dummyRelPath }
dir.fileNameMap.addRoute(PathSteps.fromRelativeFileName(path), pair)
}
}
Expand All @@ -159,18 +159,18 @@ class PageFactory<
async createPaginateDirectory<Item>(
arg: Readonly<PaginateArg<Item, Base, This, Impl>>
): Promise<Dir<Base>> {
const dir = TreeLeafImpl.createDirectory<Base>()
const dir = TreeAbstImpl.createDirectory<Base>()
const rawPageSize = arg.pageSize ?? 10
const pageSize = rawPageSize >= 1 ? rawPageSize : 1
const load = arg.load
const allItems = await listItems(arg.items, this.leaf.basis)
const allItems = await listItems(arg.items, this.abst.basis)
const numAllItems = allItems.length
const numPages = Math.ceil(numAllItems / pageSize)
const pages: Array<Paginate<Item, This>> = []
for (let pageIndex = 0; pageIndex < numPages; pageIndex++) {
const itemIndex = pageIndex * pageSize
const items = allItems.slice(itemIndex, itemIndex + pageSize)
const relPath = this.leaf.basis.paginatePath(pageIndex)
const relPath = this.abst.basis.paginatePath(pageIndex)
const page = undefined as unknown as This // dummy
const pagi = { pages, page, pageIndex, items, itemIndex, numAllItems }
const content = (): Loaded<Impl> => Reflect.apply(load, pagi.page, [pagi])
Expand Down Expand Up @@ -203,7 +203,7 @@ export class Page<
type F = PageFactory<Base, Page<Impl, Base> & This, Impl, Args>
const dir = delay(async () => await factory.createModuleDirectory(arg))
const factory: F = new PageFactory(this, arg, args, dir)
return factory.leaf.basis
return factory.abst.basis
}

static paginate<
Expand All @@ -220,7 +220,7 @@ export class Page<
type F = PageFactory<Base, Page<Impl, Base> & This, Impl, Args>
const dir = delay(async () => await factory.createPaginateDirectory(arg))
const factory: F = new PageFactory(this, arg, args, dir)
return factory.leaf.basis
return factory.abst.basis
}

parsePath(fileName: string): ParsePath {
Expand Down
6 changes: 3 additions & 3 deletions packages/page/src/ref.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ModuleName } from '../../vite-plugin-minissg/src/module'
import { type Awaitable, raise } from '../../vite-plugin-minissg/src/util'
import { type Delay, delay } from './delay'
import type { Transition, Next, TreeLeaf } from './find'
import type { Transition, Next, TreeAbst } from './find'
import type { RelPath } from './filename'

interface SomeNode {
Expand All @@ -10,7 +10,7 @@ interface SomeNode {
interface TreeNode<Tree> extends SomeNode {
readonly content:
| ((...a: never) => unknown)
| PromiseLike<{ moduleNameMap: Transition<Next<TreeLeaf<Tree>>> }>
| PromiseLike<{ moduleNameMap: Transition<Next<TreeAbst<Tree>>> }>
readonly findChild: () => PromiseLike<Tree | undefined>
readonly parent: Tree | undefined
readonly root: Tree
Expand Down Expand Up @@ -118,7 +118,7 @@ const descendants = <Tree extends TreeNode<Tree>, Inst extends SomeNode>(
const k = (i: number): Awaitable<void> => {
const next = branches[i]
if (next == null) return skip.then(last).then(cont)
return next.leaf
return next.abst
.instantiate(tree, next.relPath)
.then(node => descendants(node, wait, except, queue, () => k(i + 1)))
}
Expand Down
Loading

0 comments on commit 4e5918a

Please sign in to comment.