Skip to content

Commit

Permalink
Make the check<T>() function more flexible (so that it doesn't always
Browse files Browse the repository at this point in the history
just return a 'undefined' RuntimeError).
Other small improvements.
  • Loading branch information
ronyeh committed Aug 29, 2021
1 parent a58030c commit 38653e8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/tickable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface FormatterMetrics {
*/
export abstract class Tickable extends Element {
protected ignore_ticks: boolean;
tupletStack: Tuplet[];
protected tupletStack: Tuplet[];
protected tuplet?: Tuplet;
protected ticks: Fraction;
protected center_x_shift: number;
Expand Down Expand Up @@ -118,8 +118,9 @@ export abstract class Tickable extends Element {
}

/** Ignore the ticks. */
setIgnoreTicks(flag: boolean): void {
setIgnoreTicks(flag: boolean): this {
this.ignore_ticks = flag;
return this;
}

/** Set width of note. Used by the formatter for positioning. */
Expand Down Expand Up @@ -206,6 +207,11 @@ export abstract class Tickable extends Element {
return this.tuplet;
}

/** Return the intrinsic ticks. */
getTupletStack(): Tuplet[] {
return this.tupletStack;
}

/*
* Reset the specific Tuplet if this is not provided, all tuplets are reset.
* Remove any prior tuplets from the tick calculation and
Expand Down
5 changes: 2 additions & 3 deletions src/tickcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export interface TickContextOptions {
}

/**
* Tickcontext formats abstract tickable objects, such as notes, chords,
* tabs, etc.
* TickContext formats abstract tickable objects, such as notes, chords, tabs, etc.
*/
export class TickContext extends Tickable {
protected readonly tickID: number;
Expand All @@ -46,7 +45,7 @@ export class TickContext extends Tickable {
tContexts: TickContext[];

// eslint-disable-next-line
draw() {}
draw(...args: any[]): void {}

static getNextContext(tContext: TickContext): TickContext | undefined {
const contexts = tContext.tContexts;
Expand Down
2 changes: 1 addition & 1 deletion src/tuplet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class Tuplet extends Element {
// Count the tuplets that are on the same side (above/below)
// as this tuplet:
function countTuplets(note: Note, location: number) {
return note.tupletStack.filter((tuplet) => tuplet.location === location).length;
return note.getTupletStack().filter((tuplet) => tuplet.location === location).length;
}

this.notes.forEach((note) => {
Expand Down
9 changes: 6 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export class RuntimeError extends Error {
}
}

/** Check that `x` is of type `T` and not `undefined`. */
export function check<T>(x?: T): T {
/**
* Check that `x` is of type `T` and not `undefined`.
* If `x` is `undefined`, throw a RuntimeError with the optionally provided error code and message.
*/
export function check<T>(x?: T, code: string = 'undefined', message: string = ''): T {
if (x === undefined) {
throw new RuntimeError('undefined');
throw new RuntimeError(code, message);
}
return x;
}
Expand Down

0 comments on commit 38653e8

Please sign in to comment.