Skip to content

Commit 687b144

Browse files
committed
Improving reliability of threaded rendering, separating JS Utils into separate files.
1 parent 49e9431 commit 687b144

File tree

18 files changed

+285
-197
lines changed

18 files changed

+285
-197
lines changed

common/src/history/position.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import Objects from "../module/objects.mjs"
2020
import Latex from "../module/latex.mjs"
2121
import * as MathLib from "../math/index.mjs"
22-
import { escapeHTML } from "../utils.mjs"
22+
import { escapeHTML } from "../utils/index.mjs"
2323
import { Action } from "./common.mjs"
2424

2525
/**

common/src/index.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
import js from "./lib/polyfills/js.mjs"
2020

21-
import * as Modules from "./module/index.mjs"
21+
export * as Utils from "./utils/index.mjs"
22+
2223
import * as ObjsAutoload from "./objs/autoload.mjs"
2324

25+
export * as Modules from "./module/index.mjs"
2426
export * as MathLib from "./math/index.mjs"
2527
export * as HistoryLib from "./history/index.mjs"
2628
export * as Parsing from "./parsing/index.mjs"
27-
export * as Utils from "./utils.mjs"

common/src/math/expression.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
*/
1818

1919

20-
import * as Utils from "../utils.mjs"
20+
import * as Utils from "../utils/index.mjs"
21+
import { ExprEvalExpression } from "../lib/expr-eval/expression.mjs"
2122
import Latex from "../module/latex.mjs"
2223
import ExprParser from "../module/expreval.mjs"
2324
import Objects from "../module/objects.mjs"
24-
import { ExprEvalExpression } from "../lib/expr-eval/expression.mjs"
2525

2626
const NUMBER_MATCHER = /^\d*\.\d+(e[+-]\d+)?$/
2727

common/src/math/sequence.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import * as Expr from "./expression.mjs"
20-
import * as Utils from "../utils.mjs"
20+
import * as Utils from "../utils/index.mjs"
2121
import Latex from "../module/latex.mjs"
2222
import Objects from "../module/objects.mjs"
2323
import ExprParser from "../module/expreval.mjs"

common/src/module/canvas.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import { Module } from "./common.mjs"
2020
import { CanvasInterface, DialogInterface } from "./interface.mjs"
21-
import { textsup } from "../utils.mjs"
21+
import { textsup } from "../utils/index.mjs"
2222
import { Expression } from "../math/index.mjs"
2323
import Latex from "./latex.mjs"
2424
import Objects from "./objects.mjs"

common/src/module/latex.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class LatexAPI extends Module {
9797
* true if latex has been enabled by the user, false otherwise.
9898
*/
9999
this.enabled = false
100+
this.promises = new Set()
100101
}
101102

102103
/**
@@ -139,9 +140,12 @@ class LatexAPI extends Module {
139140
if(!this.initialized) throw new Error("Attempting requestAsyncRender before initialize!")
140141
let render
141142
if(this.#latex.supportsAsyncRender) {
142-
console.trace()
143143
this.emit(new AsyncRenderStartedEvent(markup, fontSize, color))
144-
render = await this.#latex.renderAsync(markup, fontSize, color)
144+
// Storing promise so that it does not get dereferenced.
145+
const promise = this.#latex.renderAsync(markup, fontSize, color)
146+
this.promises.add(promise)
147+
render = await promise
148+
this.promises.delete(promise)
145149
this.emit(new AsyncRenderFinishedEvent(markup, fontSize, color))
146150
} else {
147151
render = this.#latex.renderSync(markup, fontSize, color)

common/src/module/objects.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import { Module } from "./common.mjs"
20-
import { textsub } from "../utils.mjs"
20+
import { textsub } from "../utils/index.mjs"
2121

2222
class ObjectsAPI extends Module {
2323

common/src/objs/common.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19-
import { getRandomColor } from "../utils.mjs"
2019
import Objects from "../module/objects.mjs"
2120
import Latex from "../module/latex.mjs"
21+
import { getRandomColor } from "../utils/index.mjs"
2222
import { ensureTypeSafety, serializesByPropertyType } from "../parameters.mjs"
2323

2424
// This file contains the default data to be imported from all other objects

common/src/objs/function.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19-
import { textsub } from "../utils.mjs"
19+
import { textsub } from "../utils/index.mjs"
2020
import Objects from "../module/objects.mjs"
2121
import { ExecutableObject } from "./common.mjs"
2222
import { parseDomain, Expression, SpecialDomain } from "../math/index.mjs"

common/src/utils.mjs renamed to common/src/utils/expression.mjs

Lines changed: 1 addition & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -16,151 +16,7 @@
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19-
// Add string methods
20-
/**
21-
* Replaces latin characters with their uppercase versions.
22-
* @return {string}
23-
*/
24-
String.prototype.toLatinUppercase = function() {
25-
return this.replace(/[a-z]/g, function(match) {
26-
return match.toUpperCase()
27-
})
28-
}
29-
30-
/**
31-
* Removes the first and last character of a string
32-
* Used to remove enclosing characters like quotes, parentheses, brackets...
33-
* @note Does NOT check for their existence ahead of time.
34-
* @return {string}
35-
*/
36-
String.prototype.removeEnclosure = function() {
37-
return this.substring(1, this.length - 1)
38-
}
39-
40-
/**
41-
* Rounds to a certain number of decimal places.
42-
* From https://stackoverflow.com/a/48764436
43-
*
44-
* @param {number} decimalPlaces
45-
* @return {number}
46-
*/
47-
Number.prototype.toDecimalPrecision = function(decimalPlaces = 0) {
48-
const p = Math.pow(10, decimalPlaces)
49-
const n = (this * p) * (1 + Number.EPSILON)
50-
return Math.round(n) / p
51-
}
52-
53-
const CHARACTER_TO_POWER = new Map([
54-
["-", "⁻"],
55-
["+", "⁺"],
56-
["=", "⁼"],
57-
[" ", " "],
58-
["(", "⁽"],
59-
[")", "⁾"],
60-
["0", "⁰"],
61-
["1", "¹"],
62-
["2", "²"],
63-
["3", "³"],
64-
["4", "⁴"],
65-
["5", "⁵"],
66-
["6", "⁶"],
67-
["7", "⁷"],
68-
["8", "⁸"],
69-
["9", "⁹"],
70-
["a", "ᵃ"],
71-
["b", "ᵇ"],
72-
["c", "ᶜ"],
73-
["d", "ᵈ"],
74-
["e", "ᵉ"],
75-
["f", "ᶠ"],
76-
["g", "ᵍ"],
77-
["h", "ʰ"],
78-
["i", "ⁱ"],
79-
["j", "ʲ"],
80-
["k", "ᵏ"],
81-
["l", "ˡ"],
82-
["m", "ᵐ"],
83-
["n", "ⁿ"],
84-
["o", "ᵒ"],
85-
["p", "ᵖ"],
86-
["r", "ʳ"],
87-
["s", "ˢ"],
88-
["t", "ᵗ"],
89-
["u", "ᵘ"],
90-
["v", "ᵛ"],
91-
["w", "ʷ"],
92-
["x", "ˣ"],
93-
["y", "ʸ"],
94-
["z", "ᶻ"]
95-
])
96-
97-
const CHARACTER_TO_INDICE = new Map([
98-
["-", "₋"],
99-
["+", "₊"],
100-
["=", "₌"],
101-
["(", "₍"],
102-
[")", "₎"],
103-
[" ", " "],
104-
["0", "₀"],
105-
["1", "₁"],
106-
["2", "₂"],
107-
["3", "₃"],
108-
["4", "₄"],
109-
["5", "₅"],
110-
["6", "₆"],
111-
["7", "₇"],
112-
["8", "₈"],
113-
["9", "₉"],
114-
["a", "ₐ"],
115-
["e", "ₑ"],
116-
["h", "ₕ"],
117-
["i", "ᵢ"],
118-
["j", "ⱼ"],
119-
["k", "ₖ"],
120-
["l", "ₗ"],
121-
["m", "ₘ"],
122-
["n", "ₙ"],
123-
["o", "ₒ"],
124-
["p", "ₚ"],
125-
["r", "ᵣ"],
126-
["s", "ₛ"],
127-
["t", "ₜ"],
128-
["u", "ᵤ"],
129-
["v", "ᵥ"],
130-
["x", "ₓ"]
131-
])
132-
133-
const EXPONENTS = [
134-
"⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"
135-
]
136-
137-
const EXPONENTS_REG = new RegExp("([" + EXPONENTS.join("") + "]+)", "g")
138-
139-
/**
140-
* Put a text in sup position
141-
* @param {string} text
142-
* @return {string}
143-
*/
144-
export function textsup(text) {
145-
let ret = ""
146-
text = text.toString()
147-
for(let letter of text)
148-
ret += CHARACTER_TO_POWER.has(letter) ? CHARACTER_TO_POWER.get(letter) : letter
149-
return ret
150-
}
151-
152-
/**
153-
* Put a text in sub position
154-
* @param {string} text
155-
* @return {string}
156-
*/
157-
export function textsub(text) {
158-
let ret = ""
159-
text = text.toString()
160-
for(let letter of text)
161-
ret += CHARACTER_TO_INDICE.has(letter) ? CHARACTER_TO_INDICE.get(letter) : letter
162-
return ret
163-
}
19+
import { textsub, textsup } from "./subsup.mjs"
16420

16521
/**
16622
* Simplifies (mathematically) a mathematical expression.
@@ -400,35 +256,3 @@ export function parseName(str, removeUnallowed = true) {
400256

401257
return str
402258
}
403-
404-
/**
405-
* Creates a randomized color string.
406-
* @returns {string}
407-
*/
408-
export function getRandomColor() {
409-
let clrs = "0123456789ABCDEF"
410-
let color = "#"
411-
for(let i = 0; i < 6; i++) {
412-
color += clrs[Math.floor(Math.random() * (16 - 5 * (i % 2 === 0)))]
413-
}
414-
return color
415-
}
416-
417-
/**
418-
* Escapes text to html entities.
419-
* @param {string} str
420-
* @returns {string}
421-
*/
422-
export function escapeHTML(str) {
423-
return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
424-
}
425-
426-
427-
/**
428-
* Parses exponents and replaces them with expression values
429-
* @param {string} expression - The expression to replace in.
430-
* @return {string} The parsed expression
431-
*/
432-
export function exponentsToExpression(expression) {
433-
return expression.replace(EXPONENTS_REG, (m, exp) => "^" + exp.split("").map((x) => EXPONENTS.indexOf(x)).join(""))
434-
}

0 commit comments

Comments
 (0)