Skip to content

[context] suggestion: only keeping weak references to subscribers in providers #64

@jsebrech

Description

@jsebrech

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions