Skip to content

Commit

Permalink
Added context prototype comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-danylchenko committed Aug 30, 2024
1 parent 312a06e commit 521c4d6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ function debounce(function_, wait = 100, options = {}) {
}

const debounced = function (...arguments_) {
if (storedContext && this !== storedContext) {
throw new Error('Debounced method called with different contexts.');
if (
storedContext &&

Check failure on line 44 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 20

'&&' should be placed at the beginning of the line.

Check failure on line 44 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 18

'&&' should be placed at the beginning of the line.
this !== storedContext &&

Check failure on line 45 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 20

'&&' should be placed at the beginning of the line.

Check failure on line 45 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 18

'&&' should be placed at the beginning of the line.
Object.getPrototypeOf(this) !== Object.getPrototypeOf(storedContext)
) {
throw new Error('Debounced method called with different contexts of the same prototype.');
}

storedContext = this; // eslint-disable-line unicorn/no-this-assignment
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test('context check in debounced function', async t => {
instance2.debounced();
} catch (error) {
errorThrown = true;
assert.strictEqual(error.message, 'Debounced method called with different contexts.', 'Error message should match');
assert.strictEqual(error.message, 'Debounced method called with different contexts of the same prototype.', 'Error message should match');
}

assert.ok(errorThrown, 'An error should have been thrown');
Expand Down

0 comments on commit 521c4d6

Please sign in to comment.