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

Added typing for subscribe and unsubscribe events #3014

Merged
Merged
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
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"]
}