UNPKG

3.61 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 * @format
9 * @emails oncall+relay
10 */
11'use strict';
12
13var _asyncToGenerator = require("@babel/runtime/helpers/asyncToGenerator");
14
15var MAX_ATTEMPT_LIMIT = 5;
16
17function delay(delayMs) {
18 return new Promise(function (resolve) {
19 return setTimeout(resolve, delayMs);
20 });
21}
22
23var GraphQLWatchmanClient =
24/*#__PURE__*/
25function () {
26 GraphQLWatchmanClient.isAvailable = function isAvailable() {
27 return new Promise(function (resolve) {
28 var client = new GraphQLWatchmanClient(MAX_ATTEMPT_LIMIT);
29 client.on('error', function () {
30 resolve(false);
31 client.end();
32 });
33 client.hasCapability('relative_root').then(function (hasRelativeRoot) {
34 resolve(hasRelativeRoot);
35 client.end();
36 }, function () {
37 resolve(false);
38 client.end();
39 });
40 });
41 };
42
43 function GraphQLWatchmanClient() {
44 var attemptLimit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
45 this._client = new (require("fb-watchman").Client)();
46 this._attemptLimit = Math.max(Math.min(MAX_ATTEMPT_LIMIT, attemptLimit), 0);
47 }
48
49 var _proto = GraphQLWatchmanClient.prototype;
50
51 _proto._command = function _command() {
52 var _this = this;
53
54 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
55 args[_key] = arguments[_key];
56 }
57
58 return new Promise(function (resolve, reject) {
59 _this._client.command(args, function (error, response) {
60 if (error) {
61 reject(error);
62 } else {
63 resolve(response);
64 }
65 });
66 });
67 };
68
69 _proto.command =
70 /*#__PURE__*/
71 function () {
72 var _command = _asyncToGenerator(function* () {
73 var attempt = 0;
74
75 for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
76 args[_key2] = arguments[_key2];
77 }
78
79 while (true) {
80 try {
81 attempt++;
82 return yield this._command.apply(this, args);
83 } catch (error) {
84 if (attempt > this._attemptLimit) {
85 throw error;
86 }
87
88 yield delay(Math.pow(2, attempt) * 500);
89
90 this._client.end();
91
92 this._client = new (require("fb-watchman").Client)();
93 }
94 }
95 });
96
97 return function command() {
98 return _command.apply(this, arguments);
99 };
100 }();
101
102 _proto.hasCapability =
103 /*#__PURE__*/
104 function () {
105 var _hasCapability = _asyncToGenerator(function* (capability) {
106 var resp = yield this.command('list-capabilities');
107 return resp.capabilities.includes(capability);
108 });
109
110 return function hasCapability(_x) {
111 return _hasCapability.apply(this, arguments);
112 };
113 }();
114
115 _proto.watchProject =
116 /*#__PURE__*/
117 function () {
118 var _watchProject = _asyncToGenerator(function* (baseDir) {
119 var resp = yield this.command('watch-project', baseDir);
120
121 if ('warning' in resp) {
122 console.error('Warning:', resp.warning);
123 }
124
125 return {
126 root: resp.watch,
127 relativePath: resp.relative_path
128 };
129 });
130
131 return function watchProject(_x2) {
132 return _watchProject.apply(this, arguments);
133 };
134 }();
135
136 _proto.on = function on(event, callback) {
137 this._client.on(event, callback);
138 };
139
140 _proto.end = function end() {
141 this._client.end();
142 };
143
144 return GraphQLWatchmanClient;
145}();
146
147module.exports = GraphQLWatchmanClient;
\No newline at end of file