Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bartosz-oczujda opened this issue Oct 30, 2016 · 0 comments
Open
Assignees

Comments

@bartosz-oczujda
Copy link
Contributor

bartosz-oczujda commented Oct 30, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant