64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
// 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())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
`
|