fix(app.tsx): add safe check for readyPromise (#2489)
* fix(app.tsx): add safe check for readyPromise * make type-safe Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
parent
fba37e422d
commit
8f8fd023f8
@ -301,9 +301,9 @@ class App extends React.Component<ExcalidrawProps, AppState> {
|
|||||||
};
|
};
|
||||||
if (excalidrawRef) {
|
if (excalidrawRef) {
|
||||||
const readyPromise =
|
const readyPromise =
|
||||||
typeof excalidrawRef === "function"
|
("current" in excalidrawRef && excalidrawRef.current?.readyPromise) ||
|
||||||
? resolvablePromise<ExcalidrawImperativeAPI>()
|
resolvablePromise<ExcalidrawImperativeAPI>();
|
||||||
: excalidrawRef.current!.readyPromise;
|
|
||||||
const api: ExcalidrawImperativeAPI = {
|
const api: ExcalidrawImperativeAPI = {
|
||||||
ready: true,
|
ready: true,
|
||||||
readyPromise,
|
readyPromise,
|
||||||
|
@ -24,7 +24,9 @@ import { ExcalidrawElement } from "../element/types";
|
|||||||
import { SAVE_TO_LOCAL_STORAGE_TIMEOUT } from "./app_constants";
|
import { SAVE_TO_LOCAL_STORAGE_TIMEOUT } from "./app_constants";
|
||||||
import { EVENT_LOAD, EVENT_SHARE, trackEvent } from "../analytics";
|
import { EVENT_LOAD, EVENT_SHARE, trackEvent } from "../analytics";
|
||||||
|
|
||||||
const excalidrawRef: React.MutableRefObject<ExcalidrawAPIRefValue> = {
|
const excalidrawRef: React.MutableRefObject<
|
||||||
|
MarkRequired<ExcalidrawAPIRefValue, "ready" | "readyPromise">
|
||||||
|
> = {
|
||||||
current: {
|
current: {
|
||||||
readyPromise: resolvablePromise(),
|
readyPromise: resolvablePromise(),
|
||||||
ready: false,
|
ready: false,
|
||||||
|
3
src/global.d.ts
vendored
3
src/global.d.ts
vendored
@ -43,6 +43,9 @@ type ResolutionType<T extends (...args: any) => any> = T extends (
|
|||||||
// https://github.com/krzkaczor/ts-essentials
|
// https://github.com/krzkaczor/ts-essentials
|
||||||
type MarkOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
type MarkOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
||||||
|
|
||||||
|
type MarkRequired<T, RK extends keyof T> = Exclude<T, RK> &
|
||||||
|
Required<Pick<T, RK>>;
|
||||||
|
|
||||||
// PNG encoding/decoding
|
// PNG encoding/decoding
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
type TEXtChunk = { name: "tEXt"; data: Uint8Array };
|
type TEXtChunk = { name: "tEXt"; data: Uint8Array };
|
||||||
|
10
src/types.ts
10
src/types.ts
@ -133,14 +133,16 @@ export declare class GestureEvent extends UIEvent {
|
|||||||
export type LibraryItem = readonly NonDeleted<ExcalidrawElement>[];
|
export type LibraryItem = readonly NonDeleted<ExcalidrawElement>[];
|
||||||
export type LibraryItems = readonly LibraryItem[];
|
export type LibraryItems = readonly LibraryItem[];
|
||||||
|
|
||||||
|
// NOTE ready/readyPromise props are optional for host apps' sake (our own
|
||||||
|
// implem guarantees existence)
|
||||||
export type ExcalidrawAPIRefValue =
|
export type ExcalidrawAPIRefValue =
|
||||||
| (ExcalidrawImperativeAPI & {
|
| (ExcalidrawImperativeAPI & {
|
||||||
readyPromise: ResolvablePromise<ExcalidrawImperativeAPI>;
|
readyPromise?: ResolvablePromise<ExcalidrawImperativeAPI>;
|
||||||
ready: true;
|
ready?: true;
|
||||||
})
|
})
|
||||||
| {
|
| {
|
||||||
readyPromise: ResolvablePromise<ExcalidrawImperativeAPI>;
|
readyPromise?: ResolvablePromise<ExcalidrawImperativeAPI>;
|
||||||
ready: false;
|
ready?: false;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface ExcalidrawProps {
|
export interface ExcalidrawProps {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user