Skip to content

Don't serialize/deserialize stream token #3255

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 1 commit into from
Jun 24, 2020
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
2 changes: 1 addition & 1 deletion packages/firestore/src/protos/firestore_proto_api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export declare namespace firestoreV1ApiClientInterfaces {
}
interface WriteResponse {
streamId?: string;
streamToken?: string;
streamToken?: string | Uint8Array;
writeResults?: WriteResult[];
commitTime?: Timestamp;
}
Expand Down
18 changes: 6 additions & 12 deletions packages/firestore/src/remote/persistent_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,17 @@ import { isNullOrUndefined } from '../util/types';
import { ExponentialBackoff } from './backoff';
import { Connection, Stream } from './connection';
import {
fromBytes,
fromVersion,
fromWatchChange,
fromWriteResults,
getEncodedDatabaseId,
JsonProtoSerializer,
toBytes,
toListenRequestLabels,
toMutation,
toTarget,
versionFromListenResponse
} from './serializer';
import { WatchChange } from './watch_change';
import { ByteString } from '../util/byte_string';

const LOG_TAG = 'PersistentStream';

Expand Down Expand Up @@ -679,7 +676,7 @@ export class PersistentWriteStream extends PersistentStream<
* PersistentWriteStream manages propagating this value from responses to the
* next request.
*/
private lastStreamToken: ByteString = ByteString.EMPTY_BYTE_STRING;
private lastStreamToken: string | Uint8Array | undefined;

/**
* Tracks whether or not a handshake has been successfully exchanged and
Expand All @@ -692,7 +689,7 @@ export class PersistentWriteStream extends PersistentStream<
// Override of PersistentStream.start
start(): void {
this.handshakeComplete_ = false;
this.lastStreamToken = ByteString.EMPTY_BYTE_STRING;
this.lastStreamToken = undefined;
super.start();
}

Expand All @@ -717,10 +714,7 @@ export class PersistentWriteStream extends PersistentStream<
!!responseProto.streamToken,
'Got a write response without a stream token'
);
this.lastStreamToken = fromBytes(
this.serializer,
responseProto.streamToken
);
this.lastStreamToken = responseProto.streamToken;

if (!this.handshakeComplete_) {
// The first response is always the handshake response
Expand Down Expand Up @@ -754,7 +748,7 @@ export class PersistentWriteStream extends PersistentStream<
debugAssert(this.isOpen(), 'Writing handshake requires an opened stream');
debugAssert(!this.handshakeComplete_, 'Handshake already completed');
debugAssert(
this.lastStreamToken.isEqual(ByteString.EMPTY_BYTE_STRING),
!this.lastStreamToken,
'Stream token should be empty during handshake'
);
// TODO(dimond): Support stream resumption. We intentionally do not set the
Expand All @@ -772,12 +766,12 @@ export class PersistentWriteStream extends PersistentStream<
'Handshake must be complete before writing mutations'
);
debugAssert(
this.lastStreamToken.approximateByteSize() > 0,
!!this.lastStreamToken,
'Trying to write mutation without a token'
);

const request: WriteRequest = {
streamToken: toBytes(this.serializer, this.lastStreamToken),
streamToken: this.lastStreamToken,
writes: mutations.map(mutation => toMutation(this.serializer, mutation))
};

Expand Down