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

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') }
}
const appdata = {
$delimiters: ['{|', '|}'],
conf: {} as confdata,
data: {},
output: '',
save,
poll,
mounted(name: string) {
console.log('app mounted: ', name)
}
}
export const pvapp = {
run(conf: confdata) {
appdata.conf = conf
createApp(appdata).mount()
if (appdata.conf.pollurl) {
appdata.poll()
}
}
}
// components
function Data() {
return {
data: {},
save
}
}
// appdata 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))
}
}
}