mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2024-12-28 17:21:18 +01:00
Only init websocket if local API
This commit is contained in:
parent
ce39ae723c
commit
fa38667125
3 changed files with 34 additions and 34 deletions
|
@ -29,8 +29,8 @@ export default class App extends React.Component {
|
||||||
onLocalStyleChange: mapStyle => this.onStyleChanged(mapStyle, false)
|
onLocalStyleChange: mapStyle => this.onStyleChanged(mapStyle, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.styleStore.supported(isSupported => {
|
this.styleStore.init(err => {
|
||||||
if(!isSupported) {
|
if(err) {
|
||||||
console.log('Falling back to local storage for storing styles')
|
console.log('Falling back to local storage for storing styles')
|
||||||
this.styleStore = new StyleStore()
|
this.styleStore = new StyleStore()
|
||||||
}
|
}
|
||||||
|
@ -150,11 +150,11 @@ export default class App extends React.Component {
|
||||||
onDataChange: (e) => {
|
onDataChange: (e) => {
|
||||||
this.layerWatcher.analyzeMap(e.map)
|
this.layerWatcher.analyzeMap(e.map)
|
||||||
},
|
},
|
||||||
//TODO: This would actually belong to the layout component
|
//TODO: This would actually belong to the layout component
|
||||||
style:{
|
style:{
|
||||||
top: 40,
|
top: 40,
|
||||||
//left: 500,
|
//left: 500,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const renderer = metadata['maputnik:renderer'] || 'mbgljs'
|
const renderer = metadata['maputnik:renderer'] || 'mbgljs'
|
||||||
|
|
|
@ -7,46 +7,46 @@ const port = '8000'
|
||||||
const localUrl = `http://${host}:${port}`
|
const localUrl = `http://${host}:${port}`
|
||||||
const websocketUrl = `ws://${host}:${port}/ws`
|
const websocketUrl = `ws://${host}:${port}/ws`
|
||||||
|
|
||||||
console.log(localUrl, websocketUrl)
|
|
||||||
|
|
||||||
export class ApiStyleStore {
|
export class ApiStyleStore {
|
||||||
constructor(opts) {
|
constructor(opts) {
|
||||||
if(opts.onLocalStyleChange) {
|
this.onLocalStyleChange = opts.onLocalStyleChange || (() => {})
|
||||||
const connection = new ReconnectingWebSocket(websocketUrl)
|
|
||||||
connection.onmessage = function(e) {
|
|
||||||
if(!e.data) return
|
|
||||||
try {
|
|
||||||
console.log('Received style update from API')
|
|
||||||
const updatedStyle = style.ensureStyleValidity(JSON.parse(e.data))
|
|
||||||
opts.onLocalStyleChange(updatedStyle)
|
|
||||||
} catch(err) {
|
|
||||||
console.error('Cannot parse local file ' + e.data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
supported(cb) {
|
init(cb) {
|
||||||
request(localUrl + '/styles', (error, response, body) => {
|
request(localUrl + '/styles', (error, response, body) => {
|
||||||
cb(error === null)
|
if (!error && body && response.statusCode == 200) {
|
||||||
|
const styleIds = JSON.parse(body)
|
||||||
|
this.latestStyleId = styleIds[0]
|
||||||
|
this.notifyLocalChanges()
|
||||||
|
cb(null)
|
||||||
|
} else {
|
||||||
|
cb(new Error('Can not connect to style API'))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
notifyLocalChanges() {
|
||||||
|
const connection = new ReconnectingWebSocket(websocketUrl)
|
||||||
|
connection.onmessage = function(e) {
|
||||||
|
if(!e.data) return
|
||||||
|
try {
|
||||||
|
console.log('Received style update from API')
|
||||||
|
const updatedStyle = style.ensureStyleValidity(JSON.parse(e.data))
|
||||||
|
this.onLocalStyleChange(updatedStyle)
|
||||||
|
} catch(err) {
|
||||||
|
console.error('Cannot parse local file ' + e.data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
latestStyle(cb) {
|
latestStyle(cb) {
|
||||||
if(this.latestStyleId) {
|
if(this.latestStyleId) {
|
||||||
request(localUrl + '/styles/' + this.latestStyleId, (error, response, body) => {
|
request(localUrl + '/styles/' + this.latestStyleId, (error, response, body) => {
|
||||||
cb(JSON.parse(body))
|
cb(style.ensureStyleValidity(JSON.parse(body)))
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
request(localUrl + '/styles', (error, response, body) => {
|
throw new Error('No latest style available. You need to init the api backend first.')
|
||||||
if (!error && response.statusCode == 200) {
|
|
||||||
const styleIds = JSON.parse(body);
|
|
||||||
this.latestStyleId = styleIds[0];
|
|
||||||
request(localUrl + '/styles/' + this.latestStyleId, (error, response, body) => {
|
|
||||||
cb(style.ensureStyleValidity(JSON.parse(body)))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,8 +69,8 @@ export class StyleStore {
|
||||||
this.mapStyles = loadStoredStyles()
|
this.mapStyles = loadStoredStyles()
|
||||||
}
|
}
|
||||||
|
|
||||||
supported(cb) {
|
init(cb) {
|
||||||
cb(window.localStorage !== undefined)
|
cb(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete entire style history
|
// Delete entire style history
|
||||||
|
|
Loading…
Reference in a new issue