Merge branch 'master' into v1

This commit is contained in:
bordun 2020-02-23 16:36:10 +01:00
commit 3ad7915b8f
103 changed files with 94 additions and 59 deletions

212
src/client/ipc/callbacks.js Normal file
View file

@ -0,0 +1,212 @@
var utils = require('../ui/utils'),
osc = require('../osc'),
session = require('../managers/session'),
widgetManager = require('../managers/widgets'),
state = require('../managers/state'),
editor = require('../editor/'),
locales = require('../locales'),
{deepCopy} = require('../utils'),
sidepanel = require('../ui/sidepanel'),
notifications = require('../ui/notifications'),
{TRAVERSING_SAMEWIDGET} = require('../events/utils'),
raw = require('nanohtml/raw'),
ipc = require('./'),
windowTitle = document.title
module.exports = {
bundle: function(data) {
for (let i in data) {
osc.receive(data[i])
}
},
receiveOsc: function(data){
osc.receive(data)
},
connected:function(){
LOADING.close()
},
sessionList: function(data){
session.list(data)
},
sessionOpen: function(data){
session.open(data)
},
sessionNew: function(){
session.create()
},
sessionSaved: function(data){
editor.unsavedSession = false
},
stateLoad: function(data){
state.load(data.state, data.send, data.path)
},
stateSend:function(){
notifications.add({
icon: 'wifi',
message: locales('loading_newclient')
})
setTimeout(function(){
osc.syncOnly = true
state.send()
osc.syncOnly = false
},200)
},
editorDisable: function(data){
editor.disable(data.permanent)
},
error: function(data){
new utils.Popup({title: raw(utils.icon('exclamation-triangle') + '  ' + locales('error')), content: raw(data), closable:true})
},
reloadCss: function(){
var queryString = '?reload=' + Date.now()
DOM.each(document, 'link[rel="stylesheet"][hot-reload]', (el)=>{
el.href = el.href.replace(/\?.*|$/, queryString)
})
setTimeout(()=>{
var root = widgetManager.getWidgetById('root')[0]
if (root) root.onPropChanged('color')
GRIDWIDTH = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--grid-width'))
},200)
},
readOnly: function(){
READ_ONLY = true
$('.editor-menu .btn').remove()
$('.editor-menu .title').addClass('disabled')
if (editor.enabled) editor.disable()
},
reload: function(){
var id = Math.random(),
search = location.search,
query = 'backupId=' + id
// disable editor's warning
window.onbeforeunload = null
if (!session.session) {
window.location.href = window.location.href
return
}
try {
// store session & state backup
ipc.send('storeBackup', {
backupId: id,
session: session.session || {},
sessionPath: session.sessionPath,
state: state.get(),
history: editor.history,
historyState: editor.historyState,
editorEnabled: editor.enabled,
})
// reload page and hold backup id
if (search) {
if (search.includes('backupId=')) {
search = search.replace(/backupId=[^&]*/, query)
} else {
search += '&' + query
}
} else {
search += '?' + query
}
window.location.search = search
} catch(e) {
window.location.href = window.location.href
}
},
loadBackup: function(data) {
session.load(data.session, ()=>{
state.set(data.state, false)
editor.historySession = deepCopy(data.session)
editor.history = data.history
editor.historyState = data.historyState
if (data.editorEnabled) editor.enable()
if (data.sidepanelOpened) sidepanel.open()
if (data.traversing === TRAVERSING_SAMEWIDGET) {
DOM.get('.traversingSmart')[0].click()
} else if (data.traversing) {
DOM.get('.traversingEnable')[0].click()
}
ipc.send('sessionSetPath', {path: data.sessionPath})
session.setSessionPath(data.sessionPath)
ipc.send('deleteBackup', data.backupId)
})
},
notify: function(data) {
var message = data.message || ''
if (data.locale) message = locales(data.locale) + message
notifications.add({
icon: data.icon,
class: data.class,
message: message
})
},
setTitle: function(data) {
document.title = windowTitle + (data ? ' (' + data + ')' : '')
},
serverTargets: function(data) {
if (data) osc.serverTargets = data
}
}

176
src/client/ipc/index.js Normal file
View file

@ -0,0 +1,176 @@
var EventEmitter = require('../events/event-emitter'),
uuid = require('nanoid/generate')('0123456789abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ', 10),
notifications,
locales
setTimeout(()=>{
notifications = require('../ui/notifications')
locales = require('../locales')
})
var reconnectTimeout = 2000,
hearbeatInterval = 5000,
hearbeatTimeout = 3000,
protocol = document.location.protocol === 'https:' ? 'wss://' : 'ws://'
class Ipc extends EventEmitter {
constructor() {
super()
this.socket = null
this.queue = []
this.disconnected = false
this.reconnect = undefined
this.hearbeat = undefined
this.hearbeatTimeout = undefined
this.on('pong', ()=>{
clearTimeout(this.hearbeatTimeout)
})
this.on('ping', ()=>{
this.socket.send('["pong"]')
})
document.cookie = 'client_id=' + uuid
try {
this.open()
} catch(e) {
console.warn('Could not open a WebSocket connection')
console.log(e)
}
}
init() {
var callbacks = require('./callbacks')
for (let i in callbacks) {
let callback = callbacks[i]
this.on(i, (data)=>{
callback(data)
})
}
}
open() {
this.socket = new WebSocket(protocol + window.location.host + '/' + uuid)
this.socket.onclose = this.socket.onerror = ()=>{
if (!this.connected()) this.close()
}
this.socket.onopen = (e)=>{
this.hearbeat = setInterval(()=>{
if (!this.connected()) return
this.socket.send('["ping"]')
this.hearbeatTimeout = setTimeout(()=>{
if (this.connected()) this.close()
}, hearbeatTimeout)
}, hearbeatInterval)
if (notifications && this.disconnected) notifications.add({
icon: 'wifi',
message: locales('server_connected'),
id: 'ipc_state'
})
this.trigger('connect')
this.flush()
this.socket.onmessage = (e)=>{
this.receive(e.data)
}
}
}
close() {
this.socket.onclose = this.socket.onerror = null
this.socket.close()
this.socket = null
clearInterval(this.hearbeat)
clearTimeout(this.hearbeatTimeout)
notifications.add({
icon: 'wifi',
class: 'error',
message: locales('server_disconnected'),
id: 'ipc_state',
duration: Infinity
})
this.disconnected = true
clearTimeout(this.reconnect)
this.reconnect = setTimeout(()=>{
this.open()
}, reconnectTimeout)
}
connected() {
return this.socket && this.socket.readyState == this.socket.OPEN
}
receive(message) {
if (typeof message == 'string') {
var packet = JSON.parse(message)
if (Array.isArray(packet) && typeof packet[0] == 'string') {
this.trigger(packet[0], packet[1])
}
}
}
send(event, data) {
var packet = JSON.stringify([event, data])
if (this.connected()) {
this.socket.send(packet)
} else {
this.queue.push(packet)
}
}
flush(){
for (var i in this.queue) {
if (this.connected()) this.socket.send(this.queue[i])
}
this.queue = []
}
}
var ipc = new Ipc()
module.exports = ipc