@atlaskit/media-client 14.1.0 → 14.3.1

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.
Files changed (81) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/cjs/client/file-fetcher/index.js +1 -10
  3. package/dist/cjs/client/media-client.js +50 -1
  4. package/dist/cjs/client/media-store/resolveAuth.js +22 -19
  5. package/dist/cjs/client/mobile-upload.js +98 -0
  6. package/dist/cjs/index.js +8 -0
  7. package/dist/cjs/models/mobile-upload.js +5 -0
  8. package/dist/cjs/utils/imageResizeModeToFileImageMode.js +12 -0
  9. package/dist/cjs/utils/mobileUpload/error.js +67 -0
  10. package/dist/cjs/utils/mobileUpload/helpers.js +110 -0
  11. package/dist/cjs/utils/mobileUpload/index.js +49 -0
  12. package/dist/cjs/utils/mobileUpload/servicesCache.js +12 -0
  13. package/dist/cjs/utils/mobileUpload/stateMachine/index.js +103 -0
  14. package/dist/cjs/utils/mobileUpload/stateMachine/states/error.js +10 -0
  15. package/dist/cjs/utils/mobileUpload/stateMachine/states/processed.js +10 -0
  16. package/dist/cjs/utils/mobileUpload/stateMachine/states/processing.js +77 -0
  17. package/dist/cjs/utils/mobileUpload/stateMachine/states/processingFailed.js +10 -0
  18. package/dist/cjs/utils/mobileUpload/stateMachine/states/uploading.js +70 -0
  19. package/dist/cjs/utils/mobileUpload/stateMachine/types.js +5 -0
  20. package/dist/cjs/utils/polling/index.js +5 -6
  21. package/dist/cjs/version.json +1 -1
  22. package/dist/es2019/client/file-fetcher/index.js +1 -11
  23. package/dist/es2019/client/media-client.js +15 -1
  24. package/dist/es2019/client/media-store/resolveAuth.js +3 -3
  25. package/dist/es2019/client/mobile-upload.js +80 -0
  26. package/dist/es2019/index.js +1 -0
  27. package/dist/es2019/models/mobile-upload.js +1 -0
  28. package/dist/es2019/utils/imageResizeModeToFileImageMode.js +1 -0
  29. package/dist/es2019/utils/mobileUpload/error.js +30 -0
  30. package/dist/es2019/utils/mobileUpload/helpers.js +55 -0
  31. package/dist/es2019/utils/mobileUpload/index.js +4 -0
  32. package/dist/es2019/utils/mobileUpload/servicesCache.js +4 -0
  33. package/dist/es2019/utils/mobileUpload/stateMachine/index.js +52 -0
  34. package/dist/es2019/utils/mobileUpload/stateMachine/states/error.js +3 -0
  35. package/dist/es2019/utils/mobileUpload/stateMachine/states/processed.js +3 -0
  36. package/dist/es2019/utils/mobileUpload/stateMachine/states/processing.js +60 -0
  37. package/dist/es2019/utils/mobileUpload/stateMachine/states/processingFailed.js +3 -0
  38. package/dist/es2019/utils/mobileUpload/stateMachine/states/uploading.js +44 -0
  39. package/dist/es2019/utils/mobileUpload/stateMachine/types.js +1 -0
  40. package/dist/es2019/utils/polling/index.js +5 -5
  41. package/dist/es2019/version.json +1 -1
  42. package/dist/esm/client/file-fetcher/index.js +1 -9
  43. package/dist/esm/client/media-client.js +44 -1
  44. package/dist/esm/client/media-store/resolveAuth.js +20 -17
  45. package/dist/esm/client/mobile-upload.js +83 -0
  46. package/dist/esm/index.js +1 -0
  47. package/dist/esm/models/mobile-upload.js +1 -0
  48. package/dist/esm/utils/imageResizeModeToFileImageMode.js +3 -0
  49. package/dist/esm/utils/mobileUpload/error.js +51 -0
  50. package/dist/esm/utils/mobileUpload/helpers.js +86 -0
  51. package/dist/esm/utils/mobileUpload/index.js +4 -0
  52. package/dist/esm/utils/mobileUpload/servicesCache.js +4 -0
  53. package/dist/esm/utils/mobileUpload/stateMachine/index.js +78 -0
  54. package/dist/esm/utils/mobileUpload/stateMachine/states/error.js +3 -0
  55. package/dist/esm/utils/mobileUpload/stateMachine/states/processed.js +3 -0
  56. package/dist/esm/utils/mobileUpload/stateMachine/states/processing.js +68 -0
  57. package/dist/esm/utils/mobileUpload/stateMachine/states/processingFailed.js +3 -0
  58. package/dist/esm/utils/mobileUpload/stateMachine/states/uploading.js +58 -0
  59. package/dist/esm/utils/mobileUpload/stateMachine/types.js +1 -0
  60. package/dist/esm/utils/polling/index.js +5 -5
  61. package/dist/esm/version.json +1 -1
  62. package/dist/types/client/media-client.d.ts +3 -0
  63. package/dist/types/client/media-store/resolveAuth.d.ts +2 -2
  64. package/dist/types/client/mobile-upload.d.ts +11 -0
  65. package/dist/types/index.d.ts +4 -2
  66. package/dist/types/models/mobile-upload.d.ts +28 -0
  67. package/dist/types/utils/imageResizeModeToFileImageMode.d.ts +3 -0
  68. package/dist/types/utils/mobileUpload/error.d.ts +29 -0
  69. package/dist/types/utils/mobileUpload/helpers.d.ts +8 -0
  70. package/dist/types/utils/mobileUpload/index.d.ts +5 -0
  71. package/dist/types/utils/mobileUpload/servicesCache.d.ts +4 -0
  72. package/dist/types/utils/mobileUpload/stateMachine/index.d.ts +7 -0
  73. package/dist/types/utils/mobileUpload/stateMachine/states/error.d.ts +3 -0
  74. package/dist/types/utils/mobileUpload/stateMachine/states/processed.d.ts +3 -0
  75. package/dist/types/utils/mobileUpload/stateMachine/states/processing.d.ts +3 -0
  76. package/dist/types/utils/mobileUpload/stateMachine/states/processingFailed.d.ts +3 -0
  77. package/dist/types/utils/mobileUpload/stateMachine/states/uploading.d.ts +3 -0
  78. package/dist/types/utils/mobileUpload/stateMachine/types.d.ts +69 -0
  79. package/dist/types/utils/polling/errors.d.ts +1 -1
  80. package/dist/types/utils/polling/index.d.ts +8 -3
  81. package/package.json +6 -5
@@ -0,0 +1,60 @@
1
+ import { assign, send } from 'xstate';
2
+ export const machineProcessingState = {
3
+ // Events
4
+ on: {
5
+ REMOTE_FILESTATE_PROCESSED: 'processed',
6
+ REMOTE_FILESTATE_PROCESSING_FAILED: 'processingFailed',
7
+ REMOTE_FILESTATE_ERROR: 'error'
8
+ },
9
+ // Initial sub state
10
+ initial: 'loading',
11
+ // Sub states definitions
12
+ states: {
13
+ loading: {
14
+ invoke: {
15
+ src: 'shouldFetchRemoteFileStates',
16
+ onDone: {
17
+ actions: send((_, event) => ({
18
+ // shouldFetchRemoteFileStates resolves a boolean
19
+ type: event.data ? 'REMOTE_FILESTATE_FETCH' : 'REMOTE_FILESTATE_BYPASS'
20
+ }))
21
+ },
22
+ onError: 'idle'
23
+ },
24
+ on: {
25
+ REMOTE_FILESTATE_FETCH: 'fetchingRemoteFileStates',
26
+ REMOTE_FILESTATE_BYPASS: 'idle'
27
+ }
28
+ },
29
+ fetchingRemoteFileStates: {
30
+ invoke: {
31
+ src: 'fetchRemoteFileStates',
32
+ onDone: {
33
+ actions: send(ctx => ({
34
+ type: ctx.currentFileState.status === 'processed' ? 'REMOTE_FILESTATE_PROCESSED' : 'REMOTE_FILESTATE_PROCESSING_FAILED'
35
+ }))
36
+ },
37
+ onError: {
38
+ actions: [assign({
39
+ currentFileState: (ctx, event) => ({
40
+ status: 'error',
41
+ id: ctx.currentFileState.id,
42
+ occurrenceKey: ctx.currentFileState.occurrenceKey,
43
+ message: event.data.message
44
+ })
45
+ }), send({
46
+ type: 'REMOTE_FILESTATE_ERROR'
47
+ })]
48
+ }
49
+ },
50
+ on: {
51
+ REMOTE_FILESTATE_RESULT: {
52
+ actions: assign({
53
+ currentFileState: (_, event) => event.fileState
54
+ })
55
+ }
56
+ }
57
+ },
58
+ idle: {}
59
+ }
60
+ };
@@ -0,0 +1,3 @@
1
+ export const machineProcessingFailedState = {
2
+ type: 'final'
3
+ };
@@ -0,0 +1,44 @@
1
+ import { assign } from 'xstate';
2
+ import { isUploadingFileState } from '../../../../models/file-state';
3
+ export const machineUploadingState = {
4
+ // Events
5
+ on: {
6
+ UPLOAD_PROGRESS: [{
7
+ target: 'uploading',
8
+ cond: (ctx, event) => isUploadingFileState(ctx.currentFileState) && event.progress > ctx.currentFileState.progress,
9
+ actions: assign({
10
+ currentFileState: (ctx, event) => ({ ...ctx.currentFileState,
11
+ progress: event.progress
12
+ })
13
+ })
14
+ }, {
15
+ target: 'error'
16
+ }],
17
+ UPLOAD_END: {
18
+ target: 'processing',
19
+ actions: assign({
20
+ currentFileState: ctx => isUploadingFileState(ctx.currentFileState) && {
21
+ status: 'processing',
22
+ id: ctx.currentFileState.id,
23
+ occurrenceKey: ctx.currentFileState.occurrenceKey,
24
+ name: ctx.currentFileState.name,
25
+ size: ctx.currentFileState.size,
26
+ mediaType: ctx.currentFileState.mediaType,
27
+ mimeType: ctx.currentFileState.mimeType,
28
+ preview: ctx.currentFileState.preview,
29
+ createdAt: ctx.currentFileState.createdAt
30
+ } || ctx.currentFileState
31
+ })
32
+ },
33
+ UPLOAD_ERROR: {
34
+ target: 'error',
35
+ actions: assign({
36
+ currentFileState: (ctx, event) => ({
37
+ status: 'error',
38
+ id: ctx.currentFileState.id,
39
+ message: event.message
40
+ })
41
+ })
42
+ }
43
+ }
44
+ };
@@ -1,11 +1,11 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import { getMediaFeatureFlag } from '@atlaskit/media-common/mediaFeatureFlags';
3
2
  import { PollingError } from './errors';
3
+ // default polling options without using feature flags
4
4
  export const defaultPollingOptions = {
5
- poll_intervalMs: getMediaFeatureFlag('poll_intervalMs'),
6
- poll_maxAttempts: getMediaFeatureFlag('poll_maxAttempts'),
7
- poll_backoffFactor: getMediaFeatureFlag('poll_backoffFactor'),
8
- poll_maxIntervalMs: getMediaFeatureFlag('poll_maxIntervalMs')
5
+ poll_intervalMs: 3000,
6
+ poll_maxAttempts: 30,
7
+ poll_backoffFactor: 1.25,
8
+ poll_maxIntervalMs: 200000
9
9
  };
10
10
  /**
11
11
  * This class encapsulates polling functionality with the following features:
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/media-client",
3
- "version": "14.1.0",
3
+ "version": "14.3.1",
4
4
  "sideEffects": false
5
5
  }
@@ -37,7 +37,6 @@ import { isMimeTypeSupportedByBrowser, getMediaTypeFromMimeType } from '@atlaski
37
37
  import { shouldFetchRemoteFileStates, shouldFetchRemoteFileStatesObservable } from '../../utils/shouldFetchRemoteFileStates';
38
38
  import { PollingFunction } from '../../utils/polling';
39
39
  import { isEmptyFile } from '../../utils/detectEmptyFile';
40
- import { getMediaFeatureFlag } from '@atlaskit/media-common/mediaFeatureFlags';
41
40
  export { isFileFetcherError, FileFetcherError } from './error';
42
41
  export var FileFetcherImpl = /*#__PURE__*/function () {
43
42
  function FileFetcherImpl(mediaStore) {
@@ -47,14 +46,7 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
47
46
 
48
47
  _defineProperty(this, "createDownloadFileStream", function (id, collectionName, occurrenceKey) {
49
48
  var subject = createFileStateSubject();
50
- var featureFlags = _this.mediaStore.featureFlags;
51
- var pollingOptions = {
52
- poll_intervalMs: getMediaFeatureFlag('poll_intervalMs', featureFlags),
53
- poll_maxAttempts: getMediaFeatureFlag('poll_maxAttempts', featureFlags),
54
- poll_backoffFactor: getMediaFeatureFlag('poll_backoffFactor', featureFlags),
55
- poll_maxIntervalMs: getMediaFeatureFlag('poll_maxIntervalMs', featureFlags)
56
- };
57
- var poll = new PollingFunction(pollingOptions); // ensure subject errors if polling exceeds max iterations or uncaught exception in executor
49
+ var poll = new PollingFunction(); // ensure subject errors if polling exceeds max iterations or uncaught exception in executor
58
50
 
59
51
  poll.onError = function (error) {
60
52
  return subject.error(error);
@@ -8,6 +8,7 @@ import { CollectionFetcher } from './collection-fetcher';
8
8
  import { FileFetcherImpl } from './file-fetcher';
9
9
  import { StargateClient } from './stargate-client';
10
10
  export var MediaClient = /*#__PURE__*/function () {
11
+ // mobile upload is lazily loaded
11
12
  // Deprecated value introduced for backward compatibility with Context
12
13
  function MediaClient(mediaClientConfig, featureFlags) {
13
14
  _classCallCheck(this, MediaClient);
@@ -15,7 +16,8 @@ export var MediaClient = /*#__PURE__*/function () {
15
16
  this.mediaClientConfig = mediaClientConfig;
16
17
  this.featureFlags = featureFlags;
17
18
  this.mediaStore = new MediaStore({
18
- authProvider: mediaClientConfig.authProvider
19
+ authProvider: mediaClientConfig.authProvider,
20
+ initialAuth: mediaClientConfig.initialAuth
19
21
  }, featureFlags);
20
22
  this.config = mediaClientConfig;
21
23
  this.collection = new CollectionFetcher(this.mediaStore);
@@ -67,6 +69,47 @@ export var MediaClient = /*#__PURE__*/function () {
67
69
 
68
70
  return getImageMetadata;
69
71
  }()
72
+ }, {
73
+ key: "mobileUploadPromise",
74
+ value: function () {
75
+ var _mobileUploadPromise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
76
+ var module;
77
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
78
+ while (1) {
79
+ switch (_context2.prev = _context2.next) {
80
+ case 0:
81
+ if (!this.mobileUpload) {
82
+ _context2.next = 2;
83
+ break;
84
+ }
85
+
86
+ return _context2.abrupt("return", this.mobileUpload);
87
+
88
+ case 2:
89
+ _context2.next = 4;
90
+ return import(
91
+ /* webpackChunkName: "@atlaskit-internal_media-client-mobile-upload" */
92
+ './mobile-upload');
93
+
94
+ case 4:
95
+ module = _context2.sent;
96
+ this.mobileUpload = new module.MobileUploadImpl(this.mediaStore);
97
+ return _context2.abrupt("return", this.mobileUpload);
98
+
99
+ case 7:
100
+ case "end":
101
+ return _context2.stop();
102
+ }
103
+ }
104
+ }, _callee2, this);
105
+ }));
106
+
107
+ function mobileUploadPromise() {
108
+ return _mobileUploadPromise.apply(this, arguments);
109
+ }
110
+
111
+ return mobileUploadPromise;
112
+ }()
70
113
  }, {
71
114
  key: "on",
72
115
  value: function on(event, listener) {
@@ -2,54 +2,57 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
3
  import { MediaStoreError } from './error';
4
4
  import { rejectTimeout } from '../../utils/setTimeoutPromise';
5
- export var AUTH_PROVIDER_TIMEOUT = 10000;
5
+ export var DEFAULT_AUTH_PROVIDER_TIMEOUT = 10000;
6
6
  export var resolveAuth = /*#__PURE__*/function () {
7
7
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(authProvider, authContext) {
8
- var auth;
8
+ var authProviderTimeout,
9
+ auth,
10
+ _args = arguments;
9
11
  return _regeneratorRuntime.wrap(function _callee$(_context) {
10
12
  while (1) {
11
13
  switch (_context.prev = _context.next) {
12
14
  case 0:
13
- _context.prev = 0;
14
- _context.next = 3;
15
- return Promise.race([authProvider(authContext), rejectTimeout(AUTH_PROVIDER_TIMEOUT, new MediaStoreError('authProviderTimedOut'))]);
15
+ authProviderTimeout = _args.length > 2 && _args[2] !== undefined ? _args[2] : DEFAULT_AUTH_PROVIDER_TIMEOUT;
16
+ _context.prev = 1;
17
+ _context.next = 4;
18
+ return Promise.race([authProvider(authContext), rejectTimeout(authProviderTimeout, new MediaStoreError('authProviderTimedOut'))]);
16
19
 
17
- case 3:
20
+ case 4:
18
21
  auth = _context.sent;
19
- _context.next = 11;
22
+ _context.next = 12;
20
23
  break;
21
24
 
22
- case 6:
23
- _context.prev = 6;
24
- _context.t0 = _context["catch"](0);
25
+ case 7:
26
+ _context.prev = 7;
27
+ _context.t0 = _context["catch"](1);
25
28
 
26
29
  if (!(_context.t0 instanceof MediaStoreError)) {
27
- _context.next = 10;
30
+ _context.next = 11;
28
31
  break;
29
32
  }
30
33
 
31
34
  throw _context.t0;
32
35
 
33
- case 10:
36
+ case 11:
34
37
  throw new MediaStoreError('failedAuthProvider', _context.t0);
35
38
 
36
- case 11:
39
+ case 12:
37
40
  if (auth) {
38
- _context.next = 13;
41
+ _context.next = 14;
39
42
  break;
40
43
  }
41
44
 
42
45
  throw new MediaStoreError('emptyAuth');
43
46
 
44
- case 13:
47
+ case 14:
45
48
  return _context.abrupt("return", auth);
46
49
 
47
- case 14:
50
+ case 15:
48
51
  case "end":
49
52
  return _context.stop();
50
53
  }
51
54
  }
52
- }, _callee, null, [[0, 6]]);
55
+ }, _callee, null, [[1, 7]]);
53
56
  }));
54
57
 
55
58
  return function resolveAuth(_x, _x2) {
@@ -0,0 +1,83 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import { getMediaTypeFromMimeType } from '@atlaskit/media-common';
4
+ import { getFileStreamsCache } from '../file-streams-cache';
5
+ import { createFileDataloader } from '../utils/createFileDataLoader';
6
+ import { createServicesCache, createMobileUploadStateMachine, createMobileUploadService, createMobileFileStateSubject } from '../utils/mobileUpload';
7
+ export var MobileUploadImpl = /*#__PURE__*/function () {
8
+ function MobileUploadImpl(mediaStore) {
9
+ _classCallCheck(this, MobileUploadImpl);
10
+
11
+ this.dataloader = createFileDataloader(mediaStore);
12
+ this.servicesCache = createServicesCache();
13
+ }
14
+
15
+ _createClass(MobileUploadImpl, [{
16
+ key: "notifyUploadStart",
17
+ value: function notifyUploadStart(event) {
18
+ var fileId = event.fileId,
19
+ collectionName = event.collectionName,
20
+ occurrenceKey = event.occurrenceKey,
21
+ fileName = event.fileName,
22
+ fileSize = event.fileSize,
23
+ fileMimetype = event.fileMimetype,
24
+ preview = event.preview,
25
+ createdAt = event.createdAt;
26
+ var mediaType = getMediaTypeFromMimeType(fileMimetype);
27
+ var initialState = {
28
+ status: 'uploading',
29
+ id: fileId,
30
+ occurrenceKey: occurrenceKey,
31
+ name: fileName,
32
+ size: fileSize,
33
+ progress: 0,
34
+ mediaType: mediaType,
35
+ mimeType: fileMimetype,
36
+ preview: preview,
37
+ createdAt: createdAt
38
+ };
39
+ var service = createMobileUploadService(createMobileUploadStateMachine(this.dataloader, initialState, collectionName));
40
+ var subject = createMobileFileStateSubject(service);
41
+ this.servicesCache.put(fileId, service);
42
+ getFileStreamsCache().set(fileId, subject);
43
+ }
44
+ }, {
45
+ key: "notifyUploadProgress",
46
+ value: function notifyUploadProgress(event) {
47
+ var fileId = event.fileId,
48
+ progress = event.progress;
49
+ var service = this.servicesCache.get(fileId);
50
+
51
+ if (service) {
52
+ service.send('UPLOAD_PROGRESS', {
53
+ progress: progress
54
+ });
55
+ }
56
+ }
57
+ }, {
58
+ key: "notifyUploadEnd",
59
+ value: function notifyUploadEnd(event) {
60
+ var fileId = event.fileId;
61
+ var service = this.servicesCache.get(fileId);
62
+
63
+ if (service) {
64
+ service.send('UPLOAD_END');
65
+ }
66
+ }
67
+ }, {
68
+ key: "notifyUploadError",
69
+ value: function notifyUploadError(event) {
70
+ var fileId = event.fileId,
71
+ message = event.message;
72
+ var service = this.servicesCache.get(fileId);
73
+
74
+ if (service) {
75
+ service.send('UPLOAD_ERROR', {
76
+ message: message
77
+ });
78
+ }
79
+ }
80
+ }]);
81
+
82
+ return MobileUploadImpl;
83
+ }();
package/dist/esm/index.js CHANGED
@@ -10,6 +10,7 @@ export { request, RequestError, isRequestError, isRateLimitedError } from './uti
10
10
  export { isAbortedRequestError, mapResponseToJson, mapResponseToBlob, mapResponseToVoid, createUrl } from './utils/request/helpers';
11
11
  export { PollingFunction } from './utils/polling';
12
12
  export { isPollingError, PollingError } from './utils/polling/errors';
13
+ export { imageResizeModeToFileImageMode } from './utils/imageResizeModeToFileImageMode';
13
14
  export { FileFetcherImpl, FileFetcherError, isFileFetcherError } from './client/file-fetcher';
14
15
  export { CollectionFetcher } from './client/collection-fetcher';
15
16
  export { MediaClient } from './client/media-client';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export var imageResizeModeToFileImageMode = function imageResizeModeToFileImageMode(resizeMode) {
2
+ return resizeMode === 'stretchy-fit' ? 'full-fit' : resizeMode;
3
+ };
@@ -0,0 +1,51 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _inherits from "@babel/runtime/helpers/inherits";
4
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
5
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
6
+
7
+ 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); }; }
8
+
9
+ 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; } }
10
+
11
+ import { BaseMediaClientError } from '../../models/errors';
12
+ export var MobileUploadError = /*#__PURE__*/function (_BaseMediaClientError) {
13
+ _inherits(MobileUploadError, _BaseMediaClientError);
14
+
15
+ var _super = _createSuper(MobileUploadError);
16
+
17
+ function MobileUploadError(reason, id, metadata) {
18
+ var _this;
19
+
20
+ _classCallCheck(this, MobileUploadError);
21
+
22
+ _this = _super.call(this, reason);
23
+ _this.reason = reason;
24
+ _this.id = id;
25
+ _this.metadata = metadata;
26
+ return _this;
27
+ }
28
+
29
+ _createClass(MobileUploadError, [{
30
+ key: "attributes",
31
+ get: function get() {
32
+ var reason = this.reason,
33
+ id = this.id,
34
+ _this$metadata = this.metadata;
35
+ _this$metadata = _this$metadata === void 0 ? {} : _this$metadata;
36
+ var collectionName = _this$metadata.collectionName,
37
+ occurrenceKey = _this$metadata.occurrenceKey;
38
+ return {
39
+ reason: reason,
40
+ id: id,
41
+ collectionName: collectionName,
42
+ occurrenceKey: occurrenceKey
43
+ };
44
+ }
45
+ }]);
46
+
47
+ return MobileUploadError;
48
+ }(BaseMediaClientError);
49
+ export function isMobileUploadError(err) {
50
+ return err instanceof MobileUploadError;
51
+ }
@@ -0,0 +1,86 @@
1
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
+ import { ReplaySubject } from 'rxjs/ReplaySubject';
4
+ import { from } from 'rxjs/observable/from';
5
+ import { map } from 'rxjs/operators/map';
6
+ import { mapMediaItemToFileState } from '../../models/file-state';
7
+ import { createFileStateSubject } from '../createFileStateSubject';
8
+ import { isEmptyFile } from '../detectEmptyFile';
9
+ import { PollingFunction } from '../polling';
10
+ import { MobileUploadError } from './error';
11
+ export var createMobileFileStateSubject = function createMobileFileStateSubject(service) {
12
+ var subject = new ReplaySubject(1);
13
+ from(service.start()).pipe(map(function (state) {
14
+ return state.context.currentFileState;
15
+ })).subscribe(subject);
16
+ return subject;
17
+ };
18
+ export var createMobileDownloadFileStream = function createMobileDownloadFileStream(dataloader, id, collectionName, occurrenceKey) {
19
+ var subject = createFileStateSubject();
20
+ var poll = new PollingFunction(); // ensure subject errors if polling exceeds max iterations or uncaught exception in executor
21
+
22
+ poll.onError = function (error) {
23
+ return subject.error(error);
24
+ };
25
+
26
+ poll.execute( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
27
+ var response, fileState;
28
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
29
+ while (1) {
30
+ switch (_context.prev = _context.next) {
31
+ case 0:
32
+ _context.next = 2;
33
+ return dataloader.load({
34
+ id: id,
35
+ collectionName: collectionName
36
+ });
37
+
38
+ case 2:
39
+ response = _context.sent;
40
+
41
+ if (response) {
42
+ _context.next = 5;
43
+ break;
44
+ }
45
+
46
+ throw new MobileUploadError('emptyItems', id, {
47
+ collectionName: collectionName,
48
+ occurrenceKey: occurrenceKey
49
+ });
50
+
51
+ case 5:
52
+ if (!isEmptyFile(response)) {
53
+ _context.next = 7;
54
+ break;
55
+ }
56
+
57
+ throw new MobileUploadError('zeroVersionFile', id, {
58
+ collectionName: collectionName,
59
+ occurrenceKey: occurrenceKey
60
+ });
61
+
62
+ case 7:
63
+ fileState = mapMediaItemToFileState(id, response);
64
+ subject.next(fileState);
65
+ _context.t0 = fileState.status;
66
+ _context.next = _context.t0 === 'processing' ? 12 : _context.t0 === 'processed' ? 14 : 16;
67
+ break;
68
+
69
+ case 12:
70
+ // the only case for continuing polling, otherwise this function is run once only
71
+ poll.next();
72
+ return _context.abrupt("break", 16);
73
+
74
+ case 14:
75
+ subject.complete();
76
+ return _context.abrupt("break", 16);
77
+
78
+ case 16:
79
+ case "end":
80
+ return _context.stop();
81
+ }
82
+ }
83
+ }, _callee);
84
+ })));
85
+ return subject;
86
+ };
@@ -0,0 +1,4 @@
1
+ export { isMobileUploadError, MobileUploadError } from './error';
2
+ export { createServicesCache } from './servicesCache';
3
+ export { createMobileUploadService, createMobileUploadStateMachine } from './stateMachine';
4
+ export { createMobileFileStateSubject } from './helpers';
@@ -0,0 +1,4 @@
1
+ import { LRUCache } from 'lru-fast';
2
+ export function createServicesCache() {
3
+ return new LRUCache(100);
4
+ }
@@ -0,0 +1,78 @@
1
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
+ import { map } from 'rxjs/operators/map';
4
+ import { createMachine, interpret } from 'xstate';
5
+ import { isProcessingFileState } from '../../../models/file-state';
6
+ import { shouldFetchRemoteFileStates as _shouldFetchRemoteFileStates } from '../../shouldFetchRemoteFileStates';
7
+ import { createMobileDownloadFileStream } from '../helpers';
8
+ import { machineUploadingState } from './states/uploading';
9
+ import { machineProcessingState } from './states/processing';
10
+ import { machineProcessedState } from './states/processed';
11
+ import { machineProcessingFailedState } from './states/processingFailed';
12
+ import { machineErrorState } from './states/error';
13
+ export var createMobileUploadStateMachine = function createMobileUploadStateMachine(dataloader, initialState, collectionName) {
14
+ return createMachine({
15
+ // Initial state
16
+ initial: initialState.status,
17
+ // Context
18
+ context: {
19
+ currentFileState: initialState
20
+ },
21
+ // State definitions
22
+ states: {
23
+ uploading: machineUploadingState,
24
+ processing: machineProcessingState,
25
+ processed: machineProcessedState,
26
+ processingFailed: machineProcessingFailedState,
27
+ error: machineErrorState
28
+ }
29
+ }, {
30
+ services: {
31
+ shouldFetchRemoteFileStates: function () {
32
+ var _shouldFetchRemoteFileStates2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(ctx) {
33
+ var currentFileState, mediaType, mimeType, preview;
34
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
35
+ while (1) {
36
+ switch (_context.prev = _context.next) {
37
+ case 0:
38
+ currentFileState = ctx.currentFileState;
39
+
40
+ if (!isProcessingFileState(currentFileState)) {
41
+ _context.next = 4;
42
+ break;
43
+ }
44
+
45
+ mediaType = currentFileState.mediaType, mimeType = currentFileState.mimeType, preview = currentFileState.preview;
46
+ return _context.abrupt("return", _shouldFetchRemoteFileStates(mediaType, mimeType, preview));
47
+
48
+ case 4:
49
+ return _context.abrupt("return", false);
50
+
51
+ case 5:
52
+ case "end":
53
+ return _context.stop();
54
+ }
55
+ }
56
+ }, _callee);
57
+ }));
58
+
59
+ function shouldFetchRemoteFileStates(_x) {
60
+ return _shouldFetchRemoteFileStates2.apply(this, arguments);
61
+ }
62
+
63
+ return shouldFetchRemoteFileStates;
64
+ }(),
65
+ fetchRemoteFileStates: function fetchRemoteFileStates(ctx) {
66
+ return createMobileDownloadFileStream(dataloader, ctx.currentFileState.id, collectionName, ctx.currentFileState.occurrenceKey).pipe(map(function (fileState) {
67
+ return {
68
+ type: 'REMOTE_FILESTATE_RESULT',
69
+ fileState: fileState
70
+ };
71
+ }));
72
+ }
73
+ }
74
+ });
75
+ };
76
+ export function createMobileUploadService(machine) {
77
+ return interpret(machine);
78
+ }
@@ -0,0 +1,3 @@
1
+ export var machineErrorState = {
2
+ type: 'final'
3
+ };
@@ -0,0 +1,3 @@
1
+ export var machineProcessedState = {
2
+ type: 'final'
3
+ };