-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement JawgJSLoader for JawgPlaces
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,4 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
.serve/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@jawg/js-loader", | ||
"version": "0.0.0", | ||
"description": "A library to load jawg js libraries", | ||
"main": "./dist/jawg-js-loader.js", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"watch": "rollup -c -w", | ||
"start": "ROLLUP_SERVE=true rollup -c -w" | ||
}, | ||
"author": "Jawg", | ||
"license": "SEE LICENSE IN LICENSE", | ||
"files": [ | ||
"src/index.js", | ||
"dist/jawg-js-loader.js", | ||
"README.md", | ||
"packages.json" | ||
], | ||
"devDependencies": { | ||
"@babel/core": "^7.14.8", | ||
"@babel/plugin-transform-runtime": "^7.14.5", | ||
"@babel/preset-env": "^7.14.8", | ||
"@babel/register": "^7.14.5", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@rollup/plugin-commonjs": "^11.0.1", | ||
"@rollup/plugin-json": "^4.1.0", | ||
"@rollup/plugin-node-resolve": "^7.0.0", | ||
"prettier": "^2.3.2", | ||
"rollup": "^2.53.3", | ||
"rollup-plugin-app-utils": "^1.0.6", | ||
"rollup-plugin-serve": "^1.1.0", | ||
"rollup-plugin-terser": "^7.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import resolve from '@rollup/plugin-node-resolve'; | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import json from '@rollup/plugin-json'; | ||
import { emptyDirectories } from 'rollup-plugin-app-utils'; | ||
import serve from 'rollup-plugin-serve'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import { babel } from '@rollup/plugin-babel'; | ||
const useServe = process.env.ROLLUP_SERVE === 'true'; | ||
const port = process.env.ROLLUP_PORT || 8000; | ||
const output = useServe ? '.serve' : 'dist'; | ||
|
||
export default [ | ||
{ | ||
input: 'src/index.js', | ||
plugins: [ | ||
emptyDirectories(output), | ||
resolve({ jsnext: true, preferBuiltins: true, browser: true }), | ||
commonjs(), | ||
json(), | ||
babel({ babelHelpers: 'bundled', presets: ['@babel/env'] }), | ||
terser(), | ||
].concat(useServe ? [serve({ host: 'localhost', port: port, contentBase: [output] })] : []), | ||
output: { | ||
name: 'JawgJSLoader', | ||
file: `${output}/jawg-js-loader.js`, | ||
sourcemap: useServe, | ||
format: 'umd', | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const CALLBACK_CACHE = {}; | ||
|
||
const createUrl = ({ lib, version = 'latest', accessToken }) => { | ||
let url = `https://api.jawg.io/libraries/`; | ||
switch (lib) { | ||
case 'JawgPlaces': | ||
url += `jawg-places@${version}/jawg-places.js`; | ||
break; | ||
default: | ||
throw new Error(`Library ${lib} not found`); | ||
} | ||
return `${url}?access-token=${accessToken}`; | ||
}; | ||
|
||
const load = ({ lib, version, accessToken } = {}) => { | ||
if (window[lib] !== undefined) { | ||
return new Promise.resolve(window[lib]); | ||
} else if (CALLBACK_CACHE[lib] !== undefined) { | ||
return new Promise((resolve, reject) => { | ||
CALLBACK_CACHE[lib].push({ resolve, reject }); | ||
}); | ||
} else if (CALLBACK_CACHE[lib] === undefined) { | ||
CALLBACK_CACHE[lib] = []; | ||
} | ||
return new Promise((resolve, reject) => { | ||
CALLBACK_CACHE[lib].push({ resolve, reject }); | ||
const url = createUrl({ lib, version, accessToken }); | ||
const script = document.createElement('script'); | ||
script.type = 'text/javascript'; | ||
script.src = url; | ||
script.onerror = (error) => { | ||
CALLBACK_CACHE[lib].forEach(({ reject }) => reject(error)); | ||
}; | ||
script.defer = true; | ||
script.async = true; | ||
script.onload = () => { | ||
CALLBACK_CACHE[lib].forEach(({ resolve }) => { | ||
resolve(window.JawgPlaces); | ||
}); | ||
}; | ||
|
||
document.head.appendChild(script); | ||
}); | ||
}; | ||
|
||
export default class JawgJSLoader { | ||
constructor({ accessToken }) { | ||
this.accessToken = accessToken; | ||
|
||
if (JawgJSLoader.instance) { | ||
if (accessToken !== JawgJSLoader.instance.accessToken) { | ||
throw new Error('Loader must not be called twice with different access tokens'); | ||
} | ||
return JawgJSLoader.instance; | ||
} | ||
JawgJSLoader.instance = this; | ||
} | ||
|
||
loadJawgPlaces(version = 'latest') { | ||
return load({ lib: 'JawgPlaces', version, accessToken: this.accessToken }); | ||
} | ||
} |