Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

dispatch() method contans duplicated code in multiple classes #1

@bartosz-oczujda

Description

@bartosz-oczujda

The dispatch() method in OnceSignal, MonoSignal & DeluxeSignal contains duplicated code regarding type checks for payloads:

        // If valueClasses is empty, value objects are not type-checked.
        var numValueClasses: number = this._valueClasses.length;
        var numValueObjects: number = valueObjects.length;

        // Cannot dispatch fewer objects than declared classes.
        if (numValueObjects < numValueClasses) {
            throw new Error('Incorrect number of arguments. ' +
                'Expected at least ' + numValueClasses + ' but received ' +
                numValueObjects + '.');
        }

        // Cannot dispatch differently typed objects than declared classes.
        for (var i: number = 0; i < numValueClasses; i++) {
            // Optimized for the optimistic case that values are correct.
            if (valueObjects[i] === null || valueObjects[i].constructor === this._valueClasses[i])
                continue;

            throw new Error('Value object <' + valueObjects[i]
                + '> is not an instance of <' + this._valueClasses[i] + '>.');
        }

Code duplication can be removed by delegating this logic to another class i.e. PayloadValidator

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions