Mocking interfaces (+adding new method to canvas to make it more JS-like)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Adsooi 2024-10-12 04:57:07 +02:00
parent 885d1f5dc3
commit 4c1b705240
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
11 changed files with 216 additions and 13 deletions

View file

@ -522,8 +522,9 @@ class CanvasAPI extends Module {
const onRendered = (imgData) => {
if(!this.#canvas.isImageLoaded(imgData.source) && !this.#canvas.isImageLoading(imgData.source)) {
// Wait until the image is loaded to callback.
this.#canvas.loadImage(imgData.source)
this.#canvas.imageLoaders[imgData.source] = [callback, imgData]
this.#canvas.loadImageAsync(imgData.source).then(() => {
callback(imgData)
})
} else {
// Callback directly
callback(imgData)

View file

@ -57,7 +57,7 @@ export class Module extends BaseEventEmitter {
if(!options.hasOwnProperty(name))
throw new Error(`Option '${name}' of initialize of module ${this.#name} does not exist.`)
if(typeof value === "function" && value.prototype instanceof Interface)
Interface.check_implementation(value, options[name])
Interface.checkImplementation(value, options[name])
else if(typeof value !== typeof options[name])
throw new Error(`Option '${name}' of initialize of module ${this.#name} is not a '${value}' (${typeof options[name]}).`)
}

View file

@ -35,9 +35,8 @@ export class Interface {
* Throws an error if the implementation does not conform to the interface.
* @param {typeof Interface} interface_
* @param {object} classToCheck
* @return {boolean}
*/
static check_implementation(interface_, classToCheck) {
static checkImplementation(interface_, classToCheck) {
const properties = new interface_()
const interfaceName = interface_.name
const toCheckName = classToCheck.constructor.name
@ -52,7 +51,7 @@ export class Interface {
else if((typeof value) === "object")
// Test type of object.
if(value instanceof Interface)
Interface.check_implementation(value, classToCheck[property])
Interface.checkImplementation(value, classToCheck[property])
else if(value.prototype && !(classToCheck[property] instanceof value))
throw new Error(`Property '${property}' of ${interfaceName} implementation ${toCheckName} is not '${value.constructor.name}'.`)
}
@ -61,13 +60,12 @@ export class Interface {
export class CanvasInterface extends Interface {
imageLoaders = OBJECT
/** @type {function(string): CanvasRenderingContext2D} */
getContext = FUNCTION
/** @type {function(rect)} */
markDirty = FUNCTION
/** @type {function(string)} */
loadImage = FUNCTION
/** @type {function(string): Promise} */
loadImageAsync = FUNCTION
/** @type {function(string)} */
isImageLoading = FUNCTION
/** @type {function(string)} */