@atlaskit/link-picker 1.8.3 → 1.8.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atlaskit/link-picker
2
2
 
3
+ ## 1.8.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [`e94f9f982b4`](https://bitbucket.org/atlassian/atlassian-frontend/commits/e94f9f982b4) - Fixes link picker attempting to dispatch updates after being unmounted.
8
+
3
9
  ## 1.8.3
4
10
 
5
11
  ### Patch Changes
@@ -13,19 +13,13 @@ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/
13
13
 
14
14
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
15
 
16
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
-
18
16
  var _react = require("react");
19
17
 
20
18
  var _linkPicker = require("../../ui/link-picker");
21
19
 
22
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
23
-
24
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
20
+ var _utils = require("./utils");
25
21
 
26
- function reducer(state, payload) {
27
- return _objectSpread(_objectSpread({}, state), payload);
28
- }
22
+ var _reducer = require("./reducer");
29
23
 
30
24
  function usePlugins(state, activeTab, plugins) {
31
25
  var _useState = (0, _react.useState)(0),
@@ -33,19 +27,11 @@ function usePlugins(state, activeTab, plugins) {
33
27
  retries = _useState2[0],
34
28
  setRetries = _useState2[1];
35
29
 
36
- var _useReducer = (0, _react.useReducer)(reducer, {
37
- items: null,
38
- error: null,
39
- isLoading: false
40
- }),
41
- _useReducer2 = (0, _slicedToArray2.default)(_useReducer, 2),
42
- pluginState = _useReducer2[0],
43
- dispatch = _useReducer2[1];
30
+ var _usePluginReducer = (0, _reducer.usePluginReducer)(),
31
+ _usePluginReducer2 = (0, _slicedToArray2.default)(_usePluginReducer, 2),
32
+ pluginState = _usePluginReducer2[0],
33
+ dispatch = _usePluginReducer2[1];
44
34
 
45
- var items = pluginState.items,
46
- isLoading = pluginState.isLoading,
47
- error = pluginState.error;
48
- var queryVersion = (0, _react.useRef)(0);
49
35
  var activePlugin = plugins === null || plugins === void 0 ? void 0 : plugins[activeTab];
50
36
  (0, _react.useEffect)(function () {
51
37
  if (!activePlugin) {
@@ -54,107 +40,75 @@ function usePlugins(state, activeTab, plugins) {
54
40
 
55
41
  if (!state) {
56
42
  dispatch({
57
- items: null,
58
- isLoading: false,
59
- error: null
43
+ type: 'CLEAR'
60
44
  });
61
45
  return;
62
46
  }
63
47
 
64
- var updates = activePlugin.resolve(state);
65
48
  dispatch({
66
- items: null,
67
- isLoading: true,
68
- error: null
49
+ type: 'LOADING'
69
50
  });
70
- var newQuery = ++queryVersion.current;
51
+
52
+ var _resolvePluginUpdates = (0, _utils.resolvePluginUpdates)(activePlugin, state),
53
+ next = _resolvePluginUpdates.next,
54
+ cancel = _resolvePluginUpdates.cancel;
71
55
 
72
56
  var updateResults = /*#__PURE__*/function () {
73
57
  var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
74
- var done, value, _result$done, result;
58
+ var _isLoading, _yield$next, value, done;
75
59
 
76
60
  return _regenerator.default.wrap(function _callee$(_context) {
77
61
  while (1) {
78
62
  switch (_context.prev = _context.next) {
79
63
  case 0:
80
64
  _context.prev = 0;
65
+ _isLoading = true;
81
66
 
82
- case 1:
83
- if (!(newQuery === queryVersion.current)) {
84
- _context.next = 23;
85
- break;
86
- }
87
-
88
- done = false;
89
- value = void 0;
90
-
91
- if (!(updates instanceof Promise)) {
92
- _context.next = 11;
67
+ case 2:
68
+ if (!_isLoading) {
69
+ _context.next = 12;
93
70
  break;
94
71
  }
95
72
 
96
- done = true;
97
- _context.next = 8;
98
- return updates;
99
-
100
- case 8:
101
- value = _context.sent;
102
- _context.next = 16;
103
- break;
104
-
105
- case 11:
106
- _context.next = 13;
107
- return updates.next();
73
+ _context.next = 5;
74
+ return next();
108
75
 
109
- case 13:
110
- result = _context.sent;
111
- done = (_result$done = result.done) !== null && _result$done !== void 0 ? _result$done : false;
112
- value = result.value;
113
-
114
- case 16:
115
- if (!(newQuery !== queryVersion.current)) {
116
- _context.next = 18;
117
- break;
118
- }
119
-
120
- return _context.abrupt("return");
121
-
122
- case 18:
76
+ case 5:
77
+ _yield$next = _context.sent;
78
+ value = _yield$next.value;
79
+ done = _yield$next.done;
80
+ _isLoading = !done;
123
81
  dispatch({
124
- items: limit(value.data),
125
- isLoading: !done
82
+ type: 'SUCCESS',
83
+ payload: {
84
+ items: limit(value.data),
85
+ isLoading: !done
86
+ }
126
87
  });
127
-
128
- if (!done) {
129
- _context.next = 21;
130
- break;
131
- }
132
-
133
- return _context.abrupt("return");
134
-
135
- case 21:
136
- _context.next = 1;
88
+ _context.next = 2;
137
89
  break;
138
90
 
139
- case 23:
140
- _context.next = 28;
91
+ case 12:
92
+ _context.next = 17;
141
93
  break;
142
94
 
143
- case 25:
144
- _context.prev = 25;
95
+ case 14:
96
+ _context.prev = 14;
145
97
  _context.t0 = _context["catch"](0);
146
- dispatch({
147
- error: _context.t0,
148
- items: null,
149
- isLoading: false
150
- });
151
98
 
152
- case 28:
99
+ if (!(_context.t0 instanceof _utils.CancellationError)) {
100
+ dispatch({
101
+ type: 'ERROR',
102
+ payload: _context.t0
103
+ });
104
+ }
105
+
106
+ case 17:
153
107
  case "end":
154
108
  return _context.stop();
155
109
  }
156
110
  }
157
- }, _callee, null, [[0, 25]]);
111
+ }, _callee, null, [[0, 14]]);
158
112
  }));
159
113
 
160
114
  return function updateResults() {
@@ -163,7 +117,8 @@ function usePlugins(state, activeTab, plugins) {
163
117
  }();
164
118
 
165
119
  updateResults();
166
- }, [activePlugin, queryVersion, state, retries]);
120
+ return cancel;
121
+ }, [activePlugin, state, retries, dispatch]);
167
122
  var tabs = (0, _react.useMemo)(function () {
168
123
  if (!plugins || plugins.length <= 1) {
169
124
  return [];
@@ -182,6 +137,9 @@ function usePlugins(state, activeTab, plugins) {
182
137
  return ++prev;
183
138
  });
184
139
  }, []);
140
+ var items = pluginState.items,
141
+ isLoading = pluginState.isLoading,
142
+ error = pluginState.error;
185
143
  return {
186
144
  tabs: tabs,
187
145
  items: items,
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.INITIAL_STATE = exports.ACTION_SUCCESS = exports.ACTION_LOADING = exports.ACTION_ERRORED = exports.ACTION_CLEAR = void 0;
9
+ exports.reducer = reducer;
10
+ exports.usePluginReducer = void 0;
11
+
12
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
+
14
+ var _react = require("react");
15
+
16
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
17
+
18
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
19
+
20
+ var ACTION_CLEAR = 'CLEAR';
21
+ exports.ACTION_CLEAR = ACTION_CLEAR;
22
+ var ACTION_LOADING = 'LOADING';
23
+ exports.ACTION_LOADING = ACTION_LOADING;
24
+ var ACTION_SUCCESS = 'SUCCESS';
25
+ exports.ACTION_SUCCESS = ACTION_SUCCESS;
26
+ var ACTION_ERRORED = 'ERROR';
27
+ exports.ACTION_ERRORED = ACTION_ERRORED;
28
+ var INITIAL_STATE = {
29
+ items: null,
30
+ error: null,
31
+ isLoading: false
32
+ };
33
+ exports.INITIAL_STATE = INITIAL_STATE;
34
+
35
+ function reducer(state, action) {
36
+ switch (action.type) {
37
+ case ACTION_CLEAR:
38
+ return _objectSpread({}, INITIAL_STATE);
39
+
40
+ case ACTION_LOADING:
41
+ return _objectSpread(_objectSpread({}, INITIAL_STATE), {}, {
42
+ isLoading: true
43
+ });
44
+
45
+ case ACTION_SUCCESS:
46
+ return _objectSpread(_objectSpread(_objectSpread({}, state), action.payload), {}, {
47
+ error: null
48
+ });
49
+
50
+ case ACTION_ERRORED:
51
+ return _objectSpread(_objectSpread({}, INITIAL_STATE), {}, {
52
+ error: action.payload
53
+ });
54
+
55
+ default:
56
+ return state;
57
+ }
58
+ }
59
+
60
+ var usePluginReducer = function usePluginReducer() {
61
+ return (0, _react.useReducer)(reducer, INITIAL_STATE);
62
+ };
63
+
64
+ exports.usePluginReducer = usePluginReducer;
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.resolvePluginUpdates = exports.cancellable = exports.CancellationError = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
+
16
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
17
+
18
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
19
+
20
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
21
+
22
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
23
+
24
+ var _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapNativeSuper"));
25
+
26
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
27
+
28
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
29
+
30
+ var CancellationError = /*#__PURE__*/function (_Error) {
31
+ (0, _inherits2.default)(CancellationError, _Error);
32
+
33
+ var _super = _createSuper(CancellationError);
34
+
35
+ function CancellationError() {
36
+ (0, _classCallCheck2.default)(this, CancellationError);
37
+ return _super.apply(this, arguments);
38
+ }
39
+
40
+ return (0, _createClass2.default)(CancellationError);
41
+ }( /*#__PURE__*/(0, _wrapNativeSuper2.default)(Error));
42
+
43
+ exports.CancellationError = CancellationError;
44
+
45
+ var cancellable = function cancellable(promise) {
46
+ var reject;
47
+ return {
48
+ promise: new Promise(function (resolve, _reject) {
49
+ reject = _reject;
50
+ promise.then(resolve, reject);
51
+ }),
52
+ cancel: function cancel() {
53
+ reject(new CancellationError('Promise cancelled'));
54
+ }
55
+ };
56
+ };
57
+ /**
58
+ * Wraps the resolve with a cancellation wrapper and makes promise + generator plugin resolves more compatible
59
+ * Calling cancel for generator plugin prevents it from yielding any further updates
60
+ */
61
+
62
+
63
+ exports.cancellable = cancellable;
64
+
65
+ var resolvePluginUpdates = function resolvePluginUpdates(plugin, state) {
66
+ var updates = plugin.resolve(state); // Promise plugin
67
+
68
+ if (updates instanceof Promise) {
69
+ var _cancellable = cancellable(updates),
70
+ promise = _cancellable.promise,
71
+ cancel = _cancellable.cancel;
72
+
73
+ return {
74
+ cancel: cancel,
75
+ next: function () {
76
+ var _next = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
77
+ return _regenerator.default.wrap(function _callee$(_context) {
78
+ while (1) {
79
+ switch (_context.prev = _context.next) {
80
+ case 0:
81
+ _context.next = 2;
82
+ return promise;
83
+
84
+ case 2:
85
+ _context.t0 = _context.sent;
86
+ return _context.abrupt("return", {
87
+ value: _context.t0,
88
+ done: true
89
+ });
90
+
91
+ case 4:
92
+ case "end":
93
+ return _context.stop();
94
+ }
95
+ }
96
+ }, _callee);
97
+ }));
98
+
99
+ function next() {
100
+ return _next.apply(this, arguments);
101
+ }
102
+
103
+ return next;
104
+ }()
105
+ };
106
+ } // Generator plugin
107
+
108
+
109
+ var cancellationCallbacks = [];
110
+ var cancelled = false;
111
+ return {
112
+ cancel: function cancel() {
113
+ cancelled = true;
114
+ cancellationCallbacks.forEach(function (cb) {
115
+ return cb();
116
+ });
117
+ },
118
+ next: function next() {
119
+ var _cancellable2 = cancellable(updates.next()),
120
+ promise = _cancellable2.promise,
121
+ cancel = _cancellable2.cancel;
122
+
123
+ if (cancelled) {
124
+ cancel();
125
+ } else {
126
+ cancellationCallbacks.push(cancel);
127
+ }
128
+
129
+ return promise;
130
+ }
131
+ };
132
+ };
133
+
134
+ exports.resolvePluginUpdates = resolvePluginUpdates;
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/link-picker",
3
- "version": "1.8.3",
3
+ "version": "1.8.4",
4
4
  "sideEffects": false
5
5
  }
@@ -1,25 +1,10 @@
1
- import { useEffect, useReducer, useMemo, useRef, useState, useCallback } from 'react';
1
+ import { useEffect, useMemo, useState, useCallback } from 'react';
2
2
  import { RECENT_SEARCH_LIST_SIZE } from '../../ui/link-picker';
3
-
4
- function reducer(state, payload) {
5
- return { ...state,
6
- ...payload
7
- };
8
- }
9
-
3
+ import { CancellationError, resolvePluginUpdates } from './utils';
4
+ import { usePluginReducer } from './reducer';
10
5
  export function usePlugins(state, activeTab, plugins) {
11
6
  const [retries, setRetries] = useState(0);
12
- const [pluginState, dispatch] = useReducer(reducer, {
13
- items: null,
14
- error: null,
15
- isLoading: false
16
- });
17
- const {
18
- items,
19
- isLoading,
20
- error
21
- } = pluginState;
22
- const queryVersion = useRef(0);
7
+ const [pluginState, dispatch] = usePluginReducer();
23
8
  const activePlugin = plugins === null || plugins === void 0 ? void 0 : plugins[activeTab];
24
9
  useEffect(() => {
25
10
  if (!activePlugin) {
@@ -28,62 +13,50 @@ export function usePlugins(state, activeTab, plugins) {
28
13
 
29
14
  if (!state) {
30
15
  dispatch({
31
- items: null,
32
- isLoading: false,
33
- error: null
16
+ type: 'CLEAR'
34
17
  });
35
18
  return;
36
19
  }
37
20
 
38
- const updates = activePlugin.resolve(state);
39
21
  dispatch({
40
- items: null,
41
- isLoading: true,
42
- error: null
22
+ type: 'LOADING'
43
23
  });
44
- const newQuery = ++queryVersion.current;
24
+ const {
25
+ next,
26
+ cancel
27
+ } = resolvePluginUpdates(activePlugin, state);
45
28
 
46
29
  const updateResults = async () => {
47
30
  try {
48
- while (newQuery === queryVersion.current) {
49
- let done = false;
50
- let value;
51
-
52
- if (updates instanceof Promise) {
53
- done = true;
54
- value = await updates;
55
- } else {
56
- var _result$done;
57
-
58
- const result = await updates.next();
59
- done = (_result$done = result.done) !== null && _result$done !== void 0 ? _result$done : false;
60
- value = result.value;
61
- }
62
-
63
- if (newQuery !== queryVersion.current) {
64
- return;
65
- }
31
+ let isLoading = true;
66
32
 
33
+ while (isLoading) {
34
+ const {
35
+ value,
36
+ done
37
+ } = await next();
38
+ isLoading = !done;
67
39
  dispatch({
68
- items: limit(value.data),
69
- isLoading: !done
40
+ type: 'SUCCESS',
41
+ payload: {
42
+ items: limit(value.data),
43
+ isLoading: !done
44
+ }
70
45
  });
71
-
72
- if (done) {
73
- return;
74
- }
75
46
  }
76
47
  } catch (error) {
77
- dispatch({
78
- error,
79
- items: null,
80
- isLoading: false
81
- });
48
+ if (!(error instanceof CancellationError)) {
49
+ dispatch({
50
+ type: 'ERROR',
51
+ payload: error
52
+ });
53
+ }
82
54
  }
83
55
  };
84
56
 
85
57
  updateResults();
86
- }, [activePlugin, queryVersion, state, retries]);
58
+ return cancel;
59
+ }, [activePlugin, state, retries, dispatch]);
87
60
  const tabs = useMemo(() => {
88
61
  if (!plugins || plugins.length <= 1) {
89
62
  return [];
@@ -96,6 +69,11 @@ export function usePlugins(state, activeTab, plugins) {
96
69
  const handleRetry = useCallback(() => {
97
70
  setRetries(prev => ++prev);
98
71
  }, []);
72
+ const {
73
+ items,
74
+ isLoading,
75
+ error
76
+ } = pluginState;
99
77
  return {
100
78
  tabs,
101
79
  items,
@@ -0,0 +1,37 @@
1
+ import { useReducer } from 'react';
2
+ export const ACTION_CLEAR = 'CLEAR';
3
+ export const ACTION_LOADING = 'LOADING';
4
+ export const ACTION_SUCCESS = 'SUCCESS';
5
+ export const ACTION_ERRORED = 'ERROR';
6
+ export const INITIAL_STATE = {
7
+ items: null,
8
+ error: null,
9
+ isLoading: false
10
+ };
11
+ export function reducer(state, action) {
12
+ switch (action.type) {
13
+ case ACTION_CLEAR:
14
+ return { ...INITIAL_STATE
15
+ };
16
+
17
+ case ACTION_LOADING:
18
+ return { ...INITIAL_STATE,
19
+ isLoading: true
20
+ };
21
+
22
+ case ACTION_SUCCESS:
23
+ return { ...state,
24
+ ...action.payload,
25
+ error: null
26
+ };
27
+
28
+ case ACTION_ERRORED:
29
+ return { ...INITIAL_STATE,
30
+ error: action.payload
31
+ };
32
+
33
+ default:
34
+ return state;
35
+ }
36
+ }
37
+ export const usePluginReducer = () => useReducer(reducer, INITIAL_STATE);
@@ -0,0 +1,59 @@
1
+ export class CancellationError extends Error {}
2
+ export const cancellable = promise => {
3
+ let reject;
4
+ return {
5
+ promise: new Promise((resolve, _reject) => {
6
+ reject = _reject;
7
+ promise.then(resolve, reject);
8
+ }),
9
+ cancel: () => {
10
+ reject(new CancellationError('Promise cancelled'));
11
+ }
12
+ };
13
+ };
14
+ /**
15
+ * Wraps the resolve with a cancellation wrapper and makes promise + generator plugin resolves more compatible
16
+ * Calling cancel for generator plugin prevents it from yielding any further updates
17
+ */
18
+
19
+ export const resolvePluginUpdates = (plugin, state) => {
20
+ const updates = plugin.resolve(state); // Promise plugin
21
+
22
+ if (updates instanceof Promise) {
23
+ const {
24
+ promise,
25
+ cancel
26
+ } = cancellable(updates);
27
+ return {
28
+ cancel,
29
+ next: async () => ({
30
+ value: await promise,
31
+ done: true
32
+ })
33
+ };
34
+ } // Generator plugin
35
+
36
+
37
+ const cancellationCallbacks = [];
38
+ let cancelled = false;
39
+ return {
40
+ cancel: () => {
41
+ cancelled = true;
42
+ cancellationCallbacks.forEach(cb => cb());
43
+ },
44
+ next: () => {
45
+ const {
46
+ promise,
47
+ cancel
48
+ } = cancellable(updates.next());
49
+
50
+ if (cancelled) {
51
+ cancel();
52
+ } else {
53
+ cancellationCallbacks.push(cancel);
54
+ }
55
+
56
+ return promise;
57
+ }
58
+ };
59
+ };
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/link-picker",
3
- "version": "1.8.3",
3
+ "version": "1.8.4",
4
4
  "sideEffects": false
5
5
  }
@@ -1,38 +1,21 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
5
-
6
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
7
-
8
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
9
-
10
- import { useEffect, useReducer, useMemo, useRef, useState, useCallback } from 'react';
4
+ import { useEffect, useMemo, useState, useCallback } from 'react';
11
5
  import { RECENT_SEARCH_LIST_SIZE } from '../../ui/link-picker';
12
-
13
- function reducer(state, payload) {
14
- return _objectSpread(_objectSpread({}, state), payload);
15
- }
16
-
6
+ import { CancellationError, resolvePluginUpdates } from './utils';
7
+ import { usePluginReducer } from './reducer';
17
8
  export function usePlugins(state, activeTab, plugins) {
18
9
  var _useState = useState(0),
19
10
  _useState2 = _slicedToArray(_useState, 2),
20
11
  retries = _useState2[0],
21
12
  setRetries = _useState2[1];
22
13
 
23
- var _useReducer = useReducer(reducer, {
24
- items: null,
25
- error: null,
26
- isLoading: false
27
- }),
28
- _useReducer2 = _slicedToArray(_useReducer, 2),
29
- pluginState = _useReducer2[0],
30
- dispatch = _useReducer2[1];
14
+ var _usePluginReducer = usePluginReducer(),
15
+ _usePluginReducer2 = _slicedToArray(_usePluginReducer, 2),
16
+ pluginState = _usePluginReducer2[0],
17
+ dispatch = _usePluginReducer2[1];
31
18
 
32
- var items = pluginState.items,
33
- isLoading = pluginState.isLoading,
34
- error = pluginState.error;
35
- var queryVersion = useRef(0);
36
19
  var activePlugin = plugins === null || plugins === void 0 ? void 0 : plugins[activeTab];
37
20
  useEffect(function () {
38
21
  if (!activePlugin) {
@@ -41,107 +24,75 @@ export function usePlugins(state, activeTab, plugins) {
41
24
 
42
25
  if (!state) {
43
26
  dispatch({
44
- items: null,
45
- isLoading: false,
46
- error: null
27
+ type: 'CLEAR'
47
28
  });
48
29
  return;
49
30
  }
50
31
 
51
- var updates = activePlugin.resolve(state);
52
32
  dispatch({
53
- items: null,
54
- isLoading: true,
55
- error: null
33
+ type: 'LOADING'
56
34
  });
57
- var newQuery = ++queryVersion.current;
35
+
36
+ var _resolvePluginUpdates = resolvePluginUpdates(activePlugin, state),
37
+ next = _resolvePluginUpdates.next,
38
+ cancel = _resolvePluginUpdates.cancel;
58
39
 
59
40
  var updateResults = /*#__PURE__*/function () {
60
41
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
61
- var done, value, _result$done, result;
42
+ var _isLoading, _yield$next, value, done;
62
43
 
63
44
  return _regeneratorRuntime.wrap(function _callee$(_context) {
64
45
  while (1) {
65
46
  switch (_context.prev = _context.next) {
66
47
  case 0:
67
48
  _context.prev = 0;
49
+ _isLoading = true;
68
50
 
69
- case 1:
70
- if (!(newQuery === queryVersion.current)) {
71
- _context.next = 23;
72
- break;
73
- }
74
-
75
- done = false;
76
- value = void 0;
77
-
78
- if (!(updates instanceof Promise)) {
79
- _context.next = 11;
80
- break;
81
- }
82
-
83
- done = true;
84
- _context.next = 8;
85
- return updates;
86
-
87
- case 8:
88
- value = _context.sent;
89
- _context.next = 16;
90
- break;
91
-
92
- case 11:
93
- _context.next = 13;
94
- return updates.next();
95
-
96
- case 13:
97
- result = _context.sent;
98
- done = (_result$done = result.done) !== null && _result$done !== void 0 ? _result$done : false;
99
- value = result.value;
100
-
101
- case 16:
102
- if (!(newQuery !== queryVersion.current)) {
103
- _context.next = 18;
51
+ case 2:
52
+ if (!_isLoading) {
53
+ _context.next = 12;
104
54
  break;
105
55
  }
106
56
 
107
- return _context.abrupt("return");
57
+ _context.next = 5;
58
+ return next();
108
59
 
109
- case 18:
60
+ case 5:
61
+ _yield$next = _context.sent;
62
+ value = _yield$next.value;
63
+ done = _yield$next.done;
64
+ _isLoading = !done;
110
65
  dispatch({
111
- items: limit(value.data),
112
- isLoading: !done
66
+ type: 'SUCCESS',
67
+ payload: {
68
+ items: limit(value.data),
69
+ isLoading: !done
70
+ }
113
71
  });
114
-
115
- if (!done) {
116
- _context.next = 21;
117
- break;
118
- }
119
-
120
- return _context.abrupt("return");
121
-
122
- case 21:
123
- _context.next = 1;
72
+ _context.next = 2;
124
73
  break;
125
74
 
126
- case 23:
127
- _context.next = 28;
75
+ case 12:
76
+ _context.next = 17;
128
77
  break;
129
78
 
130
- case 25:
131
- _context.prev = 25;
79
+ case 14:
80
+ _context.prev = 14;
132
81
  _context.t0 = _context["catch"](0);
133
- dispatch({
134
- error: _context.t0,
135
- items: null,
136
- isLoading: false
137
- });
138
82
 
139
- case 28:
83
+ if (!(_context.t0 instanceof CancellationError)) {
84
+ dispatch({
85
+ type: 'ERROR',
86
+ payload: _context.t0
87
+ });
88
+ }
89
+
90
+ case 17:
140
91
  case "end":
141
92
  return _context.stop();
142
93
  }
143
94
  }
144
- }, _callee, null, [[0, 25]]);
95
+ }, _callee, null, [[0, 14]]);
145
96
  }));
146
97
 
147
98
  return function updateResults() {
@@ -150,7 +101,8 @@ export function usePlugins(state, activeTab, plugins) {
150
101
  }();
151
102
 
152
103
  updateResults();
153
- }, [activePlugin, queryVersion, state, retries]);
104
+ return cancel;
105
+ }, [activePlugin, state, retries, dispatch]);
154
106
  var tabs = useMemo(function () {
155
107
  if (!plugins || plugins.length <= 1) {
156
108
  return [];
@@ -169,6 +121,9 @@ export function usePlugins(state, activeTab, plugins) {
169
121
  return ++prev;
170
122
  });
171
123
  }, []);
124
+ var items = pluginState.items,
125
+ isLoading = pluginState.isLoading,
126
+ error = pluginState.error;
172
127
  return {
173
128
  tabs: tabs,
174
129
  items: items,
@@ -0,0 +1,43 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+
7
+ import { useReducer } from 'react';
8
+ export var ACTION_CLEAR = 'CLEAR';
9
+ export var ACTION_LOADING = 'LOADING';
10
+ export var ACTION_SUCCESS = 'SUCCESS';
11
+ export var ACTION_ERRORED = 'ERROR';
12
+ export var INITIAL_STATE = {
13
+ items: null,
14
+ error: null,
15
+ isLoading: false
16
+ };
17
+ export function reducer(state, action) {
18
+ switch (action.type) {
19
+ case ACTION_CLEAR:
20
+ return _objectSpread({}, INITIAL_STATE);
21
+
22
+ case ACTION_LOADING:
23
+ return _objectSpread(_objectSpread({}, INITIAL_STATE), {}, {
24
+ isLoading: true
25
+ });
26
+
27
+ case ACTION_SUCCESS:
28
+ return _objectSpread(_objectSpread(_objectSpread({}, state), action.payload), {}, {
29
+ error: null
30
+ });
31
+
32
+ case ACTION_ERRORED:
33
+ return _objectSpread(_objectSpread({}, INITIAL_STATE), {}, {
34
+ error: action.payload
35
+ });
36
+
37
+ default:
38
+ return state;
39
+ }
40
+ }
41
+ export var usePluginReducer = function usePluginReducer() {
42
+ return useReducer(reducer, INITIAL_STATE);
43
+ };
@@ -0,0 +1,111 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
+ import _inherits from "@babel/runtime/helpers/inherits";
5
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
+ import _wrapNativeSuper from "@babel/runtime/helpers/wrapNativeSuper";
8
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
9
+
10
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
11
+
12
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
13
+
14
+ export var CancellationError = /*#__PURE__*/function (_Error) {
15
+ _inherits(CancellationError, _Error);
16
+
17
+ var _super = _createSuper(CancellationError);
18
+
19
+ function CancellationError() {
20
+ _classCallCheck(this, CancellationError);
21
+
22
+ return _super.apply(this, arguments);
23
+ }
24
+
25
+ return _createClass(CancellationError);
26
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
27
+ export var cancellable = function cancellable(promise) {
28
+ var reject;
29
+ return {
30
+ promise: new Promise(function (resolve, _reject) {
31
+ reject = _reject;
32
+ promise.then(resolve, reject);
33
+ }),
34
+ cancel: function cancel() {
35
+ reject(new CancellationError('Promise cancelled'));
36
+ }
37
+ };
38
+ };
39
+ /**
40
+ * Wraps the resolve with a cancellation wrapper and makes promise + generator plugin resolves more compatible
41
+ * Calling cancel for generator plugin prevents it from yielding any further updates
42
+ */
43
+
44
+ export var resolvePluginUpdates = function resolvePluginUpdates(plugin, state) {
45
+ var updates = plugin.resolve(state); // Promise plugin
46
+
47
+ if (updates instanceof Promise) {
48
+ var _cancellable = cancellable(updates),
49
+ promise = _cancellable.promise,
50
+ cancel = _cancellable.cancel;
51
+
52
+ return {
53
+ cancel: cancel,
54
+ next: function () {
55
+ var _next = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
56
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
57
+ while (1) {
58
+ switch (_context.prev = _context.next) {
59
+ case 0:
60
+ _context.next = 2;
61
+ return promise;
62
+
63
+ case 2:
64
+ _context.t0 = _context.sent;
65
+ return _context.abrupt("return", {
66
+ value: _context.t0,
67
+ done: true
68
+ });
69
+
70
+ case 4:
71
+ case "end":
72
+ return _context.stop();
73
+ }
74
+ }
75
+ }, _callee);
76
+ }));
77
+
78
+ function next() {
79
+ return _next.apply(this, arguments);
80
+ }
81
+
82
+ return next;
83
+ }()
84
+ };
85
+ } // Generator plugin
86
+
87
+
88
+ var cancellationCallbacks = [];
89
+ var cancelled = false;
90
+ return {
91
+ cancel: function cancel() {
92
+ cancelled = true;
93
+ cancellationCallbacks.forEach(function (cb) {
94
+ return cb();
95
+ });
96
+ },
97
+ next: function next() {
98
+ var _cancellable2 = cancellable(updates.next()),
99
+ promise = _cancellable2.promise,
100
+ cancel = _cancellable2.cancel;
101
+
102
+ if (cancelled) {
103
+ cancel();
104
+ } else {
105
+ cancellationCallbacks.push(cancel);
106
+ }
107
+
108
+ return promise;
109
+ }
110
+ };
111
+ };
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/link-picker",
3
- "version": "1.8.3",
3
+ "version": "1.8.4",
4
4
  "sideEffects": false
5
5
  }
@@ -0,0 +1,30 @@
1
+ /// <reference types="react" />
2
+ import { LinkSearchListItemData } from '../../ui/types';
3
+ export interface PluginState {
4
+ items: LinkSearchListItemData[] | null;
5
+ error: unknown | null;
6
+ isLoading: boolean;
7
+ }
8
+ export declare const ACTION_CLEAR = "CLEAR";
9
+ export declare const ACTION_LOADING = "LOADING";
10
+ export declare const ACTION_SUCCESS = "SUCCESS";
11
+ export declare const ACTION_ERRORED = "ERROR";
12
+ interface ClearAction {
13
+ type: typeof ACTION_CLEAR;
14
+ }
15
+ interface ResolveLoadingAction {
16
+ type: typeof ACTION_LOADING;
17
+ }
18
+ interface ResolveSuccessAction {
19
+ type: typeof ACTION_SUCCESS;
20
+ payload: Pick<PluginState, 'items' | 'isLoading'>;
21
+ }
22
+ interface ResolveErrorAction {
23
+ type: typeof ACTION_ERRORED;
24
+ payload: PluginState['error'];
25
+ }
26
+ export declare type ReducerAction = ClearAction | ResolveLoadingAction | ResolveSuccessAction | ResolveErrorAction;
27
+ export declare const INITIAL_STATE: PluginState;
28
+ export declare function reducer(state: PluginState, action: ReducerAction): PluginState;
29
+ export declare const usePluginReducer: () => [PluginState, import("react").Dispatch<ReducerAction>];
30
+ export {};
@@ -0,0 +1,18 @@
1
+ import { LinkPickerPlugin, LinkPickerState, ResolveResult } from '../../ui/types';
2
+ export declare class CancellationError extends Error {
3
+ }
4
+ export declare const cancellable: <T extends unknown>(promise: Promise<T>) => {
5
+ promise: Promise<T>;
6
+ cancel: () => void;
7
+ };
8
+ /**
9
+ * Wraps the resolve with a cancellation wrapper and makes promise + generator plugin resolves more compatible
10
+ * Calling cancel for generator plugin prevents it from yielding any further updates
11
+ */
12
+ export declare const resolvePluginUpdates: (plugin: LinkPickerPlugin, state: LinkPickerState) => {
13
+ cancel: () => void;
14
+ next: () => Promise<{
15
+ done?: boolean;
16
+ value: ResolveResult;
17
+ }>;
18
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/link-picker",
3
- "version": "1.8.3",
3
+ "version": "1.8.4",
4
4
  "description": "Standalone link picker",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "publishConfig": {
@@ -32,16 +32,16 @@
32
32
  "dependencies": {
33
33
  "@atlaskit/analytics-next": "^8.3.1",
34
34
  "@atlaskit/button": "^16.2.0",
35
- "@atlaskit/empty-state": "^7.3.0",
35
+ "@atlaskit/empty-state": "^7.4.0",
36
36
  "@atlaskit/form": "^8.5.1",
37
37
  "@atlaskit/icon": "^21.10.0",
38
38
  "@atlaskit/spinner": "^15.1.0",
39
- "@atlaskit/tabs": "^13.2.11",
40
- "@atlaskit/textfield": "^5.1.7",
41
- "@atlaskit/theme": "^12.1.0",
39
+ "@atlaskit/tabs": "^13.3.0",
40
+ "@atlaskit/textfield": "^5.2.0",
41
+ "@atlaskit/theme": "^12.2.0",
42
42
  "@atlaskit/tokens": "^0.10.0",
43
43
  "@atlaskit/tooltip": "^17.5.0",
44
- "@atlaskit/visually-hidden": "^1.0.0",
44
+ "@atlaskit/visually-hidden": "^1.1.0",
45
45
  "@babel/runtime": "^7.0.0",
46
46
  "@emotion/react": "^11.7.1",
47
47
  "date-fns": "^2.17.0",
@@ -59,7 +59,7 @@
59
59
  "@atlaskit/link-test-helpers": "^1.4.0",
60
60
  "@atlaskit/popup": "^1.4.0",
61
61
  "@atlaskit/ssr": "*",
62
- "@atlaskit/toggle": "^12.4.0",
62
+ "@atlaskit/toggle": "^12.5.0",
63
63
  "@atlaskit/visual-regression": "*",
64
64
  "@atlaskit/webdriver-runner": "*",
65
65
  "@atlassian/atlassian-frontend-prettier-config-1.0.0": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.0",