Skip to content

Commit

Permalink
♻️ refactor(lib/pdf): Factor out isomorphic canvas helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed May 13, 2024
1 parent 7d7671e commit 4b932fa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
50 changes: 50 additions & 0 deletions imports/lib/canvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {type Canvas} from 'canvas/types';

export {
type Canvas,
type JpegConfig,
type PdfConfig,
type PngConfig,
} from 'canvas/types';

type CreateCanvasOptions = {
width: number;
height: number;
};

const createCanvasIso = async ({
width,
height,
}: CreateCanvasOptions): Promise<HTMLCanvasElement | Canvas> => {
if (Meteor.isServer) {
const {createCanvas} = await import('canvas');
return createCanvas(width, height);
}

const browserCanvas = document.createElement('canvas');
browserCanvas.width = width;
browserCanvas.height = height;
return browserCanvas;
};

export const destroyCanvasIso = (canvas: HTMLCanvasElement | Canvas) => {
canvas.width = 0;
canvas.height = 0;
if (Meteor.isClient) {
(canvas as HTMLCanvasElement).remove();
}
};

type CreateContextOptions = {} & CreateCanvasOptions;

export const createContextIso = async (
options: CreateContextOptions,
): Promise<CanvasRenderingContext2D> => {
return createCanvasIso(options).then(
(canvas) => canvas.getContext('2d') as CanvasRenderingContext2D,
);
};

export const destroyContextIso = (context: CanvasRenderingContext2D) => {
destroyCanvasIso(context.canvas);
};
51 changes: 8 additions & 43 deletions imports/lib/pdf/pdfthumbnails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
import {type Buffer} from 'buffer';
import {type Readable} from 'stream';

import addDays from 'date-fns/addDays';

import {cache as lru, type IndexedDBPersistedLRUCache} from '../cache/lru';

import {
type Canvas,
type JpegConfig,
type PdfConfig,
type PngConfig,
} from 'canvas/types';
import addDays from 'date-fns/addDays';

import {cache as lru, type IndexedDBPersistedLRUCache} from '../cache/lru';
createContextIso,
destroyContextIso,
} from '../canvas';

import {type DocumentInitParameters, type PageViewport, fetchPDF} from './pdf';

Expand All @@ -22,44 +25,6 @@ if (Meteor.isClient) {
});
}

type CreateCanvasOptions = {
width: number;
height: number;
};

const createCanvasIso = async ({
width,
height,
}: CreateCanvasOptions): Promise<HTMLCanvasElement | Canvas> => {
if (Meteor.isServer) {
const {createCanvas} = await import('canvas');
return createCanvas(width, height);
}

const browserCanvas = document.createElement('canvas');
browserCanvas.width = width;
browserCanvas.height = height;
return browserCanvas;
};

const destroyCanvasIso = (canvas: HTMLCanvasElement | Canvas) => {
canvas.width = 0;
canvas.height = 0;
if (Meteor.isClient) {
(canvas as HTMLCanvasElement).remove();
}
};

type CreateContextOptions = {} & CreateCanvasOptions;

const createContextIso = async (
options: CreateContextOptions,
): Promise<CanvasRenderingContext2D> => {
return createCanvasIso(options).then(
(canvas) => canvas.getContext('2d') as CanvasRenderingContext2D,
);
};

type RenderContext = {
canvasContext: CanvasRenderingContext2D;
viewport: PageViewport;
Expand Down Expand Up @@ -91,7 +56,7 @@ const createRenderContextIso = async (
};

const destroyRenderContextIso = (renderContext: RenderContext) => {
destroyCanvasIso(renderContext.canvasContext.canvas);
destroyContextIso(renderContext.canvasContext);
// @ts-expect-error This is for garbage collection.
renderContext.canvasContext = null;
};
Expand Down

0 comments on commit 4b932fa

Please sign in to comment.