Skip to content

Commit

Permalink
fix: Make timer functions work in both Zotero 6 and 7
Browse files Browse the repository at this point in the history
  • Loading branch information
dvanoni committed Jul 1, 2023
1 parent 9f74de3 commit 037ac30
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
13 changes: 12 additions & 1 deletion esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ require('@dvanoni/zotero-plugin/copy-assets');
require('@dvanoni/zotero-plugin/generate-install-manifest');
require('@dvanoni/zotero-plugin/version');

const banner = `if (!Zotero.Notero) {
// Make timer functions globally available in Zotero 6
if (typeof setTimeout === 'undefined') {
var setTimeout = Zotero.setTimeout;
}
if (typeof clearTimeout === 'undefined') {
var clearTimeout = Zotero.clearTimeout;
}
`;

const builds = [
{
entryPoints: ['src/bootstrap.ts'],
Expand All @@ -17,7 +28,7 @@ const builds = [
target: ['firefox60'],
entryPoints: ['src/content/notero.ts'],
outdir: 'build/content',
banner: { js: 'if (!Zotero.Notero) {\n' },
banner: { js: banner },
footer: { js: '\n}' },
},
{
Expand Down
20 changes: 0 additions & 20 deletions patches/@notionhq+client+2.2.3.patch

This file was deleted.

6 changes: 3 additions & 3 deletions src/content/services/sync-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const SYNC_DEBOUNCE_MS = 2000;

type QueuedSync = {
readonly itemIDs: Set<Zotero.Item['id']>;
timeoutID?: ReturnType<Zotero['setTimeout']>;
timeoutID?: ReturnType<typeof setTimeout>;
};

export default class SyncManager implements Service {
Expand Down Expand Up @@ -210,15 +210,15 @@ export default class SyncManager implements Service {
if (!items.length) return;

if (this.queuedSync?.timeoutID) {
Zotero.clearTimeout(this.queuedSync.timeoutID);
clearTimeout(this.queuedSync.timeoutID);
}

const itemIDs = new Set([
...(this.queuedSync?.itemIDs?.values() ?? []),
...items.map(({ id }) => id),
]);

const timeoutID = Zotero.setTimeout(() => {
const timeoutID = setTimeout(() => {
if (!this.queuedSync) return;

this.queuedSync.timeoutID = undefined;
Expand Down
5 changes: 3 additions & 2 deletions typings/zotero.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,10 @@ declare interface Zotero {

/**
* @see https://groups.google.com/g/zotero-dev/c/O1TGIpfAdT0/m/Z7S8ONANAgAJ
* @see https://groups.google.com/g/zotero-dev/c/gvuARfJBDIo
*/
setTimeout(func: () => unknown, ms: number): number;
clearTimeout(id: ReturnType<this['setTimeout']>): void;
setTimeout?(func: () => unknown, ms: number): number;
clearTimeout?(id: number): void;
}

declare const Zotero: Zotero;
Expand Down

0 comments on commit 037ac30

Please sign in to comment.