Skip to content

Commit

Permalink
Added typings for internal events API (#3014)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Holländer <[email protected]>
  • Loading branch information
gevzak and HackbrettXXX authored Jan 15, 2021
1 parent 6579099 commit edab14a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
17 changes: 15 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
*/

declare module "jspdf" {
import construct = Reflect.construct;

export interface Annotation {
type: "text" | "freetext" | "link";
title?: string;
Expand Down Expand Up @@ -633,6 +631,20 @@ declare module "jspdf" {
yStep?: number;
}

export interface PubSub {
subscribe(
topic: string,
callback: (...args: any[]) => void,
once?: boolean
): string;
unsubscribe(token: string): boolean;
publish(topic: string, ...args: any[]): void;
getTopics(): Record<
string,
Record<string, [(...args: any[]) => void, boolean]>
>;
}

export class jsPDF {
constructor(options?: jsPDFOptions);
constructor(
Expand Down Expand Up @@ -844,6 +856,7 @@ declare module "jspdf" {
getVerticalCoordinateString(value: number): number;

internal: {
events: PubSub;
scaleFactor: number;
pageSize: {
width: number;
Expand Down
11 changes: 11 additions & 0 deletions types/jspdf-tests-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ const {
TilingPattern
} = jspdf;

function pubsub() {
const doc = new jsPDF();
const token = doc.internal.events.subscribe("topic", (a, b) => {}, true);
doc.internal.events.unsubscribe(token);
doc.internal.events.publish("topic", 1, "foo");
const topics = doc.internal.events.getTopics();
if (topics["topic"][token][1]) {
topics["topic"][token][0](1, "foo");
}
}

function classes() {
new GState({});
new TilingPattern([], 0, 0);
Expand Down
11 changes: 11 additions & 0 deletions types/jspdf-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import {
AcroFormTextField
} from "jspdf";

function pubsub() {
const doc = new jsPDF();
const token = doc.internal.events.subscribe("topic", (a, b) => {}, true);
doc.internal.events.unsubscribe(token);
doc.internal.events.publish("topic", 1, "foo");
const topics = doc.internal.events.getTopics();
if (topics["topic"][token][1]) {
topics["topic"][token][0](1, "foo");
}
}

function classes() {
new GState({});
new TilingPattern([], 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "jspdf-tests.ts", "jspdf-tests.ts"]
"files": ["index.d.ts", "jspdf-tests.ts"]
}

0 comments on commit edab14a

Please sign in to comment.