@miso.ai/client-sdk-core 1.7.3-beta.3 → 1.7.3-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,8 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _commons = require("@miso.ai/commons");
8
8
  var _base = _interopRequireDefault(require("./base"));
9
- const _excluded = ["pollingInternal"];
9
+ const _excluded = ["pollingInterval", "signal"],
10
+ _excluded2 = ["signal"];
10
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
12
13
  class Ask extends _base.default {
@@ -16,7 +17,7 @@ class Ask extends _base.default {
16
17
  async questions(payload) {
17
18
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
18
19
  const response = await this._run('questions', payload, options);
19
- return new Answer(this, response);
20
+ return new Answer(this, response, options);
20
21
  }
21
22
  async _questions(payload, options) {
22
23
  return this._run('questions', payload, options);
@@ -26,13 +27,18 @@ class Ask extends _base.default {
26
27
  method: 'GET'
27
28
  }));
28
29
  }
30
+
31
+ // TODO: move impl away
29
32
  _iterable(questionId) {
30
33
  let _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
31
34
  let {
32
- pollingInternal = 500
35
+ pollingInterval = 1000,
36
+ signal
33
37
  } = _ref,
34
38
  options = _objectWithoutPropertiesLoose(_ref, _excluded);
35
- // TODO: abort signal
39
+ if (signal && signal.aborted) {
40
+ return [];
41
+ }
36
42
  let apiErrorCount = 0;
37
43
  const buffer = new _commons.ValueBuffer();
38
44
  const intervalId = setInterval(async () => {
@@ -40,13 +46,12 @@ class Ask extends _base.default {
40
46
  try {
41
47
  response = await this._get(questionId, options);
42
48
  } catch (error) {
49
+ console.error(error); // TODO
43
50
  apiErrorCount++;
44
51
  if (apiErrorCount > 10) {
45
52
  clearInterval(intervalId);
46
- // TODO: cascade to buffer and user
53
+ buffer.error(error);
47
54
  }
48
-
49
- console.error(error); // TODO
50
55
  return;
51
56
  }
52
57
  const {
@@ -56,20 +61,31 @@ class Ask extends _base.default {
56
61
  if (response.finished) {
57
62
  clearInterval(intervalId);
58
63
  }
59
- }, pollingInternal);
64
+ }, pollingInterval);
65
+ if (signal && signal.addEventListener) {
66
+ signal.addEventListener('abort', () => {
67
+ clearInterval(intervalId);
68
+ // TODO: optimize: abort current request as well
69
+ buffer.abort();
70
+ });
71
+ }
60
72
  return buffer;
61
73
  }
62
74
  }
63
75
  exports.default = Ask;
64
76
  class Answer {
65
- constructor(api, _ref2) {
77
+ constructor(api, _ref3) {
66
78
  let {
67
79
  question_id
68
- } = _ref2;
69
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
80
+ } = _ref3;
81
+ let _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
82
+ let options = _objectWithoutPropertiesLoose(_ref2, _excluded2);
70
83
  this._api = api;
71
84
  this._id = question_id;
72
- this._options = options;
85
+ this._ac = new AbortController();
86
+ this._options = Object.assign({}, options, {
87
+ signal: this._ac.signal
88
+ });
73
89
  }
74
90
  get id() {
75
91
  return this._id;
@@ -78,6 +94,9 @@ class Answer {
78
94
  options = Object.assign({}, this._options, options);
79
95
  return this._api._get(this._id, options);
80
96
  }
97
+ abort() {
98
+ this._ac.abort();
99
+ }
81
100
  [Symbol.asyncIterator]() {
82
101
  return this._api._iterable(this._id, this._options)[Symbol.asyncIterator]();
83
102
  }
@@ -17,11 +17,10 @@ class ApiHelpers {
17
17
  method = 'POST',
18
18
  timeout
19
19
  } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
20
+ // TODO: external abort signal
20
21
  // TODO: organize arguments
21
22
  const body = method !== 'GET' && payload != undefined ? JSON.stringify(payload) : undefined;
22
- const controller = timeout && new AbortController();
23
- const signal = controller && controller.signal;
24
- const timeoutId = controller && setTimeout(() => controller.abort(), timeout);
23
+ const signal = timeout ? AbortSignal.timeout(timeout) : undefined;
25
24
  const res = await window.fetch(url, (0, _commons.trimObj)({
26
25
  method,
27
26
  body,
@@ -29,7 +28,6 @@ class ApiHelpers {
29
28
  mode: 'cors',
30
29
  signal
31
30
  }));
32
- timeoutId && clearTimeout(timeoutId);
33
31
  const resBody = await res.json();
34
32
  if (res.status >= 400 || resBody.errors) {
35
33
  var err = new Error(resBody.message);
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = '1.7.3-beta.3';
7
+ var _default = '1.7.3-beta.4';
8
8
  exports.default = _default;
@@ -1,4 +1,5 @@
1
- const _excluded = ["pollingInternal"];
1
+ const _excluded = ["pollingInterval", "signal"],
2
+ _excluded2 = ["signal"];
2
3
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
3
4
  import { ValueBuffer } from '@miso.ai/commons';
4
5
  import ApiBase from './base';
@@ -9,7 +10,7 @@ export default class Ask extends ApiBase {
9
10
  async questions(payload) {
10
11
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
11
12
  const response = await this._run('questions', payload, options);
12
- return new Answer(this, response);
13
+ return new Answer(this, response, options);
13
14
  }
14
15
  async _questions(payload, options) {
15
16
  return this._run('questions', payload, options);
@@ -19,13 +20,18 @@ export default class Ask extends ApiBase {
19
20
  method: 'GET'
20
21
  }));
21
22
  }
23
+
24
+ // TODO: move impl away
22
25
  _iterable(questionId) {
23
26
  let _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
24
27
  let {
25
- pollingInternal = 500
28
+ pollingInterval = 1000,
29
+ signal
26
30
  } = _ref,
27
31
  options = _objectWithoutPropertiesLoose(_ref, _excluded);
28
- // TODO: abort signal
32
+ if (signal && signal.aborted) {
33
+ return [];
34
+ }
29
35
  let apiErrorCount = 0;
30
36
  const buffer = new ValueBuffer();
31
37
  const intervalId = setInterval(async () => {
@@ -33,13 +39,12 @@ export default class Ask extends ApiBase {
33
39
  try {
34
40
  response = await this._get(questionId, options);
35
41
  } catch (error) {
42
+ console.error(error); // TODO
36
43
  apiErrorCount++;
37
44
  if (apiErrorCount > 10) {
38
45
  clearInterval(intervalId);
39
- // TODO: cascade to buffer and user
46
+ buffer.error(error);
40
47
  }
41
-
42
- console.error(error); // TODO
43
48
  return;
44
49
  }
45
50
  const {
@@ -49,19 +54,30 @@ export default class Ask extends ApiBase {
49
54
  if (response.finished) {
50
55
  clearInterval(intervalId);
51
56
  }
52
- }, pollingInternal);
57
+ }, pollingInterval);
58
+ if (signal && signal.addEventListener) {
59
+ signal.addEventListener('abort', () => {
60
+ clearInterval(intervalId);
61
+ // TODO: optimize: abort current request as well
62
+ buffer.abort();
63
+ });
64
+ }
53
65
  return buffer;
54
66
  }
55
67
  }
56
68
  class Answer {
57
- constructor(api, _ref2) {
69
+ constructor(api, _ref3) {
58
70
  let {
59
71
  question_id
60
- } = _ref2;
61
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
72
+ } = _ref3;
73
+ let _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
74
+ let options = _objectWithoutPropertiesLoose(_ref2, _excluded2);
62
75
  this._api = api;
63
76
  this._id = question_id;
64
- this._options = options;
77
+ this._ac = new AbortController();
78
+ this._options = Object.assign({}, options, {
79
+ signal: this._ac.signal
80
+ });
65
81
  }
66
82
  get id() {
67
83
  return this._id;
@@ -70,6 +86,9 @@ class Answer {
70
86
  options = Object.assign({}, this._options, options);
71
87
  return this._api._get(this._id, options);
72
88
  }
89
+ abort() {
90
+ this._ac.abort();
91
+ }
73
92
  [Symbol.asyncIterator]() {
74
93
  return this._api._iterable(this._id, this._options)[Symbol.asyncIterator]();
75
94
  }
@@ -11,11 +11,10 @@ export default class ApiHelpers {
11
11
  method = 'POST',
12
12
  timeout
13
13
  } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
14
+ // TODO: external abort signal
14
15
  // TODO: organize arguments
15
16
  const body = method !== 'GET' && payload != undefined ? JSON.stringify(payload) : undefined;
16
- const controller = timeout && new AbortController();
17
- const signal = controller && controller.signal;
18
- const timeoutId = controller && setTimeout(() => controller.abort(), timeout);
17
+ const signal = timeout ? AbortSignal.timeout(timeout) : undefined;
19
18
  const res = await window.fetch(url, trimObj({
20
19
  method,
21
20
  body,
@@ -23,7 +22,6 @@ export default class ApiHelpers {
23
22
  mode: 'cors',
24
23
  signal
25
24
  }));
26
- timeoutId && clearTimeout(timeoutId);
27
25
  const resBody = await res.json();
28
26
  if (res.status >= 400 || resBody.errors) {
29
27
  var err = new Error(resBody.message);
@@ -1 +1 @@
1
- export default '1.7.3-beta.3';
1
+ export default '1.7.3-beta.4';
package/package.json CHANGED
@@ -29,7 +29,7 @@
29
29
  "url": "https://github.com/MisoAI/miso-client-js-sdk/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@miso.ai/commons": "1.7.3-beta.3"
32
+ "@miso.ai/commons": "1.7.3-beta.4"
33
33
  },
34
- "version": "1.7.3-beta.3"
34
+ "version": "1.7.3-beta.4"
35
35
  }