Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion patch-channel-store-methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { ReflectApply } = require('./primordials.js');
const {
ReflectApply,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor
} = require('./primordials.js');

module.exports = function (unpatched) {
const channels = new WeakSet();
Expand Down Expand Up @@ -48,6 +52,14 @@ module.exports = function (unpatched) {
return run();
};

if (!ObjectGetOwnPropertyDescriptor(ch, 'hasSubscribers')) {
ObjectDefineProperty(ch, 'hasSubscribers', {
get: function() {
return this.__proto__.hasSubscribers || ch._stores.size > 0;
}
});
}

channels.add(ch);

return ch;
Expand Down
4 changes: 3 additions & 1 deletion patch-garbage-collection-bug.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ module.exports = function(unpatched) {
get: function() {
const subscribers = ch._subscribers;
if (subscribers.length > 1) return true;
if (subscribers.length < 1) return false;
const stores = ch._stores;
if (stores.size > 0) return true;
if (subscribers.length < 1 ) return false;
if (subscribers[0] === PHONY_SUBSCRIBE) return false;
return true;
},
Expand Down
41 changes: 41 additions & 0 deletions test/diagnostics-channel-store-has-subscribers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const test = require('tape');

const { channel, hasSubscribers } = require('../dc-polyfill.js');

const { MAJOR, MINOR } = require('../checks.js');

test('diagnostics-channel-store-has-subscribers', t => {
if (MAJOR === 15 && MINOR === 1) {
// node:diagnostics_channel:110
// if (ref) channel = WeakRefPrototypeGet(ref);
// TypeError: WeakRefPrototypeGet is not a function
t.comment('SKIPPING TEST DUE TO A BUG IN THIS VERSION OF NODE.JS');
t.end();
return;
}

const dc = channel('diagnostics-channel-store-has-subscribers');
t.ok(!hasSubscribers('diagnostics-channel-store-has-subscribers'));

const store = {};
dc.bindStore(store);
t.ok(hasSubscribers('diagnostics-channel-store-has-subscribers'));

dc.unbindStore(store);
t.ok(!hasSubscribers('diagnostics-channel-store-has-subscribers'));

const handler = () => {};
dc.bindStore(store);
dc.subscribe(handler);
t.ok(hasSubscribers('diagnostics-channel-store-has-subscribers'));

dc.unsubscribe(handler);
t.ok(hasSubscribers('diagnostics-channel-store-has-subscribers'));

dc.unbindStore(store);
t.ok(!hasSubscribers('diagnostics-channel-store-has-subscribers'));

t.end();
});