1.4.5.2
This commit is contained in:
63
EdgeNode/internal/js/lib_utils.go
Normal file
63
EdgeNode/internal/js/lib_utils.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build script
|
||||
// +build script
|
||||
|
||||
package js
|
||||
|
||||
var LibraryUtilsJavascript = `
|
||||
let gojs = {}
|
||||
|
||||
gojs.prepareNamespace = function (ns) {
|
||||
let pieces = ns.split(".")
|
||||
let parent = gojs
|
||||
let index = 0
|
||||
pieces.forEach(function (piece) {
|
||||
if (piece == "gojs" && index == 0) {
|
||||
return
|
||||
}
|
||||
if (typeof(parent[piece]) == "undefined") {
|
||||
parent[piece] = {}
|
||||
}
|
||||
parent = parent[piece]
|
||||
index ++
|
||||
})
|
||||
}
|
||||
|
||||
gojs.copyAttrs = function (destObj, srcObj) {
|
||||
if (typeof(srcObj) !== "object") {
|
||||
return
|
||||
}
|
||||
for (let k in srcObj) {
|
||||
if (srcObj.hasOwnProperty(k)) {
|
||||
destObj[k] = srcObj[k]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BigInt.prototype.toJSON = function () {
|
||||
return this.toString()
|
||||
}
|
||||
|
||||
{
|
||||
let callbacks = []
|
||||
gojs.once = function (f) {
|
||||
if (typeof(f) == "function") {
|
||||
callbacks.push(f)
|
||||
}
|
||||
}
|
||||
|
||||
gojs.runOnce = function () {
|
||||
callbacks.forEach(function (f){
|
||||
try {
|
||||
f()
|
||||
} catch(e) {
|
||||
var fString = f.toString()
|
||||
if (fString.length > 100) {
|
||||
fString = fString.substr(0, 100) + "..."
|
||||
}
|
||||
throw new Error(fString + ": " + e.toString())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
`
|
||||
Reference in New Issue
Block a user