-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathkakaoMapApiLoader.ts
More file actions
319 lines (276 loc) · 8.1 KB
/
kakaoMapApiLoader.ts
File metadata and controls
319 lines (276 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { SIGNATURE } from "./constants"
export type Libraries = ("services" | "clusterer" | "drawing")[]
export interface LoaderOptions {
/**
* script 객체 생성시 사용자 정의 id
*/
id?: string
/**
* 발급 받은 Kakao 지도 Javscript API 키.
*
* @see [준비하기](https://apis.map.kakao.com/web/guide/#ready)
*/
appkey: string
/**
* 사용하는 라이브러리 목록
*
* Kakao 지도 Javascript API 는 지도와 함께 사용할 수 있는 라이브러리 를 지원하고 있습니다.
* 라이브러리는 javascript API와 관련되어 있지만 조금 특화된 기능을 묶어둔 것을 말합니다. 이 기능은 추가로 불러와서 사용할 수 있도록 되어있습니다.
* 현재 사용할 수 있는 라이브러리는 다음과 같습니다.
*
* clusterer: 마커를 클러스터링 할 수 있는 클러스터러 라이브러리 입니다.
* services: 장소 검색 과 주소-좌표 변환 을 할 수 있는 services 라이브러리 입니다.
* drawing: 지도 위에 마커와 그래픽스 객체를 쉽게 그릴 수 있게 그리기 모드를 지원하는 drawing 라이브러리 입니다.
* 라이브러리는 계속해서 추가될 예정입니다.
*/
libraries?: Libraries
/**
* 사용자 정의 Kakao 지도 javascript 경로 지정
*
* @default "//dapi.kakao.com/v2/maps/sdk.js"
*/
url?: string
/**
* 보안을 위한 nonce 값 설정
*/
nonce?: string
/**
* 스크립트 로드 재시도 횟수
*/
retries?: number
}
export enum LoaderStatus {
INITIALIZED,
LOADING,
SUCCESS,
FAILURE,
}
const DEFAULT_ID = `${SIGNATURE}_Loader`
export type LoaderErorr = Event | string
/**
* Kakao Map Api Loader
*
* `new Loader(options).load()` 함수를 이용하여 Api를 비동기적으로 삽입할 수 있습니다.
*
* 해당 Loader를 이용시 `react-kakao-maps-sdk` 내부에서 injection 되는 이벤트를 감지하여 kakao map api 로딩 이후에 렌더링을 진행합니다.
*/
export class Loader {
private static instance: Loader
private static loadEventCallback = new Set<(e?: LoaderErorr) => void>()
public readonly id: string
public readonly appkey: string
public readonly url: string
public readonly libraries: Libraries
public readonly nonce: string | undefined
public readonly retries: number
private callbacks: ((e?: LoaderErorr) => void)[] = []
private done = false
private loading = false
private errors: LoaderErorr[] = []
private onEvent: LoaderErorr | undefined
constructor({
appkey,
id = DEFAULT_ID,
libraries = [],
nonce,
retries = 3,
url = "//dapi.kakao.com/v2/maps/sdk.js",
}: LoaderOptions) {
this.id = id
this.appkey = appkey
this.libraries = libraries
this.nonce = nonce
this.retries = retries
this.url = url
if (
Loader.instance &&
!Loader.equalOptions(this.options, Loader.instance.options)
) {
if (!Loader.equalOptions(this.options, Loader.instance.options)) {
switch (Loader.instance.status) {
case LoaderStatus.FAILURE:
throw new Error(
`Loader must not be called again with different options. \n${JSON.stringify(
this.options,
null,
2,
)}\n!==\n${JSON.stringify(Loader.instance.options, null, 2)}`,
)
break
default:
Loader.instance.reset()
Loader.instance = this
break
}
}
}
if (!Loader.instance) {
Loader.instance = this
}
return Loader.instance
}
public get options() {
return {
appkey: this.appkey,
id: this.id,
libraries: this.libraries,
nonce: this.nonce,
retries: this.retries,
url: this.url,
}
}
public static addLoadEventLisnter(callback: (err?: LoaderErorr) => void) {
if (window.kakao && window.kakao.maps) {
window.kakao.maps.load(callback)
}
Loader.loadEventCallback.add(callback)
return callback
}
public static removeLoadEventLisnter(callback: (err?: LoaderErorr) => void) {
return Loader.loadEventCallback.delete(callback)
}
public load(): Promise<typeof kakao> {
return new Promise((resolve, reject) => {
this.loadCallback((err?: LoaderErorr) => {
if (!err) {
resolve(window.kakao)
} else {
reject(err)
}
})
})
}
public get status(): LoaderStatus {
if (this.onEvent) {
return LoaderStatus.FAILURE
}
if (this.done) {
return LoaderStatus.SUCCESS
}
if (this.loading) {
return LoaderStatus.LOADING
}
return LoaderStatus.INITIALIZED
}
private get failed(): boolean {
return this.done && !this.loading && this.errors.length >= this.retries + 1
}
private loadCallback(fn: (e?: LoaderErorr) => void): void {
this.callbacks.push(fn)
this.execute()
}
private resetIfRetryingFailed(): void {
if (this.failed) {
this.reset()
}
}
private reset(): void {
this.deleteScript()
this.done = true
this.loading = false
this.errors = []
this.onEvent = undefined
}
private execute() {
this.resetIfRetryingFailed()
if (this.done) {
this.callback()
} else {
if (window.kakao && window.kakao.maps) {
console.warn(
"Kakao Maps이 이미 외부 요소에 의해 로딩되어 있습니다." +
"설정한 옵션과 일치 하지 않을 수 있으며, 이에 따른 예상치 동작이 발생할 수 있습니다.",
)
window.kakao.maps.load(this.callback)
return
}
if (!this.loading) {
this.loading = true
this.setScript()
}
}
}
private setScript() {
if (document.getElementById(this.id)) {
this.callback()
}
const url = this.createUrl()
const script = document.createElement("script")
script.id = this.id
script.type = "text/javascript"
script.src = url
script.onerror = this.loadErrorCallback.bind(this)
script.onload = this.callback.bind(this)
script.defer = true
script.async = true
if (this.nonce) {
script.nonce = this.nonce
}
document.head.appendChild(script)
}
private loadErrorCallback(event: LoaderErorr): void {
this.errors.push(event)
if (this.errors.length <= this.retries) {
const delay = this.errors.length * 2 ** this.errors.length
console.log(`Failed to load Kakao Maps script, retrying in ${delay} ms.`)
setTimeout(() => {
this.deleteScript()
this.setScript()
}, delay)
} else {
this.done = true
this.loading = false
this.onEvent = this.errors[this.errors.length - 1]
this.callbacks.forEach((cb) => {
cb(this.onEvent)
})
this.callbacks = []
Loader.loadEventCallback.forEach((cb) => {
cb(this.onEvent)
})
}
}
public createUrl(): string {
let url = this.url
url += `?appkey=${this.appkey}`
if (this.libraries.length) {
url += `&libraries=${this.libraries.join(",")}`
}
url += `&autoload=false`
return url
}
private deleteScript() {
const script = document.getElementById(this.id)
if (script) {
script.remove()
}
}
private callback() {
kakao.maps.load(() => {
Loader.instance.done = true
Loader.instance.loading = false
Loader.instance.callbacks.forEach((cb) => {
cb(Loader.instance.onEvent)
})
Loader.instance.callbacks = []
Loader.loadEventCallback.forEach((cb) => {
cb(Loader.instance.onEvent)
})
})
}
private static equalOptions(
a: typeof Loader.prototype.options,
b: typeof Loader.prototype.options,
): boolean {
if (a.appkey !== b.appkey) return false
if (a.id !== b.id) return false
if (a.libraries.length !== b.libraries.length) return false
for (let i = 0; i < a.libraries.length; ++i) {
if (a.libraries[i] !== b.libraries[i]) return false
}
if (a.nonce !== b.nonce) return false
if (a.retries !== b.retries) return false
if (a.url !== b.url) return false
return true
}
}