Skip to content

Commit

Permalink
Refactor code to readyCallback after libzim
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabhg71 committed Dec 7, 2023
1 parent 9fbbdb7 commit 579476b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 40 deletions.
54 changes: 15 additions & 39 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import uiUtil from './lib/uiUtil.js';
import settingsStore from './lib/settingsStore.js';
import abstractFilesystemAccess from './lib/abstractFilesystemAccess.js';
import translateUI from './lib/translateUI.js';
import { type } from 'jquery';

if (params.abort) {
// If the app was loaded only to pass a message from the remote code, then we exit immediately
Expand Down Expand Up @@ -916,44 +915,21 @@ async function handleMessageChannelByWasm (event) {
// The ServiceWorker asks for some content
const title = event.data.title;
const messagePort = event.ports[0];
const ret = await selectedArchive.getEntryDirByWasm(title);
if (ret === null) {
console.error('Title ' + title + ' not found in archive.');
messagePort.postMessage({ action: 'giveContent', title: title, content: '' });
return
}

// // Let's send the content to the ServiceWorker
var message = { action: 'giveContent', title: title, content: ret.content, mimetype: ret.mimetype };
// messagePort.postMessage(message, [ret.content]);
messagePort.postMessage(message);

// var readFile = function (dirEntry) {
// if (dirEntry === null) {
// console.error('Title ' + title + ' not found in archive.');
// messagePort.postMessage({ action: 'giveContent', title: title, content: '' });
// } else if (dirEntry.isRedirect()) {
// selectedArchive.resolveRedirect(dirEntry, function (resolvedDirEntry) {
// var redirectURL = resolvedDirEntry.namespace + '/' + resolvedDirEntry.url;
// // Ask the ServiceWorker to send an HTTP redirect to the browser.
// // We could send the final content directly, but it is necessary to let the browser know in which directory it ends up.
// // Else, if the redirect URL is in a different directory than the original URL,
// // the relative links in the HTML content would fail. See #312
// messagePort.postMessage({ action: 'sendRedirect', title: title, redirectUrl: redirectURL });
// });
// } else {
// // Let's read the content in the ZIM file
// selectedArchive.readBinaryFile(dirEntry, function (fileDirEntry, content) {
// var mimetype = fileDirEntry.getMimetype();
// // Let's send the content to the ServiceWorker
// var message = { action: 'giveContent', title: title, content: content.buffer, mimetype: mimetype };
// messagePort.postMessage(message, [content.buffer]);
// });
// }
// };
// selectedArchive.getDirEntryByPath(title).then(readFile).catch(function () {
// messagePort.postMessage({ action: 'giveContent', title: title, content: new Uint8Array() });
// });
try {
const ret = await selectedArchive.getEntryDirByWasm(title);
if (ret === null) {
console.error('Title ' + title + ' not found in archive.');
messagePort.postMessage({ action: 'giveContent', title: title, content: '' });
return
}

// Let's send the content to the ServiceWorker
const message = { action: 'giveContent', title: title, content: ret.content, mimetype: ret.mimetype };
messagePort.postMessage(message);
} catch (error) {
const message = { action: 'giveContent', title: title, content: new Uint8Array(), mimetype: '' };
messagePort.postMessage(message);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions www/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ params['cacheIDB'] = 'kiwix-zim'; // Sets name of the Indexed DB database
params['isFileSystemApiSupported'] = typeof window.showOpenFilePicker === 'function'; // Sets a boolean indicating whether the FileSystem API is supported
params['isWebkitDirApiSupported'] = 'webkitdirectory' in document.createElement('input'); // Sets a boolean indicating whether the Webkit Directory API is supported
params['libzimMode'] = getSetting('libzimMode') ?? 'default'; // Sets a value indicating which libzim mode is selected
params['useLibzim'] = true; // Sets a value indicating which libzim mode is selected

/**
* Apply any override parameters that might be in the querystring.
Expand Down
6 changes: 5 additions & 1 deletion www/js/lib/zimArchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ function ZIMArchive (storage, path, callbackReady, callbackError) {
if (that.file.fullTextIndex && (params.debugLibzimASM || !isSplitZim && typeof Atomics !== 'undefined' &&
// Note that Android and NWJS currently throw due to problems with Web Worker context
!/Android/.test(params.appType) && !(window.nw && that.file._files[0].readMode === 'electron'))) {
var libzimReaderType = params.debugLibzimASM || ('WebAssembly' in self ? 'wasm' : 'asm');
// var libzimReaderType = params.debugLibzimASM || ('WebAssembly' in self ? 'wasm' : 'asm');
var libzimReaderType = params.debugLibzimASM || ('WebAssembly' in self ? 'wasm.dev' : 'asm.dev');

console.log('Instantiating libzim ' + libzimReaderType + ' Web Worker...');
LZ = new Worker('js/lib/libzim-' + libzimReaderType + '.js');
that.callLibzimWorker({ action: 'init', files: that.file._files }).then(function () {
that.libzimReady = 'ready';
params.searchProvider = 'fulltext: ' + libzimReaderType;
if (params.useLibzim) whenZimReady();

// Update the API panel
uiUtil.reportSearchProviderToAPIStatusPanel(params.searchProvider);
}).catch(function (err) {
Expand Down

0 comments on commit 579476b

Please sign in to comment.