Skip to content

Commit 46ac935

Browse files
committed
Update build files and examples
1 parent ee4ef6e commit 46ac935

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ const dgraphClient = new dgraph.DgraphClient(stub1, stub2);
259259
// Use dgraphClient
260260
// ...
261261

262-
// Cleanup resources.
262+
// Cleanup resources by closing all client stubs.
263263
stub1.close();
264264
stub2.close();
265265
```

examples/simple/index-pre-v7.6.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
const dgraph = require("dgraph-js");
22
const grpc = require("grpc");
33

4-
function newClient() {
5-
const clientStub = new dgraph.DgraphClientStub("localhost:9080", grpc.credentials.createInsecure());
4+
// Create a client stub.
5+
function newClientStub() {
6+
return new dgraph.DgraphClientStub("localhost:9080", grpc.credentials.createInsecure());
7+
}
8+
9+
// Create a client.
10+
function newClient(clientStub) {
611
return new dgraph.DgraphClient(clientStub);
712
}
813

@@ -125,13 +130,17 @@ function queryData(dgraphClient) {
125130
}
126131

127132
function main() {
128-
const dgraphClient = newClient();
133+
const dgraphClientStub = newClientStub();
134+
const dgraphClient = newClient(dgraphClientStub);
129135
return dropAll(dgraphClient).then(() => {
130136
return setSchema(dgraphClient);
131137
}).then(() => {
132138
return createData(dgraphClient);
133139
}).then(() => {
134140
return queryData(dgraphClient);
141+
}).then(() => {
142+
// Close the client stub.
143+
dgraphClientStub.close();
135144
});
136145
}
137146

examples/simple/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const dgraph = require("dgraph-js");
22
const grpc = require("grpc");
33

4+
// Create a client stub.
5+
function newClientStub() {
6+
return new dgraph.DgraphClientStub("localhost:9080", grpc.credentials.createInsecure());
7+
}
8+
49
// Create a client.
5-
function newClient() {
6-
const clientStub = new dgraph.DgraphClientStub("localhost:9080", grpc.credentials.createInsecure());
10+
function newClient(clientStub) {
711
return new dgraph.DgraphClient(clientStub);
812
}
913

@@ -114,11 +118,15 @@ async function queryData(dgraphClient) {
114118
}
115119

116120
async function main() {
117-
const dgraphClient = newClient();
121+
const dgraphClientStub = newClientStub();
122+
const dgraphClient = newClient(dgraphClientStub);
118123
await dropAll(dgraphClient);
119124
await setSchema(dgraphClient);
120125
await createData(dgraphClient);
121126
await queryData(dgraphClient);
127+
128+
// Close the client stub.
129+
dgraphClientStub.close();
122130
}
123131

124132
main().then(() => {

lib/clientStub.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ export declare class DgraphClientStub {
99
mutate(mu: messages.Mutation): Promise<messages.Assigned>;
1010
commitOrAbort(ctx: messages.TxnContext): Promise<messages.TxnContext>;
1111
checkVersion(check: messages.Check): Promise<messages.Version>;
12+
waitForReady(deadline: grpc.Deadline): Promise<void>;
13+
close(): void;
14+
grpcClient(): grpc.Client;
1215
}

lib/clientStub.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var DgraphClientStub = (function () {
1818
mutate: util_1.promisify(this.stub.mutate, this.stub),
1919
commitOrAbort: util_1.promisify(this.stub.commitOrAbort, this.stub),
2020
checkVersion: util_1.promisify(this.stub.checkVersion, this.stub),
21+
waitForReady: util_1.promisify(this.stub.waitForReady, this.stub),
2122
};
2223
}
2324
DgraphClientStub.prototype.alter = function (op) {
@@ -35,6 +36,15 @@ var DgraphClientStub = (function () {
3536
DgraphClientStub.prototype.checkVersion = function (check) {
3637
return this.promisified.checkVersion(check);
3738
};
39+
DgraphClientStub.prototype.waitForReady = function (deadline) {
40+
return this.promisified.waitForReady(deadline);
41+
};
42+
DgraphClientStub.prototype.close = function () {
43+
return this.stub.close();
44+
};
45+
DgraphClientStub.prototype.grpcClient = function () {
46+
return this.stub;
47+
};
3848
return DgraphClientStub;
3949
}());
4050
exports.DgraphClientStub = DgraphClientStub;

0 commit comments

Comments
 (0)