Skip to content

Commit edab14a

Browse files
gevzakHackbrettXXX
andauthored
Added typings for internal events API (#3014)
Co-authored-by: Lukas Holländer <[email protected]>
1 parent 6579099 commit edab14a

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

types/index.d.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
*/
5151

5252
declare module "jspdf" {
53-
import construct = Reflect.construct;
54-
5553
export interface Annotation {
5654
type: "text" | "freetext" | "link";
5755
title?: string;
@@ -633,6 +631,20 @@ declare module "jspdf" {
633631
yStep?: number;
634632
}
635633

634+
export interface PubSub {
635+
subscribe(
636+
topic: string,
637+
callback: (...args: any[]) => void,
638+
once?: boolean
639+
): string;
640+
unsubscribe(token: string): boolean;
641+
publish(topic: string, ...args: any[]): void;
642+
getTopics(): Record<
643+
string,
644+
Record<string, [(...args: any[]) => void, boolean]>
645+
>;
646+
}
647+
636648
export class jsPDF {
637649
constructor(options?: jsPDFOptions);
638650
constructor(
@@ -844,6 +856,7 @@ declare module "jspdf" {
844856
getVerticalCoordinateString(value: number): number;
845857

846858
internal: {
859+
events: PubSub;
847860
scaleFactor: number;
848861
pageSize: {
849862
width: number;

types/jspdf-tests-node.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ const {
1818
TilingPattern
1919
} = jspdf;
2020

21+
function pubsub() {
22+
const doc = new jsPDF();
23+
const token = doc.internal.events.subscribe("topic", (a, b) => {}, true);
24+
doc.internal.events.unsubscribe(token);
25+
doc.internal.events.publish("topic", 1, "foo");
26+
const topics = doc.internal.events.getTopics();
27+
if (topics["topic"][token][1]) {
28+
topics["topic"][token][0](1, "foo");
29+
}
30+
}
31+
2132
function classes() {
2233
new GState({});
2334
new TilingPattern([], 0, 0);

types/jspdf-tests.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ import {
1717
AcroFormTextField
1818
} from "jspdf";
1919

20+
function pubsub() {
21+
const doc = new jsPDF();
22+
const token = doc.internal.events.subscribe("topic", (a, b) => {}, true);
23+
doc.internal.events.unsubscribe(token);
24+
doc.internal.events.publish("topic", 1, "foo");
25+
const topics = doc.internal.events.getTopics();
26+
if (topics["topic"][token][1]) {
27+
topics["topic"][token][0](1, "foo");
28+
}
29+
}
30+
2031
function classes() {
2132
new GState({});
2233
new TilingPattern([], 0, 0);

types/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"noEmit": true,
99
"forceConsistentCasingInFileNames": true
1010
},
11-
"files": ["index.d.ts", "jspdf-tests.ts", "jspdf-tests.ts"]
11+
"files": ["index.d.ts", "jspdf-tests.ts"]
1212
}

0 commit comments

Comments
 (0)