Skip to content

Commit

Permalink
refactor: Update Label and LabelDecorator to support lazy loading of …
Browse files Browse the repository at this point in the history
…steps
  • Loading branch information
BlackRam-oss committed Jun 17, 2024
1 parent 2a815a4 commit a58019b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/classes/Label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Label<T extends {} = {}> {
* @param onStepRun is a function that will be executed before any step is executed, is useful for example to make sure all images used have been cached
* @param choiseIndex is the index of the choice that the label will perform
*/
constructor(id: LabelIdType, steps: StepLabelType<T>[], onStepRun?: () => void | Promise<void>, choiseIndex?: number) {
constructor(id: LabelIdType, steps: StepLabelType<T>[] | (() => StepLabelType<T>[]), onStepRun?: () => void | Promise<void>, choiseIndex?: number) {
this._id = id
this._steps = steps
this._onStepRun = onStepRun
Expand All @@ -49,13 +49,16 @@ export default class Label<T extends {} = {}> {
return this._id
}

private _steps: StepLabelType<T>[]
private _steps: StepLabelType<T>[] | (() => StepLabelType<T>[])
/**
* Get the steps of the label.
* This class should be extended and the steps method should be overridden.
* Every time you update this list will also be updated when the other game versions load.
*/
public get steps(): StepLabelType<T>[] {
if (typeof this._steps === "function") {
return this._steps()
}
return this._steps
}

Expand Down
2 changes: 1 addition & 1 deletion src/decorators/LabelDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const registeredLabels: { [key: LabelIdType]: Label<any> } = {}
* @param onStepRun is a function that will be executed before any step is executed, is useful for example to make sure all images used have been cached
* @returns The created label
*/
export function newLabel<T extends {} = {}>(id: LabelIdType, steps: StepLabelType<T>[], onStepRun?: () => void | Promise<void>): Label<T> {
export function newLabel<T extends {} = {}>(id: LabelIdType, steps: StepLabelType<T>[] | (() => StepLabelType<T>[]), onStepRun?: () => void | Promise<void>): Label<T> {
if (registeredLabels[id]) {
console.warn(`[Pixi'VN] Label ${id} already exists, it will be overwritten`)
}
Expand Down

0 comments on commit a58019b

Please sign in to comment.