Skip to content
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
23 changes: 14 additions & 9 deletions packages/database/src/core/SyncTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import { ChildrenNode } from './snap/ChildrenNode';
import { ImmutableTree } from './util/ImmutableTree';
import { ListenComplete } from './operation/ListenComplete';
import { Merge } from './operation/Merge';
import { Operation, OperationSource } from './operation/Operation';
import {
newOperationSourceServer,
newOperationSourceServerTaggedQuery,
newOperationSourceUser,
Operation
} from './operation/Operation';
import { Overwrite } from './operation/Overwrite';
import { Path } from './util/Path';
import { SyncPoint } from './SyncPoint';
Expand Down Expand Up @@ -118,7 +123,7 @@ export class SyncTree {
return [];
} else {
return this.applyOperationToSyncPoints_(
new Overwrite(OperationSource.user(), path, newData)
new Overwrite(newOperationSourceUser(), path, newData)
);
}
}
Expand All @@ -139,7 +144,7 @@ export class SyncTree {
const changeTree = ImmutableTree.fromObject(changedChildren);

return this.applyOperationToSyncPoints_(
new Merge(OperationSource.user(), path, changeTree)
new Merge(newOperationSourceUser(), path, changeTree)
);
}

Expand Down Expand Up @@ -177,7 +182,7 @@ export class SyncTree {
*/
applyServerOverwrite(path: Path, newData: Node): Event[] {
return this.applyOperationToSyncPoints_(
new Overwrite(OperationSource.server(), path, newData)
new Overwrite(newOperationSourceServer(), path, newData)
);
}

Expand All @@ -193,7 +198,7 @@ export class SyncTree {
const changeTree = ImmutableTree.fromObject(changedChildren);

return this.applyOperationToSyncPoints_(
new Merge(OperationSource.server(), path, changeTree)
new Merge(newOperationSourceServer(), path, changeTree)
);
}

Expand All @@ -204,7 +209,7 @@ export class SyncTree {
*/
applyListenComplete(path: Path): Event[] {
return this.applyOperationToSyncPoints_(
new ListenComplete(OperationSource.server(), path)
new ListenComplete(newOperationSourceServer(), path)
);
}

Expand All @@ -221,7 +226,7 @@ export class SyncTree {
queryId = r.queryId;
const relativePath = Path.relativePath(queryPath, path);
const op = new Overwrite(
OperationSource.forServerTaggedQuery(queryId),
newOperationSourceServerTaggedQuery(queryId),
relativePath,
snap
);
Expand Down Expand Up @@ -250,7 +255,7 @@ export class SyncTree {
const relativePath = Path.relativePath(queryPath, path);
const changeTree = ImmutableTree.fromObject(changedChildren);
const op = new Merge(
OperationSource.forServerTaggedQuery(queryId),
newOperationSourceServerTaggedQuery(queryId),
relativePath,
changeTree
);
Expand All @@ -274,7 +279,7 @@ export class SyncTree {
queryId = r.queryId;
const relativePath = Path.relativePath(queryPath, path);
const op = new ListenComplete(
OperationSource.forServerTaggedQuery(queryId),
newOperationSourceServerTaggedQuery(queryId),
relativePath
);
return this.applyTaggedOperation_(queryPath, op);
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/core/operation/AckUserWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

import { assert } from '@firebase/util';
import { Path } from '../util/Path';
import { Operation, OperationSource, OperationType } from './Operation';
import { newOperationSourceUser, Operation, OperationType } from './Operation';
import { ImmutableTree } from '../util/ImmutableTree';

export class AckUserWrite implements Operation {
/** @inheritDoc */
type = OperationType.ACK_USER_WRITE;

/** @inheritDoc */
source = OperationSource.user();
source = newOperationSourceUser();

/**
* @param affectedTree A tree containing true for each affected path. Affected paths can't overlap.
Expand Down
63 changes: 30 additions & 33 deletions packages/database/src/core/operation/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* limitations under the License.
*/

import { assert } from '@firebase/util';
import { Path } from '../util/Path';

/**
Expand All @@ -42,40 +41,38 @@ export interface Operation {
operationForChild(childName: string): Operation | null;
}

export class OperationSource {
constructor(
public fromUser: boolean,
public fromServer: boolean,
public queryId: string | null,
public tagged: boolean
) {
assert(!tagged || fromServer, 'Tagged queries must be from server.');
}
export interface OperationSource {
fromUser: boolean;
fromServer: boolean;
queryId: string | null;
tagged: boolean;
}

static user() {
return new OperationSource(
/*fromUser=*/ true,
false,
null,
/*tagged=*/ false
);
}
export function newOperationSourceUser(): OperationSource {
return {
fromUser: true,
fromServer: false,
queryId: null,
tagged: false
};
}

static server() {
return new OperationSource(
false,
/*fromServer=*/ true,
null,
/*tagged=*/ false
);
}
export function newOperationSourceServer(): OperationSource {
return {
fromUser: false,
fromServer: true,
queryId: null,
tagged: false
};
}

static forServerTaggedQuery = function (queryId: string): OperationSource {
return new OperationSource(
false,
/*fromServer=*/ true,
queryId,
/*tagged=*/ true
);
export function newOperationSourceServerTaggedQuery(
queryId: string
): OperationSource {
return {
fromUser: false,
fromServer: true,
queryId,
tagged: true
};
}