native对象及属性改为只读

master
yutent 2023-09-12 14:28:04 +08:00
parent 5da22eed8e
commit b398551deb
1 changed files with 454 additions and 439 deletions

View File

@ -3,7 +3,7 @@
* @author yutent<yutent.io@gmail.com>
* @date 2023/07/21 17:38:11
*/
!(function () {
const MIME_TYPES = {
html: 'text/html',
json: 'application/json',
@ -48,6 +48,8 @@ const KEYS_MAP = {
const NO_CALLBACK = false
const CALL_ONCE = true
const __events__ = Symbol('events')
function defer() {
let obj = {}
obj.promise = new Promise((resolve, reject) => {
@ -112,6 +114,12 @@ function readonly(obj, key, value) {
})
}
function _extend(origin, options = {}) {
for (let k in options) {
readonly(origin, k, options[k])
}
}
class NativeImage {
#origin
@ -165,15 +173,15 @@ class NativeImage {
}
}
class EventEmitter {
class Native {
//
__events__ = Object.create(null)
[__events__] = Object.create(null)
$on(name, fn) {
if (this.__events__[name]) {
this.__events__[name].push(fn)
if (this[__events__][name]) {
this[__events__][name].push(fn)
} else {
this.__events__[name] = [fn]
this[__events__][name] = [fn]
}
}
@ -183,18 +191,20 @@ class EventEmitter {
}
$off(name, fn) {
if (this.__events__[name]) {
if (this[__events__][name]) {
if (fn) {
this.__events__[name] = this.__events__[name].filter(it => it !== fn)
this[__events__][name] = this[__events__][name].filter(
it => it !== fn
)
} else {
this.__events__[name] = []
this[__events__][name] = []
}
}
}
$emit(name, ...args) {
if (this.__events__[name]) {
for (let fn of this.__events__[name]) {
if (this[__events__][name]) {
for (let fn of this[__events__][name]) {
try {
fn.apply(this, args)
if (fn.__once__) {
@ -208,15 +218,15 @@ class EventEmitter {
}
$destroy() {
this.__events__ = Object.create(null)
this[__events__] = Object.create(null)
}
}
readonly(window, 'native', new EventEmitter())
readonly(window, 'native', new Native())
native.$on('opener_message', (data, uuid) => _postMessage(data, uuid))
Object.assign(native, {
_extend(native, {
env: '{{env}}',
app: {
name: '{{app_name}}',
@ -440,7 +450,11 @@ Object.assign(native, {
handler('window', { action: 'move', value: { x, y } }, NO_CALLBACK)
},
setOpacity(opacity = 1) {
handler('window', { action: 'set_opacity', value: opacity }, NO_CALLBACK)
handler(
'window',
{ action: 'set_opacity', value: opacity },
NO_CALLBACK
)
},
alwayOnTop(setting = true) {
handler(
@ -488,3 +502,4 @@ Object.assign(native, {
handler
})
})()