Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use one pdf worker for all documents and for all rerenders #762

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Document.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ import {
isFile as isFileProp,
isRef,
} from './shared/propTypes';
import Singleton from './Singleton';

const { PDFDataRangeTransport } = pdfjs;

const workerProvider = new Singleton(
() => new pdfjs.PDFWorker({ name: 'react-pdf-worker' }),
(pdfjsWorker) => pdfjsWorker.destroy(),
);

export default class Document extends PureComponent {
state = {
pdf: null,
Expand Down Expand Up @@ -70,6 +76,11 @@ export default class Document extends PureComponent {
linkService = new LinkService();

componentDidMount() {
const { options } = this.props;
if (options && !options.worker) {
this.worker = workerProvider.acquire();
}

this.loadDocument();
this.setupLinkService();
}
Expand All @@ -87,6 +98,7 @@ export default class Document extends PureComponent {

// If loading is in progress, let's destroy it
if (this.loadingTask) this.loadingTask.destroy();
if (this.worker) workerProvider.release();
}

loadDocument = async () => {
Expand All @@ -112,6 +124,10 @@ export default class Document extends PureComponent {

const { options, onLoadProgress, onPassword } = this.props;

if (this.worker) {
options.worker = this.worker;
}

try {
// If another rendering is in progress, let's cancel it
cancelRunningTask(this.runningTask);
Expand Down
29 changes: 29 additions & 0 deletions src/Singleton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Singleton {
constructor(create, destroy) {
this.create = create;
this.destroy = destroy;

this.usageCount = 0;
}

acquire() {
if (!this.usageCount) {
this.instance = this.create();
}

this.usageCount += 1;

return this.instance;
}

release() {
this.usageCount -= 1;

if (this.usageCount <= 0) {
this.destroy(this.instance);
this.usageCount = 0;
}
}
}

export default Singleton;
35 changes: 35 additions & 0 deletions src/Singleton.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Singleton from './Singleton';

describe('Singleton', () => {
it('should create instance only on first acquire call', () => {
const create = jest.fn();
const singleton = new Singleton(create, () => {});

singleton.acquire();
singleton.acquire();

expect(create).toHaveBeenCalledTimes(1);
});

it('should destroy instance when nobody uses it', () => {
const destroy = jest.fn();
const singleton = new Singleton(() => {}, destroy);

singleton.acquire();
singleton.acquire();
singleton.release();
singleton.release();

expect(destroy).toHaveBeenCalledTimes(1);
});

it('returns one instance for all acquire calls', () => {
const instance = {};
const singleton = new Singleton(() => instance, () => {});

const first = singleton.acquire();
const second = singleton.acquire();

expect(first).toEqual(second);
});
});