move API-specific stuff to separate app-ig.ts file
This commit is contained in:
parent
45e66e5c3f
commit
9afe216068
8 changed files with 79 additions and 86 deletions
71
assets/js/app-ig.ts
Normal file
71
assets/js/app-ig.ts
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
// app-ig.ts - petite-vue application using integrator API
|
||||||
|
|
||||||
|
import { createApp } from 'petite-vue'
|
||||||
|
import { Config, register, handle, sleep } from './common'
|
||||||
|
|
||||||
|
export const pvapp = {
|
||||||
|
run(conf: Config, components: any[]) {
|
||||||
|
appdata.conf = conf
|
||||||
|
for (const comp of components) {
|
||||||
|
appdata[comp.name] = comp
|
||||||
|
}
|
||||||
|
if (appdata.conf.pollurl) {
|
||||||
|
poll(appdata)
|
||||||
|
}
|
||||||
|
createApp(appdata).mount()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const appdata = {
|
||||||
|
$delimiters: ['{|', '|}'],
|
||||||
|
conf: {} as Config,
|
||||||
|
components: {},
|
||||||
|
register,
|
||||||
|
sendMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
// methods
|
||||||
|
|
||||||
|
async function sendMsg(basemsg: string[], data: any) {
|
||||||
|
const url = `${this.conf.apiurl}/${basemsg.join('/')}`
|
||||||
|
await send(url, this.conf, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// functions
|
||||||
|
|
||||||
|
async function send(url: string, conf: Config, data: any) {
|
||||||
|
data._interaction = conf.iid
|
||||||
|
const body = JSON.stringify(data)
|
||||||
|
const headers = {}
|
||||||
|
headers['X-Integrator-Session'] = conf.sid
|
||||||
|
return fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: headers,
|
||||||
|
body: body
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function poll(app: typeof appdata) {
|
||||||
|
const wait_time = 10000
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
const res = await(send(app.conf.pollurl, app.conf, {}))
|
||||||
|
const msg = await res.json()
|
||||||
|
switch (msg.status) {
|
||||||
|
case 'idle':
|
||||||
|
break
|
||||||
|
case 'data':
|
||||||
|
console.log('--- poll', msg)
|
||||||
|
handle(app, msg)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
console.log('**** poll', msg)
|
||||||
|
await sleep(wait_time)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
await sleep(wait_time)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
// common.ts - common stuff like config and app setup
|
// common.ts - common stuff like config and app setup
|
||||||
|
|
||||||
import { createApp } from 'petite-vue'
|
|
||||||
import { Data } from './comp-data'
|
|
||||||
|
|
||||||
import { domain } from '@params'
|
import { domain } from '@params'
|
||||||
|
|
||||||
type Config = {
|
export type Config = {
|
||||||
domain: string
|
domain: string
|
||||||
itemid: string
|
itemid: string
|
||||||
sid: string
|
sid: string
|
||||||
|
@ -26,43 +23,15 @@ export function config (api): Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const pvapp = {
|
|
||||||
run(conf: Config, components: any[]) {
|
|
||||||
appdata.conf = conf
|
|
||||||
for (const comp of components) {
|
|
||||||
appdata[comp.name] = comp
|
|
||||||
}
|
|
||||||
if (appdata.conf.pollurl) {
|
|
||||||
poll(appdata)
|
|
||||||
}
|
|
||||||
createApp(appdata).mount()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const appdata = {
|
|
||||||
$delimiters: ['{|', '|}'],
|
|
||||||
conf: {} as Config,
|
|
||||||
components: {},
|
|
||||||
register,
|
|
||||||
sendMsg
|
|
||||||
}
|
|
||||||
|
|
||||||
// generic App (appdata) methods
|
// generic App (appdata) methods
|
||||||
|
|
||||||
function register(name: string, comp: any) {
|
export function register(name: string, comp: any) {
|
||||||
this.components[name] = comp
|
this.components[name] = comp
|
||||||
}
|
}
|
||||||
|
|
||||||
// specific App (appdata) methods
|
|
||||||
|
|
||||||
export async function sendMsg(basemsg: string[], data: any) {
|
|
||||||
const url = `${this.conf.apiurl}/${basemsg.join('/')}`
|
|
||||||
await send(url, this.conf, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// common functions
|
// common functions
|
||||||
|
|
||||||
function handle(app, msg) {
|
export function handle(app, msg) {
|
||||||
const [ domain, action, class_, item ] = msg.path.split('/')
|
const [ domain, action, class_, item ] = msg.path.split('/')
|
||||||
const comp = app.components[class_ || 'data']
|
const comp = app.components[class_ || 'data']
|
||||||
if (domain && domain != comp.domain) {
|
if (domain && domain != comp.domain) {
|
||||||
|
@ -73,20 +42,6 @@ function handle(app, msg) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// app-specific functions
|
|
||||||
|
|
||||||
async function send(url: string, conf: Config, data: any) {
|
|
||||||
data._interaction = conf.iid
|
|
||||||
const body = JSON.stringify(data)
|
|
||||||
const headers = {}
|
|
||||||
headers['X-Integrator-Session'] = conf.sid
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: headers,
|
|
||||||
body: body
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function createRandString(size: number): string {
|
function createRandString(size: number): string {
|
||||||
const arr = new Uint32Array(size)
|
const arr = new Uint32Array(size)
|
||||||
crypto.getRandomValues(arr)
|
crypto.getRandomValues(arr)
|
||||||
|
@ -109,28 +64,4 @@ function getSid(): string {
|
||||||
// TODO: clear sid - when?
|
// TODO: clear sid - when?
|
||||||
//localStorage.setItem('api.sessionid', '')
|
//localStorage.setItem('api.sessionid', '')
|
||||||
|
|
||||||
async function poll(app: typeof appdata) {
|
export const sleep = (delay: number) => new Promise(r => setTimeout(r, delay))
|
||||||
const wait_time = 10000
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
const res = await(send(app.conf.pollurl, app.conf, {}))
|
|
||||||
const msg = await res.json()
|
|
||||||
switch (msg.status) {
|
|
||||||
case 'idle':
|
|
||||||
break
|
|
||||||
case 'data':
|
|
||||||
console.log('--- poll', msg)
|
|
||||||
handle(app, msg)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
console.log('**** poll', msg)
|
|
||||||
await sleep(wait_time)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error)
|
|
||||||
await sleep(wait_time)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const sleep = (delay: number) => new Promise(r => setTimeout(r, delay))
|
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
// comp-data.ts - definition and methods of the Data component
|
// comp-data.ts - definition and methods of the Data component
|
||||||
|
|
||||||
import { sendMsg } from './common'
|
|
||||||
|
|
||||||
// Data component
|
|
||||||
|
|
||||||
//export function Data(name, action, domain: string): object {
|
|
||||||
export function Data(name: string, conf: any): object {
|
export function Data(name: string, conf: any): object {
|
||||||
const domain = conf.domain || this.conf.domain
|
const domain = conf.domain || this.conf.domain
|
||||||
const comp = {
|
const comp = {
|
||||||
|
@ -51,7 +46,6 @@ function exec(action: string) {
|
||||||
for (const k of Object.keys(data)) {
|
for (const k of Object.keys(data)) {
|
||||||
value += `${k}: ${data[k]}, `
|
value += `${k}: ${data[k]}, `
|
||||||
}
|
}
|
||||||
this.output += '\n' + value
|
|
||||||
console.log('exec:', value)
|
console.log('exec:', value)
|
||||||
const msgbase = [this.domain, action, this.name]
|
const msgbase = [this.domain, action, this.name]
|
||||||
if (this.state.mode !== 'new') {
|
if (this.state.mode !== 'new') {
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
// list-data.ts - definition and methods of the List component
|
// list-data.ts - definition and methods of the List component
|
||||||
|
|
||||||
import { sendMsg } from './common'
|
|
||||||
|
|
||||||
// List component
|
|
||||||
|
|
||||||
//export function List(name, action, domain: string): object {
|
|
||||||
export function List(name: string, conf: any): object {
|
export function List(name: string, conf: any): object {
|
||||||
const domain = conf.domain || this.conf.domain
|
const domain = conf.domain || this.conf.domain
|
||||||
const comp = {
|
const comp = {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { api } from './settings'
|
import { api } from './settings'
|
||||||
import { config, pvapp } from './common'
|
import { pvapp } from './app-ig'
|
||||||
|
|
||||||
|
import { config } from './common'
|
||||||
import { Data } from './comp-data'
|
import { Data } from './comp-data'
|
||||||
import { List } from './comp-list'
|
import { List } from './comp-list'
|
||||||
|
|
|
@ -27,7 +27,7 @@ params:
|
||||||
|
|
||||||
api:
|
api:
|
||||||
domain: demo
|
domain: demo
|
||||||
module: main.ts
|
module: main-ig.ts
|
||||||
|
|
||||||
taxonomies:
|
taxonomies:
|
||||||
domain: domains
|
domain: domains
|
||||||
|
|
Loading…
Add table
Reference in a new issue