Skip to content

Commit 7488efc

Browse files
authored
Merge pull request #862 from ahmed-s-fatahallah/main
refactor: replace all `useEffect` and `useCallback` with one `useEffect`
2 parents 1a57f45 + 7688467 commit 7488efc

File tree

1 file changed

+41
-62
lines changed

1 file changed

+41
-62
lines changed

Diff for: packages/wrapper-react/src/index.tsx

+41-62
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See LICENSE in the package root for license information.
44
* ------------------------------------------------------------------------------------------ */
55

6-
import React, { type CSSProperties, useCallback, useEffect, useRef } from 'react';
6+
import React, { type CSSProperties, useEffect, useRef } from 'react';
77
import { MonacoEditorLanguageClientWrapper, type TextContents, type WrapperConfig } from 'monaco-editor-wrapper';
88

99
export type MonacoEditorProps = {
@@ -12,8 +12,7 @@ export type MonacoEditorProps = {
1212
wrapperConfig: WrapperConfig,
1313
onTextChanged?: (textChanges: TextContents) => void;
1414
onLoad?: (wrapper: MonacoEditorLanguageClientWrapper) => void;
15-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
16-
onError?: (e: any) => void;
15+
onError?: (e: unknown) => void;
1716
}
1817

1918
export const MonacoEditorReactComp: React.FC<MonacoEditorProps> = (props) => {
@@ -30,72 +29,52 @@ export const MonacoEditorReactComp: React.FC<MonacoEditorProps> = (props) => {
3029
const containerRef = useRef<HTMLDivElement>(null);
3130

3231
useEffect(() => {
33-
return () => {
34-
destroyMonaco();
32+
const destroyMonaco = async () => {
33+
try {
34+
await wrapperRef.current.dispose();
35+
} catch {
36+
// The language client may throw an error during disposal.
37+
// This should not prevent us from continue working.
38+
}
3539
};
36-
}, []);
37-
38-
useEffect(() => {
39-
handleReInit();
40-
}, [wrapperConfig]);
41-
42-
useEffect(() => {
43-
handleOnTextChanged();
44-
}, [onTextChanged]);
4540

46-
useEffect(() => {
47-
if (containerRef.current) {
48-
containerRef.current.className = className ?? '';
49-
wrapperConfig.htmlContainer = containerRef.current;
50-
}
51-
}, [className]);
52-
53-
const handleReInit = useCallback(async () => {
54-
await destroyMonaco();
55-
await initMonaco();
56-
await startMonaco();
57-
}, [wrapperConfig]);
58-
59-
const initMonaco = useCallback(async () => {
60-
if (containerRef.current) {
61-
wrapperConfig.htmlContainer = containerRef.current;
62-
await wrapperRef.current.init(wrapperConfig);
63-
} else {
64-
throw new Error('No htmlContainer found! Aborting...');
65-
}
66-
}, [wrapperConfig]);
41+
const initMonaco = async () => {
42+
if (containerRef.current) {
43+
wrapperConfig.htmlContainer = containerRef.current;
44+
await wrapperRef.current.init(wrapperConfig);
45+
} else {
46+
throw new Error('No htmlContainer found! Aborting...');
47+
}
48+
};
6749

68-
const startMonaco = useCallback(async () => {
69-
if (containerRef.current) {
70-
try {
71-
wrapperRef.current.registerTextChangeCallback(onTextChanged);
72-
await wrapperRef.current.start();
73-
onLoad?.(wrapperRef.current);
74-
handleOnTextChanged();
75-
} catch (e) {
76-
if (onError) {
77-
onError(e);
78-
} else {
79-
throw e;
50+
const startMonaco = async () => {
51+
if (containerRef.current) {
52+
try {
53+
wrapperRef.current.registerTextChangeCallback(onTextChanged);
54+
await wrapperRef.current.start();
55+
onLoad?.(wrapperRef.current);
56+
} catch (e) {
57+
if (onError) {
58+
onError(e);
59+
} else {
60+
throw e;
61+
}
8062
}
63+
} else {
64+
throw new Error('No htmlContainer found! Aborting...');
8165
}
82-
} else {
83-
throw new Error('No htmlContainer found! Aborting...');
84-
}
85-
}, [onError, onLoad, onTextChanged]);
66+
};
8667

87-
const handleOnTextChanged = useCallback(() => {
88-
if (!onTextChanged) return;
89-
}, [onTextChanged, wrapperConfig]);
68+
(async () => {
69+
await initMonaco();
70+
await startMonaco();
71+
})();
9072

91-
const destroyMonaco = useCallback(async () => {
92-
try {
93-
await wrapperRef.current.dispose();
94-
} catch {
95-
// The language client may throw an error during disposal.
96-
// This should not prevent us from continue working.
97-
}
98-
}, []);
73+
return () => {
74+
destroyMonaco();
75+
};
76+
77+
}, [wrapperConfig]);
9978

10079
return (
10180
<div

0 commit comments

Comments
 (0)