Commit f6e37053 authored by Jakob Moser's avatar Jakob Moser
Browse files

Only have one description and verify handler each

Closes #29
parent a782deba
Loading
Loading
Loading
Loading
+9 −9
Changes for static/js/exercises.api.mjs: 9 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -43,21 +43,21 @@ class Exercise {
    }

    /**
     * Add a handler that is called every time the exercise executor
     * Set the handler that is called every time the exercise executor
     * sets the description of the exercise.
     * @param {Function} handler Function that takes a description as parameter
     */
    onDescribed(handler) {
        this.#context._describeHandlers.push(handler)
        this.#context._describeHandler = handler
    }

    /**
     * Add a handler that is called every time the exercise's completion
     * Set the handler that is called every time the exercise's completion
     * is verified (this verification might fail or be successful)
     * @param {Function} handler Function that takes the result of the verification (true/false) as parameter 
     */
    onVerified(handler) {
        this.#context._verifyHandlers.push(handler)
        this.#context._verifyHandler = handler
    }
}

@@ -70,8 +70,8 @@ class Exercise {
 */
class ExerciseExecutionContext {
    constructor() {
        this._describeHandlers = []
        this._verifyHandlers = []
        this._describeHandler = null
        this._verifyHandler = null
        this._confirmExerciseCompletion = null
    }

@@ -83,7 +83,7 @@ class ExerciseExecutionContext {
     * @param {String} description Description of the exercise
     */
    describe(description) {
        this._describeHandlers.forEach(handler => handler(description))
        this._describeHandler(description)
    }

    /**
@@ -161,7 +161,7 @@ class ExerciseExecutionContext {
     */
    verify(command) {
        runCommand(command)
        const verifyHandlers = this._verifyHandlers
        const verifyHandler = this._verifyHandler

        return {
            /**
@@ -195,7 +195,7 @@ class ExerciseExecutionContext {
                    // !! converts any value to a boolean (not a special operator, just two ! (boolean not) operators in series)
                    const result = !!predicate(latestEntry.output)

                    verifyHandlers.forEach(handler => handler(result))
                    verifyHandler(result)
                }, 200)
            }
        }