-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from observerly/feat/utilities/readBlockAsArra…
…yBuffer feat: Added readBlockAsArrayBuffer() in utilities module.
- Loading branch information
Showing
4 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { getExcessByteSize } from './getExcessByteSize' | ||
|
||
export { readBlockAsArrayBuffer } from './readBlockAsArrayBuffer' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* | ||
* readBlockAsArrayBuffer() | ||
* | ||
* @description asynchronously reads a file slice as an ArrayBuffer | ||
* and onloadend will pass the ArrayBuffer result to a callback function. | ||
* | ||
* @param block represents a File or File.slice | ||
* @param callback function callback to be called onloadend passing | ||
* @returns a Promise which resolves the callback of the callback function provided | ||
*/ | ||
export const readBlockAsArrayBuffer = async ( | ||
block: Blob, | ||
callback: (result: ArrayBuffer) => { s: string; end: boolean; parsed: boolean } | ||
): Promise<{ s: string; end: boolean; parsed: boolean }> => { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader() | ||
|
||
reader.onloadend = (e: ProgressEvent<FileReader>) => { | ||
if (e.target?.result && e.target.result instanceof ArrayBuffer) { | ||
resolve(callback(e.target.result)) | ||
} | ||
|
||
resolve({ | ||
s: '', | ||
end: false, | ||
parsed: false | ||
}) | ||
} | ||
|
||
reader.onerror = reject | ||
|
||
reader.readAsArrayBuffer(block) | ||
}) | ||
} |