diff options
| author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-07-31 18:08:51 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-31 18:08:51 -0400 | 
| commit | 01c5c5c04bd9234c64d6f57ee0ea65b0f478a1b7 (patch) | |
| tree | f262b7042bd56fc9b72efb7b42d97341efd50df9 /ext/js/language | |
| parent | 1d6e437fb284a591fc1b7f4aec87bbc7cde5367f (diff) | |
Dictionary importer refactoring (#1867)
* Remove map of schemas
* Don't reuse dictionary importer instances
* Refactor
* Update how progress callback is used
Diffstat (limited to 'ext/js/language')
| -rw-r--r-- | ext/js/language/dictionary-importer-threaded.js | 34 | ||||
| -rw-r--r-- | ext/js/language/dictionary-importer-worker.js | 4 | ||||
| -rw-r--r-- | ext/js/language/dictionary-importer.js | 21 | 
3 files changed, 26 insertions, 33 deletions
| diff --git a/ext/js/language/dictionary-importer-threaded.js b/ext/js/language/dictionary-importer-threaded.js index a251906b..c9b6fb24 100644 --- a/ext/js/language/dictionary-importer-threaded.js +++ b/ext/js/language/dictionary-importer-threaded.js @@ -20,7 +20,11 @@   */  class DictionaryImporterThreaded { -    importDictionary(archiveContent, details, onProgress) { +    constructor(onProgress) { +        this._onProgress = onProgress; +    } + +    importDictionary(archiveContent, details) {          return new Promise((resolve, reject) => {              const dictionaryImporterMediaLoader = new DictionaryImporterMediaLoader();              const worker = new Worker('/js/language/dictionary-importer-worker-main.js', {}); @@ -30,13 +34,13 @@ class DictionaryImporterThreaded {                      case 'complete':                          worker.removeEventListener('message', onMessage);                          worker.terminate(); -                        this._onComplete(params, resolve, reject); +                        this._onMessageComplete(params, resolve, reject);                          break;                      case 'progress': -                        this._onProgress(params, onProgress); +                        this._onMessageProgress(params);                          break;                      case 'getImageResolution': -                        this._onGetImageResolution(params, worker, dictionaryImporterMediaLoader); +                        this._onMessageGetImageResolution(params, worker, dictionaryImporterMediaLoader);                          break;                  }              }; @@ -50,7 +54,7 @@ class DictionaryImporterThreaded {      // Private -    _onComplete(params, resolve, reject) { +    _onMessageComplete(params, resolve, reject) {          const {error} = params;          if (typeof error !== 'undefined') {              reject(deserializeError(error)); @@ -59,19 +63,13 @@ class DictionaryImporterThreaded {          }      } -    _formatResult(data) { -        const {result, errors} = data; -        const errors2 = errors.map((error) => deserializeError(error)); -        return {result, errors: errors2}; -    } - -    _onProgress(params, onProgress) { -        if (typeof onProgress !== 'function') { return; } +    _onMessageProgress(params) { +        if (typeof this._onProgress !== 'function') { return; }          const {args} = params; -        onProgress(...args); +        this._onProgress(...args);      } -    async _onGetImageResolution(params, worker, dictionaryImporterMediaLoader) { +    async _onMessageGetImageResolution(params, worker, dictionaryImporterMediaLoader) {          const {id, mediaType, content} = params;          let response;          try { @@ -82,4 +80,10 @@ class DictionaryImporterThreaded {          }          worker.postMessage({action: 'getImageResolution.response', params: response});      } + +    _formatResult(data) { +        const {result, errors} = data; +        const errors2 = errors.map((error) => deserializeError(error)); +        return {result, errors: errors2}; +    }  } diff --git a/ext/js/language/dictionary-importer-worker.js b/ext/js/language/dictionary-importer-worker.js index f44a10f9..014b6542 100644 --- a/ext/js/language/dictionary-importer-worker.js +++ b/ext/js/language/dictionary-importer-worker.js @@ -64,8 +64,8 @@ class DictionaryImporterWorker {      async _importDictionary(archiveContent, importDetails, onProgress) {          const dictionaryDatabase = await this._getPreparedDictionaryDatabase();          try { -            const dictionaryImporter = new DictionaryImporter(this._mediaLoader); -            const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, importDetails, onProgress); +            const dictionaryImporter = new DictionaryImporter(this._mediaLoader, onProgress); +            const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, importDetails);              return {                  result,                  errors: errors.map((error) => serializeError(error)) diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index 962758c3..f3e86654 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -22,12 +22,12 @@   */  class DictionaryImporter { -    constructor(mediaLoader) { +    constructor(mediaLoader, onProgress) {          this._mediaLoader = mediaLoader; -        this._schemas = new Map(); +        this._onProgress = onProgress;      } -    async importDictionary(dictionaryDatabase, archiveContent, details, onProgress) { +    async importDictionary(dictionaryDatabase, archiveContent, details) {          if (!dictionaryDatabase) {              throw new Error('Invalid database');          } @@ -35,7 +35,7 @@ class DictionaryImporter {              throw new Error('Database is not ready');          } -        const hasOnProgress = (typeof onProgress === 'function'); +        const hasOnProgress = (typeof this._onProgress === 'function');          // Read archive          const archive = await JSZip.loadAsync(archiveContent); @@ -143,7 +143,7 @@ class DictionaryImporter {                  loadedCount += count;                  if (hasOnProgress) { -                    onProgress(total, loadedCount); +                    this._onProgress(total, loadedCount);                  }              }          }; @@ -178,17 +178,6 @@ class DictionaryImporter {      }      async _getSchema(fileName) { -        let schemaPromise = this._schemas.get(fileName); -        if (typeof schemaPromise !== 'undefined') { -            return schemaPromise; -        } - -        schemaPromise = this._createSchema(fileName); -        this._schemas.set(fileName, schemaPromise); -        return schemaPromise; -    } - -    async _createSchema(fileName) {          const schema = await this._fetchJsonAsset(fileName);          return new JsonSchema(schema);      } |