From 172b3cab19f9dfbf5a56bfa73add8aacf407072c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Wed, 21 Aug 2019 14:53:20 +0200 Subject: [PATCH 1/7] Expose loaded data as FileLoader.data. --- src/filereader.js | 12 +++++++++++- src/filerepository.js | 8 +++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/filereader.js b/src/filereader.js index 656ded2..1b4f95f 100644 --- a/src/filereader.js +++ b/src/filereader.js @@ -30,6 +30,8 @@ export default class FileReader { */ this._reader = reader; + this._data = undefined; + /** * Number of bytes loaded. * @@ -53,6 +55,10 @@ export default class FileReader { return this._reader.error; } + get data() { + return this._data; + } + /** * Reads the provided file. * @@ -66,7 +72,11 @@ export default class FileReader { return new Promise( ( resolve, reject ) => { reader.onload = () => { - resolve( reader.result ); + const result = reader.result; + + this._data = result; + + resolve( result ); }; reader.onerror = () => { diff --git a/src/filerepository.js b/src/filerepository.js index 8207530..91004b1 100644 --- a/src/filerepository.js +++ b/src/filerepository.js @@ -400,6 +400,10 @@ class FileLoader { } } + get data() { + return this._reader.data; + } + /** * Reads file using {@link module:upload/filereader~FileReader}. * @@ -433,6 +437,8 @@ class FileLoader { .then( data => { this.status = 'idle'; + this._data = data; + return data; } ) .catch( err => { @@ -521,7 +527,7 @@ class FileLoader { this._filePromiseWrapper = undefined; this._reader = undefined; this._adapter = undefined; - this.data = undefined; + this._data = undefined; this.uploadResponse = undefined; } From 7322d9a15756511bf5cbc9cc6a5b560841feaee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Wed, 21 Aug 2019 15:33:25 +0200 Subject: [PATCH 2/7] Update documentation. --- src/filereader.js | 5 +++++ src/filerepository.js | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/filereader.js b/src/filereader.js index 1b4f95f..c8221af 100644 --- a/src/filereader.js +++ b/src/filereader.js @@ -55,6 +55,11 @@ export default class FileReader { return this._reader.error; } + /** + * Holds data of already loaded file. The file must be first loaded using {@link module:upload/filereader~FileReader#read}. + * + * @type {File|undefined} + */ get data() { return this._data; } diff --git a/src/filerepository.js b/src/filerepository.js index 91004b1..7de0486 100644 --- a/src/filerepository.js +++ b/src/filerepository.js @@ -400,6 +400,11 @@ class FileLoader { } } + /** + * Returns file data of a file that was already read. To read the file use {@link module:upload/filerepository~FileLoader#read} method. + * + * @type {File|undefined} + */ get data() { return this._reader.data; } @@ -437,8 +442,6 @@ class FileLoader { .then( data => { this.status = 'idle'; - this._data = data; - return data; } ) .catch( err => { @@ -527,7 +530,6 @@ class FileLoader { this._filePromiseWrapper = undefined; this._reader = undefined; this._adapter = undefined; - this._data = undefined; this.uploadResponse = undefined; } From 42aab0744f85b30e36f200a4a428d1c97219d54a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Wed, 21 Aug 2019 15:43:46 +0200 Subject: [PATCH 3/7] Add tests for data getter. --- tests/filereader.js | 21 +++++++++++++++++++-- tests/filerepository.js | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tests/filereader.js b/tests/filereader.js index 2d861f2..59318ab 100644 --- a/tests/filereader.js +++ b/tests/filereader.js @@ -37,7 +37,24 @@ describe( 'FileReader', () => { expect( reader.loaded ).to.equal( 55 ); } ); - describe( 'read', () => { + describe( 'data', () => { + it( 'should be undefined if file was not loaded', () => { + expect( reader.data ).to.be.undefined; + } ); + + it( 'should equal to loaded file data', () => { + const promise = reader.read( fileMock ) + .then( () => { + expect( reader.data ).to.equal( 'File contents.' ); + } ); + + nativeReaderMock.mockSuccess( 'File contents.' ); + + return promise; + } ); + } ); + + describe( 'read()', () => { it( 'should return a promise', () => { expect( reader.read( fileMock ) ).to.be.instanceOf( Promise ); } ); @@ -81,7 +98,7 @@ describe( 'FileReader', () => { } ); } ); - describe( 'abort', () => { + describe( 'abort()', () => { it( 'should allow to abort reading', () => { const promise = reader.read( fileMock ) .then( () => { diff --git a/tests/filerepository.js b/tests/filerepository.js index 3f248f4..3c00be6 100644 --- a/tests/filerepository.js +++ b/tests/filerepository.js @@ -452,6 +452,33 @@ describe( 'FileRepository', () => { } ); } ); + describe( 'data getter', () => { + it( 'should be undefined if no file loaded', () => { + expect( loader.data ).to.be.undefined; + } ); + + it( 'should return promise which resolves to a file', () => { + let resolveFile = null; + + const filePromise = new Promise( resolve => { + resolveFile = resolve; + } ); + + const loader = fileRepository.createLoader( filePromise ); + + const promise = loader.read() + .then( () => { + expect( loader.data ).to.equal( 'result data' ); + } ); + + resolveFile( createNativeFileMock() ); + + loader.file.then( () => nativeReaderMock.mockSuccess( 'result data' ) ); + + return promise; + } ); + } ); + describe( 'read()', () => { it( 'should throw error when status is different than idle', () => { loader.status = 'uploading'; From 64098d3d3982bece33db388baf1167640c070429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Thu, 22 Aug 2019 09:35:39 +0200 Subject: [PATCH 4/7] Wording. --- src/filereader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filereader.js b/src/filereader.js index c8221af..5fa728b 100644 --- a/src/filereader.js +++ b/src/filereader.js @@ -56,7 +56,7 @@ export default class FileReader { } /** - * Holds data of already loaded file. The file must be first loaded using {@link module:upload/filereader~FileReader#read}. + * Holds the data of an already loaded file. The file must be first loaded by using {@link module:upload/filereader~FileReader#read `read()`}. * * @type {File|undefined} */ From 9b4bd6020f8c4c1777f9845cf5a8bd0b093a3a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Thu, 22 Aug 2019 09:39:35 +0200 Subject: [PATCH 5/7] Wording. --- src/filerepository.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filerepository.js b/src/filerepository.js index 7de0486..7c3ac5b 100644 --- a/src/filerepository.js +++ b/src/filerepository.js @@ -401,7 +401,7 @@ class FileLoader { } /** - * Returns file data of a file that was already read. To read the file use {@link module:upload/filerepository~FileLoader#read} method. + * Returns the file data. To read its data, you need for first load the file by using the {@link module:upload/filerepository~FileLoader#read `read()`} method. * * @type {File|undefined} */ From ae200250b2c9417a1f39bb4f94cb8adddeb3c74f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Thu, 22 Aug 2019 10:21:06 +0200 Subject: [PATCH 6/7] Fix line length. --- src/filereader.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/filereader.js b/src/filereader.js index 5fa728b..4b81558 100644 --- a/src/filereader.js +++ b/src/filereader.js @@ -56,7 +56,8 @@ export default class FileReader { } /** - * Holds the data of an already loaded file. The file must be first loaded by using {@link module:upload/filereader~FileReader#read `read()`}. + * Holds the data of an already loaded file. The file must be first loaded + * by using {@link module:upload/filereader~FileReader#read `read()`}. * * @type {File|undefined} */ From 94128d7eb19b4425260804a8984b92e950025c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Thu, 22 Aug 2019 10:22:11 +0200 Subject: [PATCH 7/7] Fix line length. --- src/filerepository.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/filerepository.js b/src/filerepository.js index 7c3ac5b..1ba8014 100644 --- a/src/filerepository.js +++ b/src/filerepository.js @@ -401,7 +401,8 @@ class FileLoader { } /** - * Returns the file data. To read its data, you need for first load the file by using the {@link module:upload/filerepository~FileLoader#read `read()`} method. + * Returns the file data. To read its data, you need for first load the file + * by using the {@link module:upload/filerepository~FileLoader#read `read()`} method. * * @type {File|undefined} */