mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-03-02 12:00:45 -08:00
refactor: use worker_thread to start wiki
This commit is contained in:
parent
e89b904c85
commit
63041ef460
5 changed files with 50 additions and 22 deletions
|
|
@ -10,7 +10,7 @@ const path = require('path');
|
|||
const fsExtra = require('fs-extra');
|
||||
const { ElectronBlocker } = require('@cliqz/adblocker-electron');
|
||||
|
||||
const startNodeJSWiki = require('./start-nodejs-wiki')
|
||||
const startNodeJSWiki = require('./wiki/start-nodejs-wiki')
|
||||
const { getPreferences } = require('./preferences');
|
||||
const {
|
||||
getWorkspace,
|
||||
|
|
|
|||
|
|
@ -1,18 +1,14 @@
|
|||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const { dialog } = require('electron');
|
||||
const $tw = require('tiddlywiki/boot/boot.js').TiddlyWiki();
|
||||
|
||||
const mainWindow = require('../windows/main');
|
||||
const { startWiki } = require('./wiki-worker-mamager');
|
||||
const mainWindow = require('../../windows/main');
|
||||
|
||||
const tiddlyWikiPort = 5112;
|
||||
const userName = 'LinOnetwoTest';
|
||||
|
||||
module.exports = function startNodeJSWiki(homePath) {
|
||||
if ($tw.wiki) {
|
||||
console.error('Wiki has already started');
|
||||
return;
|
||||
}
|
||||
if (!homePath || typeof homePath !== 'string' || !path.isAbsolute(homePath)) {
|
||||
const errorMessage = `需要传入正确的路径,而 ${homePath} 无法被 TiddlyWiki 识别。`;
|
||||
console.error(errorMessage);
|
||||
|
|
@ -38,18 +34,5 @@ module.exports = function startNodeJSWiki(homePath) {
|
|||
return;
|
||||
}
|
||||
|
||||
process.env.TIDDLYWIKI_PLUGIN_PATH = path.resolve(homePath, 'plugins');
|
||||
process.env.TIDDLYWIKI_THEME_PATH = path.resolve(homePath, 'themes');
|
||||
// add tiddly filesystem back https://github.com/Jermolene/TiddlyWiki5/issues/4484#issuecomment-596779416
|
||||
$tw.boot.argv = [
|
||||
'+plugins/tiddlywiki/filesystem',
|
||||
'+plugins/tiddlywiki/tiddlyweb',
|
||||
homePath,
|
||||
'--listen',
|
||||
`anon-username=${userName}`,
|
||||
`port=${tiddlyWikiPort}`,
|
||||
'host=0.0.0.0',
|
||||
'root-tiddler=$:/core/save/lazy-images',
|
||||
];
|
||||
$tw.boot.boot();
|
||||
startWiki(homePath, userName, tiddlyWikiPort);
|
||||
};
|
||||
17
public/libs/wiki/wiki-worker-mamager.js
Normal file
17
public/libs/wiki/wiki-worker-mamager.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* eslint-disable no-console */
|
||||
const { Worker } = require('worker_threads');
|
||||
|
||||
// { [name: string]: Worker }
|
||||
const workers = {};
|
||||
|
||||
module.exports.startWiki = function startWiki(homePath, userName, tiddlyWikiPort) {
|
||||
const workerData = { homePath, userName, tiddlyWikiPort };
|
||||
console.warn(`workerData`, JSON.stringify(workerData, null, ' '));
|
||||
const worker = new Worker(require.resolve('./wiki-worker.js'), { workerData });
|
||||
workers[homePath] = worker;
|
||||
worker.on('message', message => console.log(`[${homePath}] ${message}`));
|
||||
worker.on('error', error => console.error(`[${homePath}] ${error}`));
|
||||
worker.on('exit', code => {
|
||||
if (code !== 0) console.error(`[${homePath}] Worker stopped with exit code ${code}`);
|
||||
});
|
||||
};
|
||||
28
public/libs/wiki/wiki-worker.js
Normal file
28
public/libs/wiki/wiki-worker.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
delete require.cache[require.resolve('tiddlywiki/boot/boot.js')];
|
||||
const { workerData, parentPort, isMainThread } = require('worker_threads');
|
||||
const path = require('path');
|
||||
const $tw = require('tiddlywiki/boot/boot.js').TiddlyWiki();
|
||||
|
||||
function startNodeJSWiki() {
|
||||
const { homePath, userName, tiddlyWikiPort = 5112 } = workerData;
|
||||
process.env.TIDDLYWIKI_PLUGIN_PATH = path.resolve(homePath, 'plugins');
|
||||
process.env.TIDDLYWIKI_THEME_PATH = path.resolve(homePath, 'themes');
|
||||
// add tiddly filesystem back https://github.com/Jermolene/TiddlyWiki5/issues/4484#issuecomment-596779416
|
||||
$tw.boot.argv = [
|
||||
'+plugins/tiddlywiki/filesystem',
|
||||
'+plugins/tiddlywiki/tiddlyweb',
|
||||
homePath,
|
||||
'--listen',
|
||||
`anon-username=${userName}`,
|
||||
`port=${tiddlyWikiPort}`,
|
||||
'host=0.0.0.0',
|
||||
'root-tiddler=$:/core/save/lazy-images',
|
||||
];
|
||||
$tw.boot.boot();
|
||||
parentPort.postMessage(`Tiddlywiki booted at http://localhost:${tiddlyWikiPort}`);
|
||||
}
|
||||
module.exports = startNodeJSWiki;
|
||||
|
||||
if (!isMainThread) {
|
||||
startNodeJSWiki();
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
const { BrowserView, Notification, app, dialog, ipcMain, nativeTheme, shell } = require('electron');
|
||||
|
||||
const { createWiki, createSubWiki } = require('../libs/create-wiki');
|
||||
const startNodeJSWiki = require('../libs/start-nodejs-wiki');
|
||||
const startNodeJSWiki = require('../libs/wiki/start-nodejs-wiki');
|
||||
const { getIconPath } = require('../libs/get-constants');
|
||||
|
||||
const { getPreference, getPreferences, resetPreferences, setPreference } = require('../libs/preferences');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue