Skip to content

Commit

Permalink
refactor, do not create sessions after getDatabasesStream call
Browse files Browse the repository at this point in the history
  • Loading branch information
AVaksman committed Sep 10, 2020
1 parent 689a279 commit 79d0b6f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 55 deletions.
30 changes: 14 additions & 16 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ class Database extends common.GrpcServiceObject {
[CLOUD_RESOURCE_HEADER]: this.formattedName_,
};
this.request = instance.request;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.requestStream = instance.requestStream as any;
this.requestStream = instance.requestStream;
this.pool_.on('error', this.emit.bind(this, 'error'));
this.pool_.open();
this.queryOptions_ = Object.assign(
Expand Down Expand Up @@ -1484,27 +1483,26 @@ class Database extends common.GrpcServiceObject {
delete gaxOpts.pageToken;
}

// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const transform = function (
this: Transform,
chunk: databaseAdmin.spanner.v1.ISession,
enc: string,
callback: Function
) {
const session = self.session(chunk.name!);
session.metadata = chunk;
callback(null, session);
};

return new pumpify.obj([
this.requestStream({
client: 'SpannerClient',
method: 'listSessionsStream',
reqOpts,
gaxOpts,
headers: this.resourceHeader_,
}),
new Transform({
objectMode: true,
transform: (
chunk: databaseAdmin.spanner.v1.ISession,
enc: string,
cb: Function
) => {
const session = this.session(chunk.name!);
session.metadata = chunk;
cb(null, session);
},
}),
new Transform({objectMode: true, transform}),
]);
}

Expand Down
27 changes: 13 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,27 +621,26 @@ class Spanner extends GrpcService {
delete gaxOpts.pageToken;
}

// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const transform = function (
this: Transform,
chunk: instanceAdmin.spanner.admin.instance.v1.IInstance,
enc: string,
callback: Function
) {
const instance = self.instance(chunk.name!);
instance.metadata = chunk;
callback(null, instance);
};

return new pumpify.obj([
this.requestStream({
client: 'InstanceAdminClient',
method: 'listInstancesStream',
reqOpts,
gaxOpts,
headers: this.resourceHeader_,
}),
new Transform({
objectMode: true,
transform: (
chunk: instanceAdmin.spanner.admin.instance.v1.IInstance,
enc: string,
cb: Function
) => {
const instance = this.instance(chunk.name!);
instance.metadata = chunk;
cb(null, instance);
},
}),
new Transform({objectMode: true, transform}),
]);
}

Expand Down
41 changes: 16 additions & 25 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,6 @@ class Instance extends common.GrpcServiceObject {
delete gaxOpts.pageSize;
delete gaxOpts.pageToken;
}
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const transform = function (
this: Transform,
chunk: instanceAdmin.spanner.admin.database.v1.IBackup,
enc: string,
callback: Function
) {
callback(null, self.backup(chunk.name!));
};

return new pumpify.obj([
this.requestStream({
Expand All @@ -454,7 +444,14 @@ class Instance extends common.GrpcServiceObject {
gaxOpts,
headers: this.resourceHeader_,
}),
new Transform({objectMode: true, transform}),
new Transform({
objectMode: true,
transform: (chunk: IBackup, enc: string, cb: Function) => {
const backup = this.backup(chunk.name!);
backup.metadata = chunk;
cb(null, backup);
},
}),
]);
}

Expand Down Expand Up @@ -1342,19 +1339,6 @@ class Instance extends common.GrpcServiceObject {
delete gaxOpts.pageToken;
}

// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const transform = function (
this: Transform,
chunk: databaseAdmin.spanner.admin.database.v1.IDatabase,
enc: string,
callback: Function
) {
const database = self.database(chunk.name!);
database.metadata = chunk;
callback(null, database);
};

return new pumpify.obj([
this.requestStream({
client: 'DatabaseAdminClient',
Expand All @@ -1363,7 +1347,14 @@ class Instance extends common.GrpcServiceObject {
gaxOpts,
headers: this.resourceHeader_,
}),
new Transform({objectMode: true, transform}),
new Transform({
objectMode: true,
transform: (chunk: IDatabase, enc: string, cb: Function) => {
const database = this.database(chunk.name!, {min: 0});
database.metadata = chunk;
cb(null, database);
},
}),
]);
}

Expand Down

0 comments on commit 79d0b6f

Please sign in to comment.