Skip to content

Tree-shake EventGenerator #4452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 127 additions & 111 deletions packages/database/src/core/view/EventGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,129 +30,145 @@ import { Event } from './Event';
*
*/
export class EventGenerator {
private index_: Index;
index_: Index;

constructor(private query_: Query) {
/**
*/
constructor(public query_: Query) {
this.index_ = this.query_.getQueryParams().getIndex();
}
}

/**
* Given a set of raw changes (no moved events and prevName not specified yet), and a set of
* EventRegistrations that should be notified of these changes, generate the actual events to be raised.
*
* Notes:
* - child_moved events will be synthesized at this time for any child_changed events that affect
* our index.
* - prevName will be calculated based on the index ordering.
*/
generateEventsForChanges(
changes: Change[],
eventCache: Node,
eventRegistrations: EventRegistration[]
): Event[] {
const events: Event[] = [];
const moves: Change[] = [];
/**
* Given a set of raw changes (no moved events and prevName not specified yet), and a set of
* EventRegistrations that should be notified of these changes, generate the actual events to be raised.
*
* Notes:
* - child_moved events will be synthesized at this time for any child_changed events that affect
* our index.
* - prevName will be calculated based on the index ordering.
*/
export function eventGeneratorGenerateEventsForChanges(
eventGenerator: EventGenerator,
changes: Change[],
eventCache: Node,
eventRegistrations: EventRegistration[]
): Event[] {
const events: Event[] = [];
const moves: Change[] = [];

changes.forEach(change => {
if (
change.type === ChangeType.CHILD_CHANGED &&
this.index_.indexedValueChanged(
change.oldSnap as Node,
change.snapshotNode
)
) {
moves.push(changeChildMoved(change.childName, change.snapshotNode));
}
});
changes.forEach(change => {
if (
change.type === ChangeType.CHILD_CHANGED &&
eventGenerator.index_.indexedValueChanged(
change.oldSnap as Node,
change.snapshotNode
)
) {
moves.push(changeChildMoved(change.childName, change.snapshotNode));
}
});

this.generateEventsForType_(
events,
ChangeType.CHILD_REMOVED,
changes,
eventRegistrations,
eventCache
);
this.generateEventsForType_(
events,
ChangeType.CHILD_ADDED,
changes,
eventRegistrations,
eventCache
);
this.generateEventsForType_(
events,
ChangeType.CHILD_MOVED,
moves,
eventRegistrations,
eventCache
);
this.generateEventsForType_(
events,
ChangeType.CHILD_CHANGED,
changes,
eventRegistrations,
eventCache
);
this.generateEventsForType_(
events,
ChangeType.VALUE,
changes,
eventRegistrations,
eventCache
);
eventGeneratorGenerateEventsForType(
eventGenerator,
events,
ChangeType.CHILD_REMOVED,
changes,
eventRegistrations,
eventCache
);
eventGeneratorGenerateEventsForType(
eventGenerator,
events,
ChangeType.CHILD_ADDED,
changes,
eventRegistrations,
eventCache
);
eventGeneratorGenerateEventsForType(
eventGenerator,
events,
ChangeType.CHILD_MOVED,
moves,
eventRegistrations,
eventCache
);
eventGeneratorGenerateEventsForType(
eventGenerator,
events,
ChangeType.CHILD_CHANGED,
changes,
eventRegistrations,
eventCache
);
eventGeneratorGenerateEventsForType(
eventGenerator,
events,
ChangeType.VALUE,
changes,
eventRegistrations,
eventCache
);

return events;
}
return events;
}

/**
* Given changes of a single change type, generate the corresponding events.
*/
private generateEventsForType_(
events: Event[],
eventType: string,
changes: Change[],
registrations: EventRegistration[],
eventCache: Node
) {
const filteredChanges = changes.filter(change => change.type === eventType);
/**
* Given changes of a single change type, generate the corresponding events.
*/
function eventGeneratorGenerateEventsForType(
eventGenerator: EventGenerator,
events: Event[],
eventType: string,
changes: Change[],
registrations: EventRegistration[],
eventCache: Node
) {
const filteredChanges = changes.filter(change => change.type === eventType);

filteredChanges.sort(this.compareChanges_.bind(this));
filteredChanges.forEach(change => {
const materializedChange = this.materializeSingleChange_(
change,
eventCache
);
registrations.forEach(registration => {
if (registration.respondsTo(change.type)) {
events.push(
registration.createEvent(materializedChange, this.query_)
);
}
});
filteredChanges.sort((a, b) =>
eventGeneratorCompareChanges(eventGenerator, a, b)
);
filteredChanges.forEach(change => {
const materializedChange = eventGeneratorMaterializeSingleChange(
eventGenerator,
change,
eventCache
);
registrations.forEach(registration => {
if (registration.respondsTo(change.type)) {
events.push(
registration.createEvent(materializedChange, eventGenerator.query_)
);
}
});
}
});
}

private materializeSingleChange_(change: Change, eventCache: Node): Change {
if (change.type === 'value' || change.type === 'child_removed') {
return change;
} else {
change.prevName = eventCache.getPredecessorChildName(
change.childName,
change.snapshotNode,
this.index_
);
return change;
}
function eventGeneratorMaterializeSingleChange(
eventGenerator: EventGenerator,
change: Change,
eventCache: Node
): Change {
if (change.type === 'value' || change.type === 'child_removed') {
return change;
} else {
change.prevName = eventCache.getPredecessorChildName(
change.childName,
change.snapshotNode,
eventGenerator.index_
);
return change;
}
}

private compareChanges_(a: Change, b: Change) {
if (a.childName == null || b.childName == null) {
throw assertionError('Should only compare child_ events.');
}
const aWrapped = new NamedNode(a.childName, a.snapshotNode);
const bWrapped = new NamedNode(b.childName, b.snapshotNode);
return this.index_.compare(aWrapped, bWrapped);
function eventGeneratorCompareChanges(
eventGenerator: EventGenerator,
a: Change,
b: Change
) {
if (a.childName == null || b.childName == null) {
throw assertionError('Should only compare child_ events.');
}
const aWrapped = new NamedNode(a.childName, a.snapshotNode);
const bWrapped = new NamedNode(b.childName, b.snapshotNode);
return eventGenerator.index_.compare(aWrapped, bWrapped);
}
8 changes: 6 additions & 2 deletions packages/database/src/core/view/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { ViewProcessor } from './ViewProcessor';
import { ChildrenNode } from '../snap/ChildrenNode';
import { CacheNode } from './CacheNode';
import { ViewCache } from './ViewCache';
import { EventGenerator } from './EventGenerator';
import {
EventGenerator,
eventGeneratorGenerateEventsForChanges
} from './EventGenerator';
import { assert } from '@firebase/util';
import { Operation, OperationType } from '../operation/Operation';
import { Change, changeChildAdded, changeValue } from './Change';
Expand Down Expand Up @@ -231,7 +234,8 @@ export class View {
const registrations = eventRegistration
? [eventRegistration]
: this.eventRegistrations_;
return this.eventGenerator_.generateEventsForChanges(
return eventGeneratorGenerateEventsForChanges(
this.eventGenerator_,
changes,
eventCache,
registrations
Expand Down