Refactor Dish _translate to handle sync and async depending on environment.

This commit is contained in:
d98762625 2019-03-01 16:02:21 +00:00
parent 0a1ca18de5
commit b48c16b4db
16 changed files with 492 additions and 223 deletions

View file

@ -926,7 +926,11 @@ class Utils {
* await Utils.readFile(new File(["hello"], "test"))
*/
static readFile(file) {
if (Utils.isBrowser()) {
if (Utils.isNode()) {
return Buffer.from(file).buffer;
} else {
return new Promise((resolve, reject) => {
const reader = new FileReader();
const data = new Uint8Array(file.size);
@ -954,17 +958,12 @@ class Utils {
seek();
});
} else if (Utils.isNode()) {
return Buffer.from(file).buffer;
}
throw new Error("Unkown environment!");
}
/** */
static readFileSync(file) {
if (Utils.isBrowser()) {
if (!Utils.isNode()) {
throw new TypeError("Browser environment cannot support readFileSync");
}
@ -1065,13 +1064,6 @@ class Utils {
}[token];
}
/**
* Check if code is running in a browser environment
*/
static isBrowser() {
return typeof window !== "undefined" && typeof window.document !== "undefined";
}
/**
* Check if code is running in a Node environment
*/