/**
 * 异步Computed
 * @param compute 异步计算函数
 * @param initValue 初始值
 * @returns Ref 一个只读的异步计算Ref
 */
export function asyncComputed(compute,initValue){
    const value = shallowRef(initValue);
    watchEffect(async ()=>{
        console.log('trigger update')
        value.value = await compute();
    })
    return readonly(value);
}
const asyncValue = asyncComputed(()=>{
    return new Promise((resolve)=>{
        setTimeout(()=>{
            resolve(1)
        },2000})
})

//输出undefined
console.log(asyncValue.value);
//2秒后变成1

 

//?? 等价于 isset
$a = '';
$b = isset($a) ? $a : 'hahha';
$b = $a ?? 'hahha';
//$b is ''
//适用于多层级array的属性读取
$arr = [ 'son'=>['name'=>'child'] ];
echo $arr['son']['age'] ?? '19';
//output: 19
echo $arr['non']['name'] ?? 'non child';
//output: non child

//?: 等价于empty
$c = empty($a) ? 'hahaha' : $a;
$c = $a ?: 'hahaha';
//c is 'hahaha'

 

在vue3 async setup中 expose 和一些生命周期钩子的注册需要在第一个await 之前,但是也有在await之后调用的方法。

示例如下:

async setup(props,{expose}){
	const instance = getCurrentInstance();
	const exposeObj = {};
	//先把这个空对象expose出去
	expose(exposeObj);
	//模拟异步操作
	const result = await new Promise((resolve)=>{
		setTimeout(()=>resolve(2),2000);
	});
	//注册生命周期钩子时,第二个参数传入instance
	onMounted(()=>{
			console.log('我挂载啦!')
	},instance)

	onUnmounted(()=>{
		console.log('我卸载啦!')
	},instance)
	//后续再添加expose的属性和方法
	exposeObj.attr = result ;
	exposeObj.func = function(){
		console.log('some exposed function.',result)
	}
	
}

 

 只用JGit把项目中更新的文件,打包成一个zip压缩包,上传到服务器上面解压即可完成更新。

首先maven导入需要的JGit库

<!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
<dependency>
    <groupId>org.eclipse.jgit</groupId>
    <artifactId>org.eclipse.jgit</artifactId>
    <version>5.12.0.202106070339-r</version>
</dependency>

 制作zip补丁包代码如下,实际使用可选择实际的提交版本作为原始版本。


import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.StreamSupport;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {

    public static void main(String[] args) throws IOException, GitAPIException {
        Path root = Path.of("这里更改为项目目录");//项目文件目录
        Path patchFile = Path.of("这里更改为patch.zip输出路径");//输出的补丁文件路径
        Git git = Git.open(root.toFile());
        Repository repo = git.getRepository();
        ObjectReader reader = repo.newObjectReader();
        //获取最近10次提交的变更
        List<RevCommit> commitList =
        StreamSupport.stream(git.log().setMaxCount(10).call().spliterator(),false).toList();
        //这里获取第10次为要制作补丁的版本
        RevCommit oldVersion = commitList.get(commitList.size()-1);
        //获取最近一次变更提交作为更新目标版本
        RevCommit newVersion = commitList.get(0);
        //制作补丁文件
        makePatch(git,oldVersion,newVersion,root,patchFile);
    }

    public static AbstractTreeIterator createIterator(ObjectReader reader, RevCommit version) throws IOException {
        CanonicalTreeParser parser = new CanonicalTreeParser();
        parser.reset(reader, version.getTree());
        return parser;
    }

    public static void makePatch(Git git,RevCommit oldVersion,RevCommit newVersion,Path root,Path patchFile) throws IOException, GitAPIException {
        ObjectReader reader = git.getRepository().newObjectReader();
        ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(patchFile));
        git.diff().setOldTree(createIterator(reader,oldVersion)).setNewTree(createIterator(reader,newVersion)).call().
                stream().filter(d-> d.getChangeType() != DiffEntry.ChangeType.DELETE).map(DiffEntry::getNewPath)
                .forEach(p->{
                    Path path = root.resolve(p);
                    ZipEntry e = new ZipEntry(p);
                    System.out.println("add file: "+e);
                    try {
                        e.setLastModifiedTime(Files.getLastModifiedTime(path));
                        zipOut.putNextEntry(e);
                        Files.copy(path,zipOut);
                        zipOut.closeEntry();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                });
        //添加更新日志
        zipOut.putNextEntry(new ZipEntry("update.txt"));
        String updateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(newVersion.getCommitTime()), ZoneId.systemDefault()).toString();
        zipOut.write("""
                更新日期:$date
                更新版本:$version
                """.replaceAll("\\$date", updateTime).replaceAll("\\$version",newVersion.getId().getName()).getBytes(StandardCharsets.UTF_8));
        zipOut.close();
        System.out.println("Make patch done:"+patchFile);
    }

}