Merge pull request #647 from nofficalfs/PR-InitJsPromise

Add Promise-Async Methods for parts of `lib.init`
This commit is contained in:
Spmario233 2023-11-17 22:42:05 +08:00 committed by GitHub
commit f56a1c2b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 0 deletions

View File

@ -8437,6 +8437,63 @@
} }
}, },
init:{ init:{
/**
* 部分函数的Promise版本
*/
promises:{
/**
* Promise版的`lib.init.js`
*
* @param {string} path - 文件路径
* @param {string | string[]} [file] - 文件名或文件名组忽略则直接读取`path`的内容
* @returns {Promise<Event>}
*/
js:(path,file)=>new Promise((resolve,reject)=>lib.init.js(path,file,resolve,reject)),
/**
* Promise版的`lib.init.css`
*
* @param {string} path - 文件路径
* @param {string | string[]} [file] - 文件名或文件名组忽略则直接读取`path`的内容
* @param {Element} [before] 新样式dom的位置
* @returns {Promise<HTMLLinkElement>}
*/
css:(path,file,before)=>new Promise((resolve,reject)=>{
const style=lib.init.css(path,file,before);
style.addEventListener("load",()=>resolve(style));
style.addEventListener("error",reject);
}),
/**
* Promise版的`lib.init.req`
*
* @param {string} str - 要读取的地址
* @param {string} [master]
* @returns {Promise<ProgressEvent>}
*/
req:(str,master)=>new Promise((resolve,reject)=>lib.init.req(str,resolve,reject,master)),
/**
* Promise版的`lib.init.json`
*
* @param {string} url - 要读取的地址
* @returns {Promise<object>}
*/
json:(url)=>new Promise((resolve,reject)=>lib.init.json(url,resolve,reject)),
/**
* Promise版的`lib.init.sheet`
*
* @returns {Promise<HTMLStyleElement>}
*/
sheet(){
return new Promise((resolve,reject)=>{
const style=lib.init.sheet.apply(lib.init,arguments);
style.addEventListener("load",()=>resolve(style));
style.addEventListener("error",reject);
})
}
},
init:function(){ init:function(){
if(typeof __dirname==='string'&&__dirname.length){ if(typeof __dirname==='string'&&__dirname.length){
var dirsplit=__dirname.split('/'); var dirsplit=__dirname.split('/');