-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
Add debug functionality #53
Conversation
Not sure why codecov doesn't pickup that I test the |
index.js
Outdated
@@ -181,16 +181,37 @@ class Emittery { | |||
}; | |||
} | |||
|
|||
constructor() { | |||
static isDebug() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a getter.
index.js
Outdated
|
||
static debugLogger(type, debugName, eventName, eventData) { | ||
if (typeof eventName !== 'string') { | ||
eventName = eventName.toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is moot. Template interpolation already calls toString.
Share your opinion on these questions (with reasonings). |
I've implemented the requested changes, updated documentation and tests where it was needed. TimestampsI think it would be useful only in specific scenarios if the default logging function printed out timestamps as well. The use case I can currently think of where time is relevant is an application where an emit happens periodically e.g. every 10 seconds. I think a Verbosity filterI think this is useful because with multiple events and multiple instances of Emittery the logs could get cluttered very quickly if we log every subscribe/unsubscribe event too. I can think of two approaches that would be good here.
The other thing that needs to be decided here is where we implement the verbosity setting, in the Emittery class, so it affects all instances at once, or on a per instance level, so it's more precisely controllable. However on the per instance level we would have to manually update every instance if we wanted to change the setting for all instances. Static logger functionI think it would be better to have this on a per instance basis, but I guess this question is kind of the same as the implementation of the verbosity filter. The benefit of logging on a per instance basis would be that the different instances could access potentially useful data that is only available in their context. But again the drawback is that we lose the ability to update the logging function of all instances all at once. |
Sorry, I just realized I missed the notification for this. |
I think timestamp can be useful for comparing how far from eachother certain events are. I agree, the date is not very useful, but it should follow the ISO9601 minus the date, so
I'm not sold on this. I think it's better to just be verbose by default and we can add modes to make it less verbose when users requests it from real-world experience.
Yeah, it should be on the instance. I think it should be The docs and TS docs comments needs more work regarding formatting and typos. |
Here are some of the changes I've made:
Let me know if I've missed anything, or there's something that could be done in a better way P.S. finally I pass all the checks yay! |
index.d.ts
Outdated
|
||
To enable this feature set the DEBUG environment variable to 'emittery' or '*'. Additionally you can set the static `isDebug` variable to true on the Emittery class, or `myEmitter.debug.enabled` on an instance of it for debugging a single instance. | ||
|
||
See API for more information on how debugging works |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All sentences should end with a .
index.d.ts
Outdated
|
||
See API for more information on how debugging works | ||
*/ | ||
type DebugLogger = (type: string, debugName: string, eventName?: EventName, eventData?: Record<string, any>) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The event data here should be strongly-typed.
const emitter = new Emittery({debug: {name: 'myEmitter'}}); | ||
emitter.on('test', data => { // do something }); | ||
//=> [16:43:20.417][emittery:subscribe][myEmitter] Event Name: test | ||
// data: undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too cramped. Use more empty lines to make it more readable. The example should also include the part that emits the event. Generally, code examples should be runnable on their own.
Applies to all code examples.
index.d.ts
Outdated
/** | ||
Define a name for the instance of Emittery to use when outputting debug data. | ||
|
||
@default Empty string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a valid value for @default
.
index.d.ts
Outdated
// data: undefined | ||
``` | ||
*/ | ||
name: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name: string; | |
readonly name: string; |
index.js
Outdated
@@ -9,6 +9,8 @@ const resolvedPromise = Promise.resolve(); | |||
const listenerAdded = Symbol('listenerAdded'); | |||
const listenerRemoved = Symbol('listenerRemoved'); | |||
|
|||
let globalDebugFlag = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let globalDebugFlag = false; | |
let isGlobalDebugEnabled = false; |
index.js
Outdated
@@ -189,10 +191,42 @@ class Emittery { | |||
}; | |||
} | |||
|
|||
constructor() { | |||
static get isDebug() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static get isDebug() { | |
static get isDebugEnabled() { |
Just to make it clear.
|
||
if (!this.debug.enabled) { | ||
this.debug.enabled = false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get what the point of this is.
index.js
Outdated
@@ -219,6 +257,10 @@ class Emittery { | |||
assertEventName(eventName); | |||
getListeners(this, eventName).delete(listener); | |||
|
|||
if (Emittery.isDebug || this.debug.enabled) { | |||
this.debug.logger('unsubscribe', this.debug.name, eventName, undefined); | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is repeated many times. There should be a utility method for this.
readme.md
Outdated
### isDebug | ||
Enables/Disables debug mode for all instances |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### isDebug | |
Enables/Disables debug mode for all instances | |
### isDebug | |
Toggle debug mode for all instances. |
Formatting
The code looks good, but the docs formatting and text needs more work. |
I implemented the requested changes
if (typeof eventName === 'symbol') {
eventName = eventName.toString();
} Because it throws let s = Symbol('test');
console.log(`Symbol to string: ${s}`); This results in the same error, so I don't know if there's a way to work around this. |
readme.md
Outdated
|
||
###### name | ||
|
||
Type: `string` | ||
|
||
Default: `Empty string` | ||
Default: `null` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer using undefined
over null
.
emitter.on('test', data => { // do something }); | ||
emitter.emit('test'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Readme and index.d.ts should be in sync.
I would recommend writing some type tests to confirm it's correct: https://github.com/sindresorhus/emittery/blob/main/index.test-d.ts
Ah yeah. I forgot it's not interpolatable. |
I've implemented the changes, and wrote some type tests for the strongly-typed logger |
Looks good now. Thanks :) |
This PR aims to implement a built-in debug feature described in #2
How it's implemented
There's a static function in the
Emittery
class calleddebugLogger
, that takes in 4 parametersDebugging can be triggered in 3 ways currently:
DEBUG
environment variable is set to emittery or *isDebug
function in Emittery returns true (by default this handles the environment variable, but this function can be overridden, to enable package wide debugging instead of the 3rd method.isDebug
variable is set to true on one or more instances.Questions/suggestions
I wasn't sure if the default logger function should include some sort of timestamp.
It might add too much complexity but a verbosity option could be nice. For example currently every emit, subscribe, unsubscribe etc. event is logged, but in some cases they might not all be interesting. Of course this can be achieved by overriding the
debugLogger
function onEmittery
, but it would be a nice convenience option.Is it acceptable that there's one static logging function on
Emittery
or should the individual instanced of it be able to override this function. This would add the ability for different instances ofEmittery
to log differently based on their function in an application.Fixes #2
IssueHunt Summary
Referenced issues
This pull request has been submitted to:
IssueHunt has been backed by the following sponsors. Become a sponsor