Skip to content

Commit

Permalink
fix: Reverts unintentional breaking API change to getRoller
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Oct 2, 2024
1 parent a0ddb12 commit 2aff04b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 75 deletions.
74 changes: 33 additions & 41 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class APIInstance {
raw: string,
source: string = "",
options: RollerOptions = this.getRollerOptions(this.data)
): Option<BasicRoller> {
): BasicRoller | null {
const {
content,
position,
Expand All @@ -229,7 +229,7 @@ class APIInstance {

if (lexemeResult.isErr()) {
console.error(lexemeResult.unwrapErr());
return None;
return null;
}
const lexemes = lexemeResult.unwrap();

Expand All @@ -253,7 +253,7 @@ class APIInstance {
roller.showRenderNotice = this.data.showRenderNotice;

roller.setSource(source);
return Some(roller);
return roller;
}
case "table": {
const roller = new TableRoller(
Expand All @@ -265,18 +265,16 @@ class APIInstance {
position,
lookup
);
return Some(roller);
return roller;
}
case "section": {
return Some(
new SectionRoller(
this.data,
content,
lexemes[0],
source,
this.app,
position
)
return new SectionRoller(
this.data,
content,
lexemes[0],
source,
this.app,
position
);
}
case "dataview": {
Expand All @@ -285,15 +283,13 @@ class APIInstance {
"Tags are only supported with the Dataview plugin installed."
);
}
return Some(
new DataViewRoller(
this.data,
content,
lexemes[0],
source,
this.app,
position
)
return new DataViewRoller(
this.data,
content,
lexemes[0],
source,
this.app,
position
);
}
case "tag": {
Expand All @@ -302,27 +298,23 @@ class APIInstance {
"Tags are only supported with the Dataview plugin installed."
);
}
return Some(
new TagRoller(
this.data,
content,
lexemes[0],
source,
this.app,
position
)
return new TagRoller(
this.data,
content,
lexemes[0],
source,
this.app,
position
);
}
case "line": {
return Some(
new LineRoller(
this.data,
content,
lexemes[0],
source,
this.app,
position
)
return new LineRoller(
this.data,
content,
lexemes[0],
source,
this.app,
position
);
}
}
Expand Down Expand Up @@ -387,7 +379,7 @@ class APIInstance {
}
public async parseDice(content: string, source: string = "") {
const roller = await this.getRoller(content, source);
return { result: await roller.unwrap()?.roll(), roller };
return { result: await roller?.roll(), roller };
}
getRollerOptions(data: DiceRollerSettings): RollerOptions {
return {
Expand Down
5 changes: 2 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ export default class DiceRollerPlugin extends Plugin {

this.registerEvent(
this.app.workspace.on("dice-roller:render-dice", async (roll) => {
const maybeRoller = await API.getRoller(roll, "external");
if (maybeRoller.isNone()) {
const roller = await API.getRoller(roll, "external");
if (roller == null) {
return;
}
const roller = maybeRoller.unwrap();
if (!(roller instanceof StackRoller)) {
new Notice("The Dice View only supports dice rolls.");
return;
Expand Down
13 changes: 4 additions & 9 deletions src/processor/live-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,11 @@ function inlineRender(
);

const currentFile = plugin.app.workspace.getActiveFile();
const maybeRoller = API.getRoller(
content,
currentFile.path
);
const roller = API.getRoller(content, currentFile.path);

if (maybeRoller.isNone()) {
if (roller == null) {
return;
}
const roller = maybeRoller.unwrap();
roller.roll().then(async () => {
const replacer = await roller.getReplacer();
const insert = `${replacer}`;
Expand All @@ -140,11 +136,10 @@ function inlineRender(
let [, content] = original.match(
/^dice(?:\+|\-|\-mod)?:\s*([\s\S]+)\s*?/
);
const maybeRoller = API.getRoller(content, currentFile.path);
if (maybeRoller.isNone()) {
const roller = API.getRoller(content, currentFile.path);
if (roller == null) {
return;
}
const roller = maybeRoller.unwrap();
roller.addContexts(component, plugin);
const widget = new InlineWidget(
original,
Expand Down
11 changes: 4 additions & 7 deletions src/processor/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ export default class DiceProcessor extends Component {
continue;

//build result map;
const maybeRoller = API.getRoller(content, ctx.sourcePath);
if (maybeRoller.isNone()) {
const roller = API.getRoller(content, ctx.sourcePath);
if (roller == null) {
return;
}
const roller = maybeRoller.unwrap();
if (roller instanceof StackRoller && roller.shouldRender) {
roller.hasRunOnce = true;
}
Expand Down Expand Up @@ -143,13 +142,11 @@ export default class DiceProcessor extends Component {
}
try {
//build result map;
const maybeRoller = API.getRoller(content, ctx.sourcePath);
if (maybeRoller.isNone()) {
const roller = API.getRoller(content, ctx.sourcePath);
if (roller == null) {
return;
}
/** Add the roller to the child context, so it can be unloaded with the context. */
const roller = maybeRoller.unwrap();

roller.onLoad(async () => {
await roller.roll();

Expand Down
15 changes: 6 additions & 9 deletions src/rollers/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,10 @@ export class TableRoller extends GenericFileRoller<string> {
const formula = foundRoller[1].trim();

// Create sub roller with formula
const maybeRoller = await API.getRoller(formula, this.source);
if (maybeRoller.isNone()) {
const subRoller = await API.getRoller(formula, this.source);
if (subRoller == null) {
continue;
}
const subRoller = maybeRoller.unwrap();
subRoller.addContexts(...this.components);
// Roll it
await subRoller.roll();
Expand Down Expand Up @@ -198,12 +197,11 @@ export class TableRoller extends GenericFileRoller<string> {

if (this.rollsFormula) {
try {
const maybeRoller = await API.getRoller(
const roller = await API.getRoller(
this.rollsFormula,
this.source
);
if (maybeRoller.isSome()) {
const roller = maybeRoller.unwrap();
if (roller) {
if (!(roller instanceof StackRoller)) {
this.prettyTooltip =
"TableRoller only supports dice rolls to select multiple elements.";
Expand Down Expand Up @@ -346,16 +344,15 @@ export class TableRoller extends GenericFileRoller<string> {
) ||
this.lookup
) {
const maybeRoller = await API.getRoller(
const roller = await API.getRoller(
this.lookup ??
Array.from(table.columns.keys())[0]
.split(":")
.pop()
.replace(/\`/g, ""),
this.source
);
if (maybeRoller.isSome()) {
const roller = maybeRoller.unwrap();
if (roller) {
roller.addContexts(...this.components);
if (roller instanceof StackRoller) {
this.lookupRoller = roller;
Expand Down
10 changes: 4 additions & 6 deletions src/view/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,8 @@ export default class DiceView extends ItemView {
const diceFormula = /^(?:1)?d(\d|%|F)+$/.test(icon.formula)
? `${Math.abs(amount)}${icon.formula.replace(/^1/, "")}`
: `${Math.abs(amount)} * (${icon.formula})`;
const maybeRoller = API.getRoller(icon.formula, VIEW_TYPE);
if (maybeRoller.isNone()) continue;
const roller = maybeRoller.unwrap();
const roller = API.getRoller(icon.formula, VIEW_TYPE);
if (roller == null) continue;
if (!(roller instanceof StackRoller)) continue;
roller.buildDiceTree();
roller.calculate();
Expand Down Expand Up @@ -258,9 +257,8 @@ export default class DiceView extends ItemView {
opts.expectedValue = ExpectedValue.Roll;
}
try {
const maybeRoller = await API.getRoller(formula, VIEW_TYPE, opts);
if (maybeRoller.isNone()) return;
const roller = maybeRoller.unwrap();
const roller = await API.getRoller(formula, VIEW_TYPE, opts);
if (roller == null) return;
if (!(roller instanceof StackRoller)) {
throw new Error("The Dice Tray only supports dice rolls.");
}
Expand Down

0 comments on commit 2aff04b

Please sign in to comment.