From 07c9475ac5469161bc1a2cbebc7d989c9e0cc917 Mon Sep 17 00:00:00 2001 From: Oleksandr Danylchenko Date: Fri, 30 Aug 2024 18:11:37 +0300 Subject: [PATCH 1/3] Added context prototype comparison --- index.js | 8 ++++++-- test.js | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6d6a826..21e4513 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test.js b/test.js index 054602e..c014784 100644 --- a/test.js +++ b/test.js @@ -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'); From 0f4331f7b918ff74883282b15c1f35b099fdd079 Mon Sep 17 00:00:00 2001 From: Oleksandr Danylchenko Date: Mon, 2 Sep 2024 13:44:18 +0300 Subject: [PATCH 2/3] Added `throws` check assertion --- test.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test.js b/test.js index c014784..48aaeaf 100644 --- a/test.js +++ b/test.js @@ -157,15 +157,11 @@ test('context check in debounced function', async t => { instance1.debounced(); - let errorThrown = false; - try { + assert.throws(() => { instance2.debounced(); - } catch (error) { - errorThrown = true; - 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'); + }, { + message: 'Debounced method called with different contexts of the same prototype.', + }); }); }); From 2271ba1bf5c6629446b5437013ba2799263a8f05 Mon Sep 17 00:00:00 2001 From: Oleksandr Danylchenko Date: Mon, 2 Sep 2024 13:48:57 +0300 Subject: [PATCH 3/3] Added test for the different prototypes exception --- test.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 48aaeaf..26f89dd 100644 --- a/test.js +++ b/test.js @@ -147,7 +147,7 @@ test('forcing execution', async t => { }); test('context check in debounced function', async t => { - await t.test('should throw an error if debounced method is called with different contexts', async () => { + await t.test('should throw an error if debounced method is called with different contexts of the same class', async () => { function MyClass() {} MyClass.prototype.debounced = debounce(() => {}); @@ -161,7 +161,28 @@ test('context check in debounced function', async t => { instance2.debounced(); }, { message: 'Debounced method called with different contexts of the same prototype.', - }); + }, 'An error should have been thrown'); + }); + + await t.test('should not throw an error if debounced method is called with different contexts of different classes', async () => { + function MyClass1() {} + function MyClass2() {} + + const debouncedFunction = debounce(() => {}); + + MyClass1.prototype.debounced = debouncedFunction; + MyClass2.prototype.debounced = debouncedFunction; + + const instance1 = new MyClass1(); + const instance2 = new MyClass2(); + + instance1.debounced(); + + assert.doesNotThrow(() => { + instance2.debounced(); + }, { + message: 'Debounced method called with different contexts of the same prototype.', + }, 'An error should not have been thrown'); }); });