mirror of
https://github.com/retorquere/zotero-better-bibtex.git
synced 2022-05-23 09:36:29 +03:00
simplify background worker #norelease
This commit is contained in:
@@ -112,7 +112,7 @@ class AutoExportPane {
|
||||
break
|
||||
|
||||
case 'status':
|
||||
if (ae.status === 'running' && Preference.workers && typeof progress === 'number') {
|
||||
if (ae.status === 'running' && Preference.worker && typeof progress === 'number') {
|
||||
(node as XUL.Textbox).value = progress < 0 ? `${this.label?.preparing || 'preparing'} ${-progress}%` : `${progress}%`
|
||||
}
|
||||
else {
|
||||
@@ -541,12 +541,12 @@ export class PrefPane {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||
for (const state of (Array.from(this.globals.document.getElementsByClassName('better-bibtex-preferences-worker-state')) as XUL.Textbox[])) {
|
||||
state.value = l10n.localize(`BetterBibTeX.workers.${Preference.workers ? 'status' : 'disabled'}`, {
|
||||
state.value = l10n.localize(`BetterBibTeX.workers.${Preference.worker ? 'status' : 'disabled'}`, {
|
||||
total: Translators.workers.total,
|
||||
workers: Preference.workers,
|
||||
workers: Preference.worker,
|
||||
running: Translators.workers.running.size,
|
||||
})
|
||||
state.classList[Preference.workers ? 'remove' : 'add']('textbox-emph')
|
||||
state.classList[Preference.worker ? 'remove' : 'add']('textbox-emph')
|
||||
}
|
||||
|
||||
if (this.autoexport) this.autoexport.refresh()
|
||||
|
||||
@@ -29,8 +29,6 @@ groupbox
|
||||
checkbox(preference="pref-better-bibtex-retainCache" label="&better-bibtex.Preferences.advanced.export.retainCache;")
|
||||
image(tooltip="tooltip-retainCache" src="chrome://zotero-better-bibtex/skin/attention.svg" width="16" height="16")
|
||||
hbox
|
||||
label(bbt:preference="pref-better-bibtex-workers") &better-bibtex.Preferences.advanced.export.workers;
|
||||
scale(min="1" max="10" preference="pref-better-bibtex-workers")
|
||||
checkbox(preference="pref-better-bibtex-cache" label="&better-bibtex.Preferences.advanced.export.workers.cache;")
|
||||
label.better-bibtex-preferences-worker-state
|
||||
hbox
|
||||
|
||||
@@ -295,20 +295,16 @@ bbt:doc.
|
||||
Any characters entered here will prefer a math-mode LaTeX-command counterpart over a math-mode,
|
||||
if a math-mode command is available. Only useful when `mapUnicode` is `conservative`.
|
||||
|
||||
preference#pref-better-bibtex-workers(name="extensions.zotero.translators.better-bibtex.workers" bbt:affects="" type="int" default="1")
|
||||
preference(name="extensions.zotero.translators.better-bibtex.worker" bbt:affects="" type="bool" default="true")
|
||||
bbt:doc.
|
||||
BBT can now perform its exports in a separate thread, and
|
||||
should no longer block Zotero's UI pretty much regardless
|
||||
of how large your library is. The default of 1 parallel
|
||||
export should suit most needs, but if you have many
|
||||
auto-exports set up, you may want to raise the maximum
|
||||
parallel exports to prevent queueing of exports. It is
|
||||
possible to turn background exports off by setting this
|
||||
value to `0` in the hidden preferences; you will get the
|
||||
old (blocking) behavior back, but you can't complain about
|
||||
Zotero being laggy during auto-exports. All Zotero exports
|
||||
are blocking, and it's a minor miracle I got background
|
||||
exports to work at all.
|
||||
BBT can perform its exports in a separate thread, and should no
|
||||
longer block Zotero's UI pretty much regardless of how large your
|
||||
library is. It is possible to turn background exports off by
|
||||
setting this value to `false` in the hidden preferences; you will get
|
||||
the old (blocking) behavior back, but you can't complain about
|
||||
Zotero being laggy during auto-exports; all Zotero exports are
|
||||
blocking, and it's a minor miracle I got background exports to
|
||||
work at all.
|
||||
|
||||
preference#pref-better-bibtex-cache(name="extensions.zotero.translators.better-bibtex.cache" bbt:affects="*" type="bool" default="true")
|
||||
bbt:doc.
|
||||
|
||||
@@ -575,7 +575,7 @@ $patch$(Zotero.Translate.Export.prototype, 'translate', original => function Zot
|
||||
if (this.noWait) { // noWait must be synchronous
|
||||
disabled = 'noWait is active'
|
||||
}
|
||||
else if (!Preference.workers) {
|
||||
else if (!Preference.worker) {
|
||||
disabled = 'user has disabled worker export'
|
||||
}
|
||||
else if (Translators.workers.disabled) {
|
||||
@@ -598,7 +598,7 @@ $patch$(Zotero.Translate.Export.prototype, 'translate', original => function Zot
|
||||
this.saveQueue = []
|
||||
this._savingAttachments = []
|
||||
|
||||
return Translators.exportItemsByQueuedWorker(translatorID, displayOptions, { translate: this, scope: { ...this._export, getter: this._itemGetter }, path })
|
||||
return Translators.exportItemsByWorker(translatorID, displayOptions, { translate: this, scope: { ...this._export, getter: this._itemGetter }, path })
|
||||
.then(result => {
|
||||
// eslint-disable-next-line id-blacklist
|
||||
this.string = result
|
||||
|
||||
@@ -100,11 +100,12 @@ export const Preference = new class PreferenceManager extends PreferenceManagerB
|
||||
Zotero.Prefs.set(key, 'selectlink')
|
||||
}
|
||||
if (typeof (old = Zotero.Prefs.get(key = 'translators.better-bibtex.workers')) !== 'undefined') {
|
||||
Zotero.Prefs.rootBranch.setIntPref('extensions.zotero.translators.better-bibtex.workers', typeof old === 'number' ? old : 1)
|
||||
Zotero.Prefs.clear(key)
|
||||
Zotero.Prefs.set('translators.better-bibtex.worker', !!old)
|
||||
}
|
||||
if (typeof (old = Zotero.Prefs.get(key = 'translators.better-bibtex.workersMax')) !== 'undefined') {
|
||||
Zotero.Prefs.clear(key)
|
||||
Zotero.Prefs.set('translators.better-bibtex.workers', typeof old === 'number' ? old : 1)
|
||||
Zotero.Prefs.set('translators.better-bibtex.worker', !!old)
|
||||
}
|
||||
if (typeof (old = Zotero.Prefs.get(key = 'translators.better-bibtex.workersCache')) !== 'undefined') {
|
||||
Zotero.Prefs.clear(key)
|
||||
|
||||
@@ -16,7 +16,7 @@ import { Preference } from './prefs'
|
||||
import * as memory from './memory'
|
||||
import { Deferred } from './deferred'
|
||||
|
||||
const setatstart: string[] = ['workers', 'testing', 'cache'].filter(p => Preference[p] !== defaults[p])
|
||||
const setatstart: string[] = ['worker', 'testing', 'cache'].filter(p => Preference[p] !== defaults[p])
|
||||
|
||||
export class TestSupport {
|
||||
public timedMemoryLog: any
|
||||
|
||||
@@ -22,15 +22,8 @@ import { Pinger } from './ping'
|
||||
|
||||
import * as translatorMetadata from '../gen/translators.json'
|
||||
|
||||
import { TaskEasy } from 'task-easy'
|
||||
|
||||
import * as l10n from './l10n'
|
||||
|
||||
interface Priority {
|
||||
priority: number
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
type ExportScope = { type: 'items', items: any[] } | { type: 'library', id: number } | { type: 'collection', collection: any }
|
||||
type ExportJob = {
|
||||
scope?: ExportScope
|
||||
@@ -41,29 +34,6 @@ type ExportJob = {
|
||||
translate: any
|
||||
}
|
||||
|
||||
class Queue {
|
||||
private queue: TaskEasy<Priority>
|
||||
|
||||
constructor() {
|
||||
this.queue = new TaskEasy((t1: Priority, t2: Priority) => t1.priority === t2.priority ? t1.timestamp < t2.timestamp : t1.priority > t2.priority)
|
||||
}
|
||||
|
||||
public async schedule(task: TaskEasy.Task<string>, translatorID: string, displayOptions: Record<string, boolean>, job: ExportJob) {
|
||||
job.started = Date.now()
|
||||
if (job.path) {
|
||||
for (const scheduled of (this.queue as any).tasks) {
|
||||
if (scheduled.started < job.started && scheduled.args && scheduled.args.length === 3) { // eslint-disable-line no-magic-numbers
|
||||
const scheduledJob = (scheduled.args[2] as ExportJob)
|
||||
if (scheduledJob.path && scheduledJob.path === job.path) {
|
||||
scheduledJob.canceled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return await this.queue.schedule(task, [translatorID, displayOptions, job], { priority: 1, timestamp: job.started })
|
||||
}
|
||||
}
|
||||
|
||||
// export singleton: https://k94n.com/es6-modules-single-instance-pattern
|
||||
export const Translators = new class { // eslint-disable-line @typescript-eslint/naming-convention,no-underscore-dangle,id-blacklist,id-match
|
||||
public byId: Record<string, Translator.Header>
|
||||
@@ -71,8 +41,6 @@ export const Translators = new class { // eslint-disable-line @typescript-eslint
|
||||
public byLabel: Record<string, Translator.Header>
|
||||
public itemType: { note: number, attachment: number, annotation: number }
|
||||
|
||||
private queue = new Queue
|
||||
|
||||
public workers: { total: number, running: Set<string>, disabled: boolean, startup: number } = {
|
||||
total: 0,
|
||||
running: new Set,
|
||||
@@ -164,15 +132,6 @@ export const Translators = new class { // eslint-disable-line @typescript-eslint
|
||||
return translation.newItems
|
||||
}
|
||||
|
||||
public async exportItemsByQueuedWorker(translatorID: string, displayOptions: Record<string, boolean>, job: ExportJob) {
|
||||
if (this.workers.running.size > Preference.workers) {
|
||||
return this.queue.schedule(this.exportItemsByWorker.bind(this), translatorID, displayOptions, job)
|
||||
}
|
||||
else {
|
||||
return this.exportItemsByWorker(translatorID, displayOptions, job)
|
||||
}
|
||||
}
|
||||
|
||||
public async exportItemsByWorker(translatorID: string, displayOptions: Record<string, boolean>, job: ExportJob) {
|
||||
if (job.path && job.canceled) return ''
|
||||
|
||||
|
||||
13
package-lock.json
generated
13
package-lock.json
generated
@@ -100,7 +100,6 @@
|
||||
"shelljs": "^0.8.5",
|
||||
"showdown": "^2.1.0",
|
||||
"string-template": "^1.0.0",
|
||||
"task-easy": "^1.0.1",
|
||||
"ts-node": "^10.7.0",
|
||||
"tslib": "^2.4.0",
|
||||
"typescript": "^4.6.4",
|
||||
@@ -14664,12 +14663,6 @@
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/task-easy": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/task-easy/-/task-easy-1.0.1.tgz",
|
||||
"integrity": "sha512-3WIgb6s9fR1zGgdxOnfLwT3bARk5HlKQDAAtow8igg+r08oa6EIZM+Ovof8ZNjkK/E6T5kA3rDcqApz9DdSJDQ==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/text-table": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz",
|
||||
@@ -26561,12 +26554,6 @@
|
||||
"readable-stream": "^3.1.1"
|
||||
}
|
||||
},
|
||||
"task-easy": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/task-easy/-/task-easy-1.0.1.tgz",
|
||||
"integrity": "sha512-3WIgb6s9fR1zGgdxOnfLwT3bARk5HlKQDAAtow8igg+r08oa6EIZM+Ovof8ZNjkK/E6T5kA3rDcqApz9DdSJDQ==",
|
||||
"dev": true
|
||||
},
|
||||
"text-table": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz",
|
||||
|
||||
@@ -148,7 +148,6 @@
|
||||
"shelljs": "^0.8.5",
|
||||
"showdown": "^2.1.0",
|
||||
"string-template": "^1.0.0",
|
||||
"task-easy": "^1.0.1",
|
||||
"ts-node": "^10.7.0",
|
||||
"tslib": "^2.4.0",
|
||||
"typescript": "^4.6.4",
|
||||
|
||||
@@ -217,12 +217,6 @@ By default, BBT clears all caches whenever BBT or Zotero is upgraded. I can't re
|
||||
|
||||
If you have this on, and you experience any problem that is not the cache getting dropped on upgrade, you *must* clear the cache and reproduce the problem. When you change this setting, as with any setting change, the cache will be dropped.
|
||||
|
||||
#### Parallel background exports:
|
||||
|
||||
default: `1`
|
||||
|
||||
BBT can now perform its exports in a separate thread, and should no longer block Zotero's UI pretty much regardless of how large your library is. The default of 1 parallel export should suit most needs, but if you have many auto-exports set up, you may want to raise the maximum parallel exports to prevent queueing of exports. It is possible to turn background exports off by setting this value to `0` in the hidden preferences; you will get the old (blocking) behavior back, but you can't complain about Zotero being laggy during auto-exports. All Zotero exports are blocking, and it's a minor miracle I got background exports to work at all.
|
||||
|
||||
#### Enable caching for background exports
|
||||
|
||||
default: `yes`
|
||||
|
||||
@@ -208,6 +208,12 @@ default: `no`
|
||||
|
||||
Both Zotero and BBT expect titles to be in sentence-case, but a lot of sites offer import data that is Title Cased. When exporting these titles to bib(la)tex you're going to get a lot of extra unwanted braces, because all these Title Cased words will look like proper nouns to BBTs own title-casing mechanism. When this setting is on, you will be warned when you import/save items in Zotero with titles that look like they're Title Cased, so that you can inspect/correct them.
|
||||
|
||||
## worker
|
||||
|
||||
default: `yes`
|
||||
|
||||
BBT can perform its exports in a separate thread, and should no longer block Zotero's UI pretty much regardless of how large your library is. It is possible to turn background exports off by setting this value to `false` in the hidden preferences; you will get the old (blocking) behavior back, but you can't complain about Zotero being laggy during auto-exports; all Zotero exports are blocking, and it's a minor miracle I got background exports to work at all.
|
||||
|
||||
## Abbreviation style:
|
||||
|
||||
default: `<not set>`
|
||||
|
||||
@@ -82,5 +82,5 @@
|
||||
"verbatimFields": "url,doi,file,ids,eprint,/^verb[a-z]$/,groups,/^citeulike-linkout-[0-9]+$/, /^bdsk-url-[0-9]+$/",
|
||||
"warnBulkModify": 10,
|
||||
"warnTitleCased": false,
|
||||
"workers": 1
|
||||
"worker": true
|
||||
}
|
||||
@@ -391,8 +391,8 @@
|
||||
"warnTitleCased": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"workers": {
|
||||
"type": "number"
|
||||
"worker": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
|
||||
@@ -815,11 +815,10 @@
|
||||
"var": "warnTitleCased"
|
||||
},
|
||||
{
|
||||
"name": "workers",
|
||||
"type": "number",
|
||||
"default": 1,
|
||||
"name": "worker",
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"affects": [],
|
||||
"var": "workers",
|
||||
"label": "Parallel background exports:"
|
||||
"var": "worker"
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user