Skip to content

Commit 9bac402

Browse files
Tree-shake EventGenerator (#4452)
1 parent 9c7f99e commit 9bac402

File tree

2 files changed

+133
-113
lines changed

2 files changed

+133
-113
lines changed

packages/database/src/core/view/EventGenerator.ts

Lines changed: 127 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -30,129 +30,145 @@ import { Event } from './Event';
3030
*
3131
*/
3232
export class EventGenerator {
33-
private index_: Index;
33+
index_: Index;
3434

35-
constructor(private query_: Query) {
36-
/**
37-
*/
35+
constructor(public query_: Query) {
3836
this.index_ = this.query_.getQueryParams().getIndex();
3937
}
38+
}
4039

41-
/**
42-
* Given a set of raw changes (no moved events and prevName not specified yet), and a set of
43-
* EventRegistrations that should be notified of these changes, generate the actual events to be raised.
44-
*
45-
* Notes:
46-
* - child_moved events will be synthesized at this time for any child_changed events that affect
47-
* our index.
48-
* - prevName will be calculated based on the index ordering.
49-
*/
50-
generateEventsForChanges(
51-
changes: Change[],
52-
eventCache: Node,
53-
eventRegistrations: EventRegistration[]
54-
): Event[] {
55-
const events: Event[] = [];
56-
const moves: Change[] = [];
40+
/**
41+
* Given a set of raw changes (no moved events and prevName not specified yet), and a set of
42+
* EventRegistrations that should be notified of these changes, generate the actual events to be raised.
43+
*
44+
* Notes:
45+
* - child_moved events will be synthesized at this time for any child_changed events that affect
46+
* our index.
47+
* - prevName will be calculated based on the index ordering.
48+
*/
49+
export function eventGeneratorGenerateEventsForChanges(
50+
eventGenerator: EventGenerator,
51+
changes: Change[],
52+
eventCache: Node,
53+
eventRegistrations: EventRegistration[]
54+
): Event[] {
55+
const events: Event[] = [];
56+
const moves: Change[] = [];
5757

58-
changes.forEach(change => {
59-
if (
60-
change.type === ChangeType.CHILD_CHANGED &&
61-
this.index_.indexedValueChanged(
62-
change.oldSnap as Node,
63-
change.snapshotNode
64-
)
65-
) {
66-
moves.push(changeChildMoved(change.childName, change.snapshotNode));
67-
}
68-
});
58+
changes.forEach(change => {
59+
if (
60+
change.type === ChangeType.CHILD_CHANGED &&
61+
eventGenerator.index_.indexedValueChanged(
62+
change.oldSnap as Node,
63+
change.snapshotNode
64+
)
65+
) {
66+
moves.push(changeChildMoved(change.childName, change.snapshotNode));
67+
}
68+
});
6969

70-
this.generateEventsForType_(
71-
events,
72-
ChangeType.CHILD_REMOVED,
73-
changes,
74-
eventRegistrations,
75-
eventCache
76-
);
77-
this.generateEventsForType_(
78-
events,
79-
ChangeType.CHILD_ADDED,
80-
changes,
81-
eventRegistrations,
82-
eventCache
83-
);
84-
this.generateEventsForType_(
85-
events,
86-
ChangeType.CHILD_MOVED,
87-
moves,
88-
eventRegistrations,
89-
eventCache
90-
);
91-
this.generateEventsForType_(
92-
events,
93-
ChangeType.CHILD_CHANGED,
94-
changes,
95-
eventRegistrations,
96-
eventCache
97-
);
98-
this.generateEventsForType_(
99-
events,
100-
ChangeType.VALUE,
101-
changes,
102-
eventRegistrations,
103-
eventCache
104-
);
70+
eventGeneratorGenerateEventsForType(
71+
eventGenerator,
72+
events,
73+
ChangeType.CHILD_REMOVED,
74+
changes,
75+
eventRegistrations,
76+
eventCache
77+
);
78+
eventGeneratorGenerateEventsForType(
79+
eventGenerator,
80+
events,
81+
ChangeType.CHILD_ADDED,
82+
changes,
83+
eventRegistrations,
84+
eventCache
85+
);
86+
eventGeneratorGenerateEventsForType(
87+
eventGenerator,
88+
events,
89+
ChangeType.CHILD_MOVED,
90+
moves,
91+
eventRegistrations,
92+
eventCache
93+
);
94+
eventGeneratorGenerateEventsForType(
95+
eventGenerator,
96+
events,
97+
ChangeType.CHILD_CHANGED,
98+
changes,
99+
eventRegistrations,
100+
eventCache
101+
);
102+
eventGeneratorGenerateEventsForType(
103+
eventGenerator,
104+
events,
105+
ChangeType.VALUE,
106+
changes,
107+
eventRegistrations,
108+
eventCache
109+
);
105110

106-
return events;
107-
}
111+
return events;
112+
}
108113

109-
/**
110-
* Given changes of a single change type, generate the corresponding events.
111-
*/
112-
private generateEventsForType_(
113-
events: Event[],
114-
eventType: string,
115-
changes: Change[],
116-
registrations: EventRegistration[],
117-
eventCache: Node
118-
) {
119-
const filteredChanges = changes.filter(change => change.type === eventType);
114+
/**
115+
* Given changes of a single change type, generate the corresponding events.
116+
*/
117+
function eventGeneratorGenerateEventsForType(
118+
eventGenerator: EventGenerator,
119+
events: Event[],
120+
eventType: string,
121+
changes: Change[],
122+
registrations: EventRegistration[],
123+
eventCache: Node
124+
) {
125+
const filteredChanges = changes.filter(change => change.type === eventType);
120126

121-
filteredChanges.sort(this.compareChanges_.bind(this));
122-
filteredChanges.forEach(change => {
123-
const materializedChange = this.materializeSingleChange_(
124-
change,
125-
eventCache
126-
);
127-
registrations.forEach(registration => {
128-
if (registration.respondsTo(change.type)) {
129-
events.push(
130-
registration.createEvent(materializedChange, this.query_)
131-
);
132-
}
133-
});
127+
filteredChanges.sort((a, b) =>
128+
eventGeneratorCompareChanges(eventGenerator, a, b)
129+
);
130+
filteredChanges.forEach(change => {
131+
const materializedChange = eventGeneratorMaterializeSingleChange(
132+
eventGenerator,
133+
change,
134+
eventCache
135+
);
136+
registrations.forEach(registration => {
137+
if (registration.respondsTo(change.type)) {
138+
events.push(
139+
registration.createEvent(materializedChange, eventGenerator.query_)
140+
);
141+
}
134142
});
135-
}
143+
});
144+
}
136145

137-
private materializeSingleChange_(change: Change, eventCache: Node): Change {
138-
if (change.type === 'value' || change.type === 'child_removed') {
139-
return change;
140-
} else {
141-
change.prevName = eventCache.getPredecessorChildName(
142-
change.childName,
143-
change.snapshotNode,
144-
this.index_
145-
);
146-
return change;
147-
}
146+
function eventGeneratorMaterializeSingleChange(
147+
eventGenerator: EventGenerator,
148+
change: Change,
149+
eventCache: Node
150+
): Change {
151+
if (change.type === 'value' || change.type === 'child_removed') {
152+
return change;
153+
} else {
154+
change.prevName = eventCache.getPredecessorChildName(
155+
change.childName,
156+
change.snapshotNode,
157+
eventGenerator.index_
158+
);
159+
return change;
148160
}
161+
}
149162

150-
private compareChanges_(a: Change, b: Change) {
151-
if (a.childName == null || b.childName == null) {
152-
throw assertionError('Should only compare child_ events.');
153-
}
154-
const aWrapped = new NamedNode(a.childName, a.snapshotNode);
155-
const bWrapped = new NamedNode(b.childName, b.snapshotNode);
156-
return this.index_.compare(aWrapped, bWrapped);
163+
function eventGeneratorCompareChanges(
164+
eventGenerator: EventGenerator,
165+
a: Change,
166+
b: Change
167+
) {
168+
if (a.childName == null || b.childName == null) {
169+
throw assertionError('Should only compare child_ events.');
157170
}
171+
const aWrapped = new NamedNode(a.childName, a.snapshotNode);
172+
const bWrapped = new NamedNode(b.childName, b.snapshotNode);
173+
return eventGenerator.index_.compare(aWrapped, bWrapped);
158174
}

packages/database/src/core/view/View.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import { ViewProcessor } from './ViewProcessor';
2020
import { ChildrenNode } from '../snap/ChildrenNode';
2121
import { CacheNode } from './CacheNode';
2222
import { ViewCache } from './ViewCache';
23-
import { EventGenerator } from './EventGenerator';
23+
import {
24+
EventGenerator,
25+
eventGeneratorGenerateEventsForChanges
26+
} from './EventGenerator';
2427
import { assert } from '@firebase/util';
2528
import { Operation, OperationType } from '../operation/Operation';
2629
import { Change, changeChildAdded, changeValue } from './Change';
@@ -235,7 +238,8 @@ export class View {
235238
const registrations = eventRegistration
236239
? [eventRegistration]
237240
: this.eventRegistrations_;
238-
return this.eventGenerator_.generateEventsForChanges(
241+
return eventGeneratorGenerateEventsForChanges(
242+
this.eventGenerator_,
239243
changes,
240244
eventCache,
241245
registrations

0 commit comments

Comments
 (0)