feat: js proxy

v3
veypi 1 year ago
parent 7a2ef864a3
commit 62711e1ff2

@ -5,7 +5,8 @@
* Distributed under terms of the MIT license. * Distributed under terms of the MIT license.
*/ */
let cfg = { import proxy from './components/proxy'
let cfg = proxy.Watch({
uuid: '', uuid: '',
refresh: '', refresh: '',
token: '', token: '',
@ -57,7 +58,7 @@ let cfg = {
appFileUrl() { appFileUrl() {
return `/fs/a/${this.uuid}/` return `/fs/a/${this.uuid}/`
}, },
} })
export default cfg export default cfg

@ -7,10 +7,7 @@
import b from './build' import b from './build'
import cfg from '../cfg' import cfg from '../cfg'
export default class { export default b('voa-account', [
main: HTMLElement
constructor() {
this.main = b('voa-account', [
b('voa-account-header', [b('voa-ah-1', '我的账户'), b('voa-ah-2')]), b('voa-account-header', [b('voa-ah-1', '我的账户'), b('voa-ah-2')]),
b('voa-account-body', [ b('voa-account-body', [
b('voa-ab-ico', [b('img', '', (d) => { d.setAttribute('src', cfg.user.icon) })]), b('voa-ab-ico', [b('img', '', (d) => { d.setAttribute('src', cfg.user.icon) })]),
@ -21,6 +18,4 @@ export default class {
b('voa-abi-4', [b('span', '手机:'), b('span', '', (d) => { d.innerHTML = cfg.user.phone })]), b('voa-abi-4', [b('span', '手机:'), b('span', '', (d) => { d.innerHTML = cfg.user.phone })]),
]) ])
]) ])
]) ])
}
}

@ -5,6 +5,8 @@
* Distributed under terms of the GPL license. * Distributed under terms of the GPL license.
*/ */
import proxy from "./proxy"
interface buildOpts { interface buildOpts {
id?: string id?: string
@ -18,13 +20,6 @@ interface buildOpts {
} }
const typs = ['div', 'img', 'span', 'p', 'a'] const typs = ['div', 'img', 'span', 'p', 'a']
const caches: (() => void)[] = []
function update() {
for (let c of caches) {
c()
}
}
export { update }
export default (opts: buildOpts | string, inner?: string | HTMLElement[], updator?: (p: HTMLElement) => void) => { export default (opts: buildOpts | string, inner?: string | HTMLElement[], updator?: (p: HTMLElement) => void) => {
if (typeof opts === 'string') { if (typeof opts === 'string') {
@ -68,16 +63,14 @@ export default (opts: buildOpts | string, inner?: string | HTMLElement[], updato
while ((match = regex.exec(opts.style)) !== null) { while ((match = regex.exec(opts.style)) !== null) {
const key = match[1].trim(); const key = match[1].trim();
const value = match[2].trim(); const value = match[2].trim();
console.log([key, value])
dom.style.setProperty(key, value) dom.style.setProperty(key, value)
} }
} }
dom.setAttribute('voa', '1') dom.setAttribute('voa', '1')
if (updator) { if (updator) {
caches.push(() => { proxy.Listen(() => {
updator(dom) updator(dom)
}) })
updator(dom)
} }
return dom return dom
} }

@ -0,0 +1,73 @@
/*
* proxy.ts
* Copyright (C) 2024 veypi <i@veypi.com>
* 2024-10-23 17:20
* Distributed under terms of the GPL license.
*/
type voidFn = () => void
const callbackCache: voidFn[] = []
function ForceUpdate() {
for (let c of callbackCache) {
c()
}
}
var listen_tag = -1
function Listen(callback: voidFn) {
if (listen_tag >= 0) {
console.warn('it shuold not happen')
return
}
listen_tag = callbackCache.length
callbackCache.push(callback)
callback()
listen_tag = -1
}
function Watch<T extends Object>(target: T) {
const listeners: { [key: string]: number[] } = {}
const handler = {
get(target: Object, key: string, receiver: any) {
console.log(`get ${key} ${listen_tag}`)
const value = Reflect.get(target, key, receiver);
if (typeof value === 'object' && value !== null) {
return new Proxy(value, handler);
}
if (listen_tag >= 0) {
if (!listeners[key]) {
listeners[key] = []
}
listeners[key].push(listen_tag)
}
return value;
},
set(target: Object, key: string, newValue: any, receiver: any) {
console.log(`set ${key} ${newValue}`)
const result = Reflect.set(target, key, newValue, receiver);
if (result) {
if (listeners[key]) {
console.log(listeners[key])
for (let cb of listeners[key]) {
callbackCache[cb]()
}
}
}
return result;
},
deleteProperty(target: Object, key: string) {
console.log(`del ${key}`)
const result = Reflect.deleteProperty(target, key);
if (result) {
}
return result
}
};
return new Proxy<T>(target, handler);
}
export default { Watch, Listen, ForceUpdate }

@ -7,6 +7,7 @@
import b from './build' import b from './build'
import bus from '../bus' import bus from '../bus'
import account from './account' import account from './account'
import cfg from '../cfg'
/* /*
mask mask
@ -38,9 +39,13 @@ export default class {
this.main = b({ this.main = b({
class: 'voa-slide-main', class: 'voa-slide-main',
children: [ children: [
new account().main, account,
b({ class: 'voa-sm-separate' }) b({ class: 'voa-sm-separate' })
] ],
onclick: () => {
console.log(cfg)
cfg.user.phone = new Date().toLocaleString()
}
}) })
this.body = b({ this.body = b({
class: 'voa-slide-body voa-animate-slow', class: 'voa-slide-body voa-animate-slow',

Loading…
Cancel
Save