hugo-theme-cyberscopes/assets/js/common.ts

67 lines
1.2 KiB
TypeScript

import { createApp } from './lib/petite-vue.es.js'
import { api, polling } from './settings'
import { pageid } from '@params'
type confdata = {
apiurl: string
pollurl: string
itemid: string
}
export function config (api, polling): confdata {
const pu = `${api.path}/${polling.msgbase.join('/')}`
const urlparams = new URL(location.href).searchParams
return { apiurl: api.path, pollurl: pu, itemid: urlparams.get('id') }
}
export const pvapp = {
$delimiters: ['{|', '|}'],
conf: {} as confdata,
data: {},
output: '',
run,
save,
poll,
newdata: '',
mounted(name: string) {
console.log('app mounted: ', name)
}
}
// pvapp method definitions
function run(conf: confdata) {
this.conf = conf
createApp(this).mount()
if (this.conf.pollurl) {
this.poll()
}
}
function save(value: string) {
this.output += '\n' + value
console.log('save:', value)
}
function poll() {
dopoll(this)
}
// basic functions
async function dopoll(app: typeof pvapp) {
while (true) {
try {
let res = await fetch(pvapp.conf.pollurl)
let msg = await res.json()
console.log(msg)
//app.handle(msg)
//app.newdata = data['status']
} catch (error) {
console.log(error)
await new Promise(r => setTimeout(r, 10000))
}
}
}