-
Notifications
You must be signed in to change notification settings - Fork 16
Description
The context provider implementation needs to track subscribers, and a natural solution to do so is a Map. The lit/context implementation for example uses a Map. However, this expects all requesters to be on their best behavior and always call unsubscribe on disconnect. It is very likely that many implementations will not do so, and this is likely to lead to memory leaks.
The current context spec only mentions a risk of memory leaks in this paragraph, but this is not sufficient to guard against the above scenario:
The provider MUST NOT retain a reference to the callback nor pass an unsubscribe callback if the context-request event's subscribe property is not truthy. Doing so may cause a memory leak as the consumer may not ever call the unsubscribe callback.
Suggestion: add implementation advice to the context spec that providers use only weak references to track subscribers (e.g. WeakMap) so that all subscribers can be garbage collected even if they never explicitly call the unsubscribe function. In short, subscribing to a context should not prevent garbage collection if that is the only reference to an element or its callback.