Conversation
630864f
to
0aa23d6
Compare
0aa23d6
to
d3f7248
Compare
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.
A few questions
src/QueuingEvented.ts
Outdated
|
||
emit<E extends EventObject>(event: E): void { | ||
super.emit(event); | ||
(function (proto) { |
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.
How come this is needed?
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.
Yes, couldn't something like this be accomplished with the mixin pattern?
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'll look into the mixin pattern
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 think this is left-over from when the signature of emit
and on
were much more complex than they are now and I was trying to avoid repeating the signatures in QueingEvented
. I've fixed these up locally to be methods on QueingEvented
.
const handles = Object.keys(listenerMapArg).map((type) => this.on(type, listenerMapArg[type])); | ||
on<K extends keyof M>(type: K, listener: EventedCallbackOrArray<K, M[K]>): Handle; | ||
on(type: T, listener: EventedCallbackOrArray<T, O>): Handle; | ||
on(type: any, listener: EventedCallbackOrArray<any, any>): Handle { |
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.
Assume this can't be T | K
?
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.
Correct. K
and T
need to be specifically associated with the correct EventedCallbackOrArray
. Using a union type means that T
could be associated with EventedCallbackOrArray<K, M[K]>
which we don't want.
src/Evented.ts
Outdated
// The following member is purely so TypeScript remembers the type of `M` when extending so | ||
// that the utilities in `on.ts` will work | ||
// tslint:disable-next-line | ||
protected __typeMap__?: M; |
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 seems is a bit hacky :(
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.
Yeah, feels dirty to me too... 😢
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.
Yeah, it's dirty, but I'm not sure if there's any other way. TypeScript isn't able to remember the type map for use with the stand-alone on()
function without it.
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've filed an issue with a reduced test case outlining this problem. It feels like a bug.
src/Evented.ts
Outdated
this.listenersMap.forEach((method, type) => { | ||
if (isGlobMatch(type, event.type)) { | ||
if (isGlobMatch(<any> type, event.type)) { |
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.
Why do we need <any>
here?
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.
Yes, lets try to comment whenever we have to cast as any
(as well as prefer the type as any
instead of the arrow brackets.
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.
Because type
is a generic that can be anything and isGlobMatch()
requires string | symbol
. Also, casting type
to string | symbol
doesn't work either.
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's try to comment whenever we have to cast as
any
As in, add a comment to code for future generations, please.
src/QueuingEvented.ts
Outdated
this.listenersMap.forEach((method, type) => { | ||
if (isGlobMatch(type, event.type)) { | ||
hasMatch = true; | ||
} | ||
}); | ||
|
||
if (!hasMatch) { | ||
// @ts-ignore | ||
let queue = this._queue.get(event.type); |
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.
What are we ignoring here?
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.
yeah, we really should comment why we are deciding to ignore something and would much prefer casting (and a comment)... I have yet to see a valid use case yet for // @ts-ignore
in our code base.
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.
These were because _queue
and listenersMap
weren't accessible outside of the class declaration of QueuingEvented
. Since I've moved the functions into the class definition, these comments are no longer needed.
if (Date.now() - milliseconds > start) { | ||
return Promise.reject<T>(reason); | ||
} | ||
if (typeof value === 'function') { |
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.
Is this new functionality?
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.
Doesn't look like it's covered in tests
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.
It's not new functionality (value
was T | (() => T | Thenable<T>)
before my changes), but it wasn't handled properly before. You're right that we need to test it.
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.
Tests have been added
Would be great if @kitsonk could also review |
const { handles } = this; | ||
handles.push(handle); | ||
own(handles: Handle | Handle[]): Handle { | ||
const handle = Array.isArray(handles) ? createCompositeHandle(...handles) : handles; |
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.
We are missing a test branch here
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.
Tests added
handles.push(handle); | ||
own(handles: Handle | Handle[]): Handle { | ||
const handle = Array.isArray(handles) ? createCompositeHandle(...handles) : handles; | ||
const { handles: _handles } = this; |
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.
Why the rename?
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.
Locally, handles
is Handle | Handle[]
and this.handles
is Handle[]
. Assigning works fine initially, but when it gets into the destroy()
function returned, the compiler reverts to thinking that handles
is Handle | Handle[]
and requires a cast. I thought a rename was cleaner than a cast.
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.
👍
src/Evented.ts
Outdated
// The following member is purely so TypeScript remembers the type of `M` when extending so | ||
// that the utilities in `on.ts` will work | ||
// tslint:disable-next-line | ||
protected __typeMap__?: M; |
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.
Yeah, feels dirty to me too... 😢
src/Evented.ts
Outdated
this.listenersMap.forEach((method, type) => { | ||
if (isGlobMatch(type, event.type)) { | ||
if (isGlobMatch(<any> type, event.type)) { |
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.
Yes, lets try to comment whenever we have to cast as any
(as well as prefer the type as any
instead of the arrow brackets.
src/QueuingEvented.ts
Outdated
|
||
emit<E extends EventObject>(event: E): void { | ||
super.emit(event); | ||
(function (proto) { |
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.
Yes, couldn't something like this be accomplished with the mixin pattern?
tests/unit/aspect.ts
Outdated
@@ -4,7 +4,7 @@ const { registerSuite } = intern.getInterface('object'); | |||
const { assert } = intern.getPlugin('chai'); | |||
import * as sinon from 'sinon'; | |||
import * as aspect from '../../src/aspect'; | |||
import { Handle } from '@dojo/interfaces/core'; | |||
import { Handle } from '../../src/interfaces'; |
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.
We added support for symbols, but no tests for symbols.
f571bcf
to
498d9fb
Compare
Type: enhancement
The following has been addressed in the PR:
Description:
Relates to dojo/meta#210