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 07c9475
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
&& this !== storedContext
&& 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 07c9475

Please sign in to comment.