|
1 | 1 | declare global { |
2 | 2 | interface Window { |
3 | | - daum: { |
4 | | - postcode: { |
| 3 | + daum?: { |
| 4 | + postcode?: { |
5 | 5 | load: (fn: () => void) => void; |
6 | 6 | version: string; |
7 | 7 | _validParam_: boolean; |
8 | 8 | }; |
9 | | - Postcode: PostcodeConstructor; |
| 9 | + Postcode?: PostcodeConstructor; |
10 | 10 | }; |
11 | 11 | } |
12 | 12 | } |
@@ -73,7 +73,7 @@ export interface Theme { |
73 | 73 | outlineColor?: string; |
74 | 74 | } |
75 | 75 |
|
76 | | -export interface ConstructorOptions { |
| 76 | +export interface PostcodeOptions { |
77 | 77 | oncomplete?: (address: Address) => void; |
78 | 78 | onresize?: (size: Size) => void; |
79 | 79 | onclose?: (state: State) => void; |
@@ -114,59 +114,40 @@ export interface EmbedOptions { |
114 | 114 | autoClose?: boolean; |
115 | 115 | } |
116 | 116 |
|
| 117 | +export interface PostcodeConstructor { |
| 118 | + new (postcodeOptions: PostcodeOptions): Postcode; |
| 119 | +} |
| 120 | + |
117 | 121 | export interface Postcode { |
118 | 122 | open(openOptions?: OpenOptions): void; |
119 | 123 | embed(element: HTMLElement, embedOptions?: EmbedOptions): void; |
120 | 124 | } |
121 | 125 |
|
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'; |
125 | 127 |
|
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; |
144 | 131 |
|
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; |
146 | 134 |
|
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 | + }) |
148 | 148 |
|
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 | +})(); |
171 | 152 |
|
172 | 153 | export default loadPostcode; |
0 commit comments