Skip to content

Commit

Permalink
Fix memory leak (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhunjadi authored and diegomura committed May 12, 2019
1 parent aa5fab8 commit 02286a0
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 9 deletions.
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,17 @@ declare module '@react-pdf/renderer' {
const pdf: (
document: React.ReactElement<DocumentProps>,
) => {
container: any;
isDirty: () => boolean;
updateContainer: (document: React.ReactElement<any>) => void;
toBuffer: () => NodeJS.ReadableStream;
toBuffer: () => Promise<NodeJS.ReadableStream>;
toBlob: () => Promise<Blob>;
toString: () => string;
};

const renderToStream: (
document: React.ReactElement<DocumentProps>,
) => NodeJS.ReadableStream;
) => Promise<NodeJS.ReadableStream>;

const renderToFile: (
document: React.ReactElement<DocumentProps>,
Expand Down
9 changes: 9 additions & 0 deletions src/elements/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Document {
removeChild(child) {
const i = this.children.indexOf(child);
child.parent = null;
child.cleanup();
this.children.slice(i, 1);
}

Expand Down Expand Up @@ -122,6 +123,14 @@ class Document {
this.props = newProps;
}

cleanup() {
this.subpages.forEach(p => p.cleanup());
}

finish() {
this.children.forEach(c => c.cleanup());
}

getLayoutData() {
return {
type: this.name,
Expand Down
8 changes: 8 additions & 0 deletions src/elements/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Node {
this.children.splice(index, 1);
this.layout.removeChild(child.layout);
}

child.cleanup();
}

removeAllChilds() {
Expand Down Expand Up @@ -133,6 +135,12 @@ class Node {

onAppendDynamically() {}

cleanup() {
this.children.forEach(c => c.cleanup());
this.layout.unsetMeasureFunc();
Yoga.Node.destroy(this.layout);
}

get position() {
return this.layout.getPositionType() === Yoga.POSITION_TYPE_ABSOLUTE
? 'absolute'
Expand Down
2 changes: 2 additions & 0 deletions src/elements/Note.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Note extends Base {
child.parent = null;
this.children.splice(index, 1);
}

child.cleanup();
}

applyProps() {
Expand Down
10 changes: 10 additions & 0 deletions src/elements/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ class Root {
}

removeChild() {
this.document.cleanup();
this.document = null;
}

markDirty() {
this.isDirty = true;
}

cleanup() {
this.document.cleanup();
}

finish() {
this.document.finish();
}

async render() {
this.instance = new PDFDocument({ autoFirstPage: false });
await this.document.render();
this.cleanup();
this.isDirty = false;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/elements/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class Text extends Base {
this.computed = false;
this.attributedString = null;
this.markDirty();

child.cleanup();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/elements/TextInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class TextInstance {
return new this.constructor(this.root, this.value);
}

cleanup() {}

update(value) {
this.value = value;
this.parent.computed = false;
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ const pdf = input => {
});
}

function toBuffer() {
callOnRender();
async function toBuffer() {
await container.render();

container.render();
callOnRender();

return container.instance;
}
Expand All @@ -87,6 +87,7 @@ const pdf = input => {

return {
isDirty,
container,
updateContainer,
toBuffer,
toBlob,
Expand Down
11 changes: 7 additions & 4 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import {
createInstance,
} from './index';

export const renderToStream = function(element) {
return pdf(element).toBuffer();
export const renderToStream = async function(element) {
const instance = pdf(element);
const buffer = await instance.toBuffer();
instance.container.finish();
return buffer;
};

export const renderToFile = function(element, filePath, callback) {
const output = renderToStream(element);
export const renderToFile = async function(element, filePath, callback) {
const output = await renderToStream(element);
const stream = fs.createWriteStream(filePath);

output.pipe(stream);
Expand Down

0 comments on commit 02286a0

Please sign in to comment.