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

Fixed negative flag #13

Merged
merged 4 commits into from
Jan 15, 2019
Merged

Fixed negative flag #13

merged 4 commits into from
Jan 15, 2019

Conversation

CodeLenny
Copy link
Owner

Closes #10

Copy link

@andrewvsk andrewvsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you can optimize your implementation:

Assertion.addMethod('emit', function(name, timeout) {
    timeout   = timeout || 1500;
    const obj = utils.flag(this, 'object'); // this._obj

    const assert = expr => {
         //                         .not = false                              .not = true
         this.assert(expr, `expected #{this} to emit '${name}'`, `expected #{this} to not emit '${name}'`);
    };

    return new Promise((resolve, reject) => {
        let done = false;

        obj.once(name, (...args) => {
            try {
                if (done) return;
                done = true;

                assert(true);   // OK | ERROR (.not = true),

                resolve(args);
            } catch (err) {
                reject(err);
            }
        });

        setTimeout(() => {
            try {
                if (done) return;
                done = true;

                assert(false); // ERROR | OK (.not = true),

                resolve();
            } catch (err) {
                reject(err);
            }
        }, timeout);
    });
});

'if (done)' - is unnecessary if on event clears timer. But with 'done' code looks more understandable.

chai-events.js Outdated Show resolved Hide resolved
@CodeLenny CodeLenny merged commit 924c159 into master Jan 15, 2019
@CodeLenny CodeLenny deleted the iss10-negative-flag branch January 15, 2019 21:58
This was referenced Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants