mirror of
https://github.com/retorquere/zotero-better-bibtex.git
synced 2022-05-23 09:36:29 +03:00
event wildcards
This commit is contained in:
@@ -6,6 +6,7 @@ config.rules['max-classes-per-file'] = 'off'
|
||||
config.rules['no-console'] = 'error'
|
||||
config.rules['no-new-func'] = 'off'
|
||||
config.rules['no-underscore-dangle'] = [ 'error', { "allowAfterThis": true } ]
|
||||
config.rules['prefer-template'] = 'off'
|
||||
|
||||
config.rules['@typescript-eslint/no-unsafe-member-access'] = 'off'
|
||||
config.rules['@typescript-eslint/no-unsafe-call'] = 'off'
|
||||
|
||||
@@ -36,7 +36,7 @@ Components.utils.import('resource://gre/modules/AddonManager.jsm')
|
||||
declare const AddonManager: any
|
||||
|
||||
import { log } from './logger'
|
||||
import { Events, itemsChanged as notifyItemsChanged } from './events'
|
||||
import { Events } from './events'
|
||||
|
||||
import { Translators } from './translators'
|
||||
import { DB } from './db/main'
|
||||
@@ -704,7 +704,7 @@ notify('item', (action: string, type: any, ids: any[], extraData: { [x: string]:
|
||||
return
|
||||
}
|
||||
|
||||
notifyItemsChanged(items.concat(parents))
|
||||
Events.itemsChanged(items.concat(parents))
|
||||
})
|
||||
|
||||
notify('collection', (event: string, _type: any, ids: string | any[], _extraData: any) => {
|
||||
|
||||
@@ -1,58 +1,80 @@
|
||||
/* eslint-disable prefer-rest-params */
|
||||
|
||||
import { EventEmitter } from 'eventemitter3'
|
||||
import { patch as $patch$ } from './monkey-patch'
|
||||
import { EventEmitter as EventEmitter3 } from 'eventemitter3'
|
||||
import { log } from './logger'
|
||||
|
||||
// export singleton: https://k94n.com/es6-modules-single-instance-pattern
|
||||
export const Events = new EventEmitter() // eslint-disable-line @typescript-eslint/naming-convention,no-underscore-dangle,id-blacklist,id-match
|
||||
const events: string[] = [
|
||||
'error',
|
||||
'preference-changed',
|
||||
'item-tag',
|
||||
'items-changed',
|
||||
'items-removed',
|
||||
'libraries-changed',
|
||||
'collections-changed',
|
||||
'collections-removed',
|
||||
'libraries-removed',
|
||||
'export-progress',
|
||||
'loaded',
|
||||
]
|
||||
const event_prefix = events.map(name => name + '.')
|
||||
|
||||
if (Zotero.Prefs.get('translators.better-bibtex.log-events')) {
|
||||
const events = [
|
||||
'preference-changed',
|
||||
'item-tag',
|
||||
'items-changed',
|
||||
'items-removed',
|
||||
'libraries-changed',
|
||||
'collections-changed',
|
||||
'collections-removed',
|
||||
'libraries-removed',
|
||||
'export-progress',
|
||||
'loaded',
|
||||
]
|
||||
const log_events = Zotero.Prefs.get('translators.better-bibtex.log-events')
|
||||
|
||||
$patch$(Events, 'on', original => function() {
|
||||
if (!events.includes(arguments[0])) throw new Error(`Unsupported event ${arguments[0]}`)
|
||||
original.apply(this, arguments)
|
||||
})
|
||||
|
||||
$patch$(Events, 'emit', original => function() {
|
||||
if (!events.includes(arguments[0])) throw new Error(`Unsupported event ${arguments[0]}`)
|
||||
Zotero.debug(`event-emit: ${JSON.stringify(Array.from(arguments))}`)
|
||||
original.apply(this, arguments)
|
||||
})
|
||||
}
|
||||
|
||||
export function itemsChanged(items: ZoteroItem[]): void {
|
||||
if (!items.length) return
|
||||
|
||||
const changed = {
|
||||
collections: new Set,
|
||||
libraries: new Set,
|
||||
export const Events = new class EventEmitter extends EventEmitter3 {
|
||||
constructor() {
|
||||
super()
|
||||
this.on('error', err => {
|
||||
throw Zotero.debug(err)
|
||||
})
|
||||
}
|
||||
|
||||
for (const item of items) {
|
||||
changed.libraries.add(item.libraryID)
|
||||
private verify(event: string | symbol) {
|
||||
if (!log_events) return true
|
||||
if (typeof event === 'symbol') return false
|
||||
if (events.includes(event) || event_prefix.find(prefix => event.startsWith(prefix))) return true
|
||||
throw new Error(`Unsupported event ${event}`)
|
||||
}
|
||||
|
||||
for (let collectionID of item.getCollections()) {
|
||||
if (changed.collections.has(collectionID)) continue
|
||||
public on(event: string | symbol, handler: (...args: any[]) => void, self?: any): this {
|
||||
this.verify(event)
|
||||
super.on(event, handler, self)
|
||||
return this
|
||||
}
|
||||
|
||||
while (collectionID) {
|
||||
changed.collections.add(collectionID)
|
||||
collectionID = Zotero.Collections.get(collectionID).parentID
|
||||
public emit(event: string | symbol, ...args: any[]): boolean {
|
||||
this.verify(event)
|
||||
|
||||
const prefix: string = typeof event === 'string' ? event + '.' : '\0'
|
||||
const results: boolean[] = []
|
||||
for (const listening of this.eventNames()) {
|
||||
log.debug('event.emit(', listening, args, ')')
|
||||
if (listening === event || (typeof listening === 'string' && listening.startsWith(prefix))) results.push(super.emit.apply(this, [listening, ...args]))
|
||||
}
|
||||
return results.length === 0 ? false : !results.find(r => !r)
|
||||
}
|
||||
|
||||
public itemsChanged(items: ZoteroItem[]): void {
|
||||
if (!items.length) return
|
||||
|
||||
const changed = {
|
||||
collections: new Set,
|
||||
libraries: new Set,
|
||||
}
|
||||
|
||||
for (const item of items) {
|
||||
changed.libraries.add(item.libraryID)
|
||||
|
||||
for (let collectionID of item.getCollections()) {
|
||||
if (changed.collections.has(collectionID)) continue
|
||||
|
||||
while (collectionID) {
|
||||
changed.collections.add(collectionID)
|
||||
collectionID = Zotero.Collections.get(collectionID).parentID
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed.collections.size) Events.emit('collections-changed', [...changed.collections])
|
||||
if (changed.libraries.size) Events.emit('libraries-changed', [...changed.libraries])
|
||||
if (changed.collections.size) this.emit('collections-changed', [...changed.collections])
|
||||
if (changed.libraries.size) this.emit('libraries-changed', [...changed.libraries])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { jieba } from './key-manager/chinese'
|
||||
import { Scheduler } from './scheduler'
|
||||
import { log } from './logger'
|
||||
import { flash } from './flash'
|
||||
import { Events, itemsChanged as notifyItemsChanged } from './events'
|
||||
import { Events } from './events'
|
||||
import { fetchAsync as fetchInspireHEP } from './inspire-hep'
|
||||
import * as Extra from './extra'
|
||||
import { $and, Query } from './db/loki'
|
||||
@@ -160,7 +160,7 @@ export class KeyManager {
|
||||
}
|
||||
}
|
||||
|
||||
if (updates.length) notifyItemsChanged(updates)
|
||||
if (updates.length) Events.itemsChanged(updates)
|
||||
}
|
||||
|
||||
public async init(): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user