Iterator interface for LanguageService #15168
Labels
API
Relates to the public API for TypeScript
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Currently, functions like
LanguageService.getReferencesAtPosition()
orgetNavigateToItems()
return an array. That means, they have to collect all references/navigation items before returning, and blocks the CPU until then.If you invoke such a function in repos like angular/angular the returned array can easily have 50k elements and the function can take a very long time to return. That means a UI can only show the results until all items have been aggregated (of course, you can pass a limit, but in the case of
getNavigateToItems()
that can result in the relevant items you wanted not being found because matching is done fuzzily and the function returns early whenlimit
items that matched the query fuzzily were found. For example, in angular/angular, a query for Http with a limit will return a lot of not-relevanthttp
variables, but a query without a limit will return theHttp
class). If you have to do more filtering, transformation etc to the items you end up with an unneeded extra iteration.I would like to propose to add or change the API to return an
Iterable
instead. This can be achieved with generators. This would allow the consumer to pull items lazily from the iterator.That means
break
in afor of
)There is no dependency needed to make this work, it's all in the language.
The returned Iterable can be consumed in a variety of ways, with
for of
, generator delegation, with iteration libraries or Observables, or easily coverted to an Array withArray.from()
.This API could be added in a backwards-compatible way by adding a new variant and changing the old one to delegate and convert to array:
The text was updated successfully, but these errors were encountered: