88 lines
1.6 KiB
TypeScript
88 lines
1.6 KiB
TypeScript
import { createApp } from 'petite-vue'
|
|
|
|
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 = polling ? `${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 = {
|
|
run(conf: confdata) {
|
|
appdata.conf = conf
|
|
createApp(appdata).mount()
|
|
if (appdata.conf.pollurl) {
|
|
appdata.poll()
|
|
}
|
|
}
|
|
}
|
|
|
|
const appdata = {
|
|
$delimiters: ['{|', '|}'],
|
|
conf: {} as confdata,
|
|
output: '',
|
|
poll,
|
|
mounted(name: string) {
|
|
console.log('app mounted: ', name)
|
|
},
|
|
Data
|
|
}
|
|
|
|
|
|
// components
|
|
|
|
function Data() {
|
|
return {
|
|
data: {},
|
|
save
|
|
}
|
|
}
|
|
|
|
// appdata and component method definitions
|
|
|
|
function save() {
|
|
let value = ''
|
|
for (const k of Object.keys(this.data)) {
|
|
value += `${k}: ${this.data[k]}, `
|
|
}
|
|
this.output += '\n' + value
|
|
console.log('save:', value)
|
|
}
|
|
|
|
function poll() {
|
|
dopoll(this)
|
|
}
|
|
|
|
// basic functions - move to api.ts
|
|
|
|
function createSid(): string {
|
|
const arr = new Uint32Array(2)
|
|
crypto.getRandomValues(arr)
|
|
return arr[0].toString(36) + arr[1].toString(36)
|
|
}
|
|
|
|
console.log("sid: ", createSid())
|
|
|
|
async function dopoll(app: typeof appdata) {
|
|
while (true) {
|
|
try {
|
|
let res = await fetch(app.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))
|
|
}
|
|
}
|
|
}
|
|
|