Skip to content

Commit 614282d

Browse files
committed
Fix/improve typings of internal _eventContext properties, add some docs for event context functions
1 parent 96e92f7 commit 614282d

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/Model/Model.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ export class Model<T = DefualtType> {
5353

5454
_at: string;
5555
_context: Context;
56-
_eventContext: number | null;
56+
/**
57+
* Optional "event context" tag applied to event listeners registered using this model, allowing
58+
* `removeContextListeners` to later remove all listeners with this model's "event context".
59+
*
60+
* Every Derby component instance uses its component id ("_1", "_b", etc.) as the event context
61+
* for its component model, so that the component can deregister all its listeners when the
62+
* component is destroyed.
63+
*
64+
* Note that this has nothing to do with `model._context`.
65+
*/
66+
_eventContext: string | null;
5767
_events: [];
5868
_maxListeners: number;
5969
_pass: any;

src/Model/events.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ declare module './Model' {
6868

6969
interface Model<T> {
7070
addListener(event: string, listener: any, arg2?: any, arg3?: any): any;
71+
/**
72+
* Returns a child model based off the current model, with the specified "event context" tag
73+
* applied to all event listeners registered using that child model.
74+
*
75+
* `removeContextListeners()` can later be used to remove all listeners that were added with
76+
* that model's event context.
77+
*
78+
* Note that this has nothing to do with the `context(contextId)` method.
79+
*
80+
* @param id event context tag
81+
*/
7182
eventContext(id: string): ChildModel<T>;
7283

7384
/**
@@ -153,6 +164,11 @@ declare module './Model' {
153164
pass(object: object, invert?: boolean): Model<T>;
154165

155166
removeAllListeners(type: string, subpath: Path): void;
167+
/**
168+
* Remove all listeners that were registered with this model's event context.
169+
*
170+
* @see {@link eventContext}
171+
*/
156172
removeContextListeners(): void;
157173

158174
removeListener(eventType: keyof ModelOnEventMap | keyof ModelOnImmediateEventMap | 'all', listener: Function): void;
@@ -171,8 +187,16 @@ declare module './Model' {
171187
_emitError(err: Error, context?: any): void;
172188
_emitMutation(segments: Segments, event: any): void;
173189
_emittingMutation: boolean;
174-
_eventContextListeners: Record<string, any>;
190+
/**
191+
* Map of "event context" name to an array of listeners with that event context
192+
*
193+
* @see {@link _eventContext}
194+
*/
195+
_eventContextListeners: Record<string, MutationListener[]>;
175196
_mutationEventQueue: null;
197+
/**
198+
* Map of event type to mutation listener tree for that type
199+
*/
176200
_mutationListeners: Record<string, EventListenerTree>;
177201
_removeAllListeners(type: string, segments: Segments): void;
178202
_removeMutationListener(listener: MutationListener): void;

0 commit comments

Comments
 (0)