refactoring, config stuff basically OK

This commit is contained in:
Helmut Merz 2023-02-27 10:40:10 +01:00
parent 5f793c354e
commit 2dca568ee8
4 changed files with 39 additions and 61 deletions

View file

@ -1,11 +1,18 @@
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 {
return { pollurl: '' }
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 = {
@ -13,17 +20,36 @@ export const pvapp = {
conf: {} as confdata,
data: {},
output: '',
run,
save,
poll
poll,
newdata: '',
mounted(name: string) {
console.log('app mounted: ', name)
}
}
// method definitions
// 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
//this.newdata = ''
console.log('save:', value)
}
function poll() {
dopoll(this)
}
// basic functions
async function dopoll(app: typeof pvapp) {
while (true) {
try {
@ -39,7 +65,3 @@ async function dopoll(app: typeof pvapp) {
}
}
function poll() {
dopoll(this)
}

View file

@ -1,52 +1,5 @@
import { createApp } from './lib/petite-vue.es.js'
import { pageid } from '@params'
console.log("pageid:", pageid)
import { api, polling } from './settings'
import { config, pvapp } from './common'
// pvapp.run(config(api, polling))
const pollUrl = `${api.path}/${polling.msgbase.join('/')}`
console.log("pollUrl:", pollUrl)
const appdata = {
$delimiters: ['{|', '|}'],
urlParams: new URL(location.href).searchParams,
poll,
newdata: '',
data: '',
save,
mounted(name: string) {
console.log('app mounted: ', name)
}
}
createApp(appdata).mount()
appdata.poll()
// method definitions
function save() {
this.data += '\n' + this.newdata
this.newdata = ''
}
async function dopoll(obj: typeof appdata, url: string) {
while (true) {
try {
let res = await fetch(url)
let data = await res.json()
console.log(data)
obj.newdata = data['status']
} catch (error) {
console.log(error)
await new Promise(r => setTimeout(r, 10000))
}
}
}
function poll() {
dopoll(this, pollUrl)
}
pvapp.run(config(api, polling))

View file

@ -9,3 +9,5 @@ export const pageid = 'pageid-dummy'
export const domain = 'domain-dummy'
export const action = 'action-dummy'
export const class_ = 'class-dummy'
export const nopolling = false

View file

@ -1,18 +1,19 @@
<div v-scope>
<input type="text" name="txt" autofocus
v-model="newdata"
@change="save" />
v-model="data.txt"
@vue:mounted="$el.value = ''"
@change="save($el.value)" />
</div>
<div v-scope @vue:mounted="mounted('data')">
<pre v-text="data"></pre>
</div>
<div v-scope>
<pre v-text="newdata" @vue:mounted="mounted('newdata')"></pre>
<div v-scope @vue:mounted="mounted('output')">
<pre v-text="output"></pre>
</div>
<div v-scope>
<div>id = {| urlParams.get('id') |}</div>
<div>id = {| conf.itemid |}</div>
</div>