Skip to content

Commit b84c130

Browse files
committed
resolve conflict
2 parents 5efc421 + 061c44d commit b84c130

File tree

3 files changed

+40
-58
lines changed

3 files changed

+40
-58
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-daum-postcode",
3-
"version": "3.0.1",
3+
"version": "3.0.3",
44
"description": "Daum Postcode service for React",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/DaumPostcodeEmbed.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { Component, createRef, CSSProperties } from 'react';
2-
import loadPostcode, { postcodeScriptUrl, ConstructorOptions } from './loadPostcode';
2+
import loadPostcode, { postcodeScriptUrl, PostcodeConstructor, PostcodeOptions } from './loadPostcode';
33

44
export interface DaumPostcodeEmbedProps
5-
extends Omit<ConstructorOptions, 'oncomplete' | 'onresize' | 'onclose' | 'onsearch' | 'width' | 'height'> {
6-
onComplete?: ConstructorOptions['oncomplete'];
7-
onResize?: ConstructorOptions['onresize'];
8-
onClose?: ConstructorOptions['onclose'];
9-
onSearch?: ConstructorOptions['onsearch'];
5+
extends Omit<PostcodeOptions, 'oncomplete' | 'onresize' | 'onclose' | 'onsearch' | 'width' | 'height'> {
6+
onComplete?: PostcodeOptions['oncomplete'];
7+
onResize?: PostcodeOptions['onresize'];
8+
onClose?: PostcodeOptions['onclose'];
9+
onSearch?: PostcodeOptions['onsearch'];
1010
className?: string;
1111
style?: CSSProperties;
1212
defaultQuery?: string;
@@ -56,7 +56,7 @@ class DaumPostcodeEmbed extends Component<DaumPostcodeEmbedProps, State> {
5656
loadPostcode(scriptUrl).then(initiate).catch(onError);
5757
}
5858

59-
initiate = (Postcode: typeof window.daum.Postcode) => {
59+
initiate = (Postcode: PostcodeConstructor) => {
6060
if (!this.wrap.current) return;
6161
const { scriptUrl, className, style, defaultQuery, autoClose, errorMessage, onComplete, onClose, onResize, onSearch, ...options } =
6262
this.props;
@@ -77,7 +77,8 @@ class DaumPostcodeEmbed extends Component<DaumPostcodeEmbedProps, State> {
7777
postcode.embed(this.wrap.current, { q: defaultQuery, autoClose: autoClose });
7878
};
7979

80-
onError = () => {
80+
onError = (e: unknown) => {
81+
console.error(e);
8182
this.setState({ hasError: true });
8283
};
8384

src/loadPostcode.ts

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
declare global {
22
interface Window {
3-
daum: {
4-
postcode: {
3+
daum?: {
4+
postcode?: {
55
load: (fn: () => void) => void;
66
version: string;
77
_validParam_: boolean;
88
};
9-
Postcode: PostcodeConstructor;
9+
Postcode?: PostcodeConstructor;
1010
};
1111
}
1212
}
@@ -73,7 +73,7 @@ export interface Theme {
7373
outlineColor?: string;
7474
}
7575

76-
export interface ConstructorOptions {
76+
export interface PostcodeOptions {
7777
oncomplete?: (address: Address) => void;
7878
onresize?: (size: Size) => void;
7979
onclose?: (state: State) => void;
@@ -114,59 +114,40 @@ export interface EmbedOptions {
114114
autoClose?: boolean;
115115
}
116116

117+
export interface PostcodeConstructor {
118+
new (postcodeOptions: PostcodeOptions): Postcode;
119+
}
120+
117121
export interface Postcode {
118122
open(openOptions?: OpenOptions): void;
119123
embed(element: HTMLElement, embedOptions?: EmbedOptions): void;
120124
}
121125

122-
export interface PostcodeConstructor {
123-
new (constructorOptions: ConstructorOptions): Postcode;
124-
}
126+
export const postcodeScriptUrl = 'https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js';
125127

126-
const promiseQueue = (function () {
127-
const queue: Array<[(value: PostcodeConstructor) => void, (error: unknown) => void]> = [];
128-
const enqueue = queue.push.bind(queue);
129-
const dequeue = () => queue.shift() ?? [];
130-
const resolveAll = (value: PostcodeConstructor) => {
131-
while (queue.length) {
132-
const [resolve] = dequeue();
133-
resolve?.(value);
134-
}
135-
};
136-
const rejectAll = (value: unknown) => {
137-
while (queue.length) {
138-
const [_, reject] = dequeue();
139-
reject?.(value);
140-
}
141-
};
142-
return { enqueue, resolveAll, rejectAll };
143-
})();
128+
const loadPostcode = (function () {
129+
const scriptId = 'daum_postcode_script';
130+
let promise: Promise<PostcodeConstructor> | null = null;
144131

145-
export const postcodeScriptUrl = 'https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js';
132+
return function (url: string = postcodeScriptUrl): Promise<PostcodeConstructor> {
133+
if( promise ) return promise;
146134

147-
const scriptId = 'daum_postcode_script';
135+
promise = new Promise((resolve, reject) => {
136+
const script = document.createElement('script');
137+
script.src = url;
138+
script.onload = () => {
139+
if( window?.daum?.Postcode ) {
140+
return resolve(window.daum.Postcode);
141+
}
142+
reject(new Error('Script is loaded successfully, but cannot find Postcode module. Check your scriptURL property.'))
143+
};
144+
script.onerror = (error) => reject(error);
145+
script.id = scriptId;
146+
document.body.appendChild(script);
147+
})
148148

149-
const loadPostcode = (url: string = postcodeScriptUrl): Promise<typeof window.daum.Postcode> => {
150-
if (window.daum && window.daum.Postcode) {
151-
return Promise.resolve(window.daum.Postcode);
152-
}
153-
if (!document.getElementById(scriptId)) {
154-
const script = document.createElement('script');
155-
script.src = url;
156-
script.id = scriptId;
157-
script.onload = () => {
158-
try {
159-
promiseQueue.resolveAll(window.daum.Postcode);
160-
} catch (e) {
161-
promiseQueue.rejectAll(e);
162-
}
163-
};
164-
script.onerror = (error) => promiseQueue.rejectAll(error);
165-
document.body.appendChild(script);
166-
}
167-
return new Promise<PostcodeConstructor>((resolve, reject) => {
168-
promiseQueue.enqueue([resolve, reject]);
169-
});
170-
};
149+
return promise;
150+
};
151+
})();
171152

172153
export default loadPostcode;

0 commit comments

Comments
 (0)