Improve core API:

- improve OS exit API
- improve VFS API
This commit is contained in:
lxsang 2021-10-12 21:58:57 +02:00
parent e6e727246d
commit c23cb1bfa8
6 changed files with 67 additions and 103 deletions

View File

@ -5,7 +5,7 @@ DOCDIR?=/opt/www/htdocs/doc/antos
BLUE=\033[1;34m
NC=\033[0m
VERSION=1.2.0
VERSION=1.2.1
GSED=sed
UNAME_S := $(shell uname -s)

4
d.ts/antos.d.ts vendored
View File

@ -2445,7 +2445,7 @@ declare namespace OS {
* to perform a particular task before shuting down the system
*/
const cleanupHandles: {
[index: string]: () => void;
[index: string]: () => Promise<any>;
};
/**
* Perform the system shutdown operation. This function calls all
@ -2463,7 +2463,7 @@ declare namespace OS {
* @param {() => void} f the callback handle
* @returns
*/
function onexit(n: string, f: () => void): () => void;
function onexit(n: string, f: () => Promise<any>): () => Promise<any>;
/**
* The namespace API is dedicated to the definition of the core system APIs
* used by AntOS and its applications. The following core APIs are defined:

BIN
release/antos-1.2.1.tar.gz Normal file

Binary file not shown.

View File

@ -1 +1 @@
1.2.0
1.2.1

View File

@ -843,7 +843,7 @@ namespace OS {
* exits. These callbacks are useful when an application or service wants
* to perform a particular task before shuting down the system
*/
export const cleanupHandles: { [index: string]: () => void } = {};
export const cleanupHandles: { [index: string]: () => Promise<any> } = {};
/**
* Perform the system shutdown operation. This function calls all
@ -854,15 +854,16 @@ namespace OS {
*/
export function exit(): void {
//do clean up first
const promises: Promise<any>[] = [];
for (let n in cleanupHandles) {
const f = cleanupHandles[n];
f();
promises.push(cleanupHandles[n]());
}
API.handle
.setting()
.then(function (r: any) {
promises.push(API.handle.setting());
Promise.all(promises)
.then(async function (r: any) {
cleanup();
return API.handle.logout().then((d: any) => boot());
const d = await API.handle.logout();
return boot();
})
.catch((e: Error) => console.error(e));
}
@ -875,7 +876,7 @@ namespace OS {
* @param {() => void} f the callback handle
* @returns
*/
export function onexit(n: string, f: () => void) {
export function onexit(n: string, f: () => Promise<any>) {
if (!cleanupHandles[n]) {
return (cleanupHandles[n] = f);
}

View File

@ -355,7 +355,7 @@ namespace OS {
if (re === "") {
return;
}
this.genealogy = re.split("/").filter(s=> s!="");
this.genealogy = re.split("/").filter(s => s != "");
this.path = `${this.protocol}://${this.genealogy.join("/")}`;
if (!this.isRoot()) {
this.basename = this.genealogy[
@ -548,12 +548,8 @@ namespace OS {
return new Promise(async (resolve, reject) => {
try {
const r = await this.onready();
try {
const d = await this._rd(t);
return resolve(d);
} catch (e) {
return reject(__e(e));
}
const d = await this._rd(t);
resolve(d);
} catch (e_1) {
return reject(__e(e_1));
}
@ -601,16 +597,12 @@ namespace OS {
return new Promise(async (resolve, reject) => {
try {
const r = await this.onready();
try {
const d_1 = await this._mk(d);
announcer.ostrigger("VFS", {
m: "mk",
file: this,
});
return resolve(d_1);
} catch (e) {
return reject(__e(e));
}
const d_1 = await this._mk(d);
announcer.ostrigger("VFS", {
m: "mk",
file: this,
});
return resolve(d_1);
} catch (e_1) {
return reject(__e(e_1));
}
@ -629,16 +621,12 @@ namespace OS {
return new Promise(async (resolve, reject) => {
try {
const r = await this.onready();
try {
const d = await this._rm();
announcer.ostrigger("VFS", {
m: "remove",
file: this,
});
return resolve(d);
} catch (e) {
return reject(__e(e));
}
const d = await this._rm();
announcer.ostrigger("VFS", {
m: "remove",
file: this,
});
return resolve(d);
} catch (e_1) {
return reject(__e(e_1));
}
@ -659,16 +647,12 @@ namespace OS {
return new Promise(async (resolve, reject) => {
try {
const r = await this.onready();
try {
const d = await this._up();
announcer.ostrigger("VFS", {
m: "upload",
file: this,
});
return resolve(d);
} catch (e) {
return reject(__e(e));
}
const d = await this._up();
announcer.ostrigger("VFS", {
m: "upload",
file: this,
});
return resolve(d);
} catch (e_1) {
return reject(__e(e_1));
}
@ -689,16 +673,12 @@ namespace OS {
return new Promise(async (resolve, reject) => {
try {
const r = await this.onready();
try {
const d = await this._pub();
announcer.ostrigger("VFS", {
m: "publish",
file: this,
});
return resolve(d);
} catch (e) {
return reject(__e(e));
}
const d = await this._pub();
announcer.ostrigger("VFS", {
m: "publish",
file: this,
});
return resolve(d);
} catch (e_1) {
return reject(__e(e_1));
}
@ -719,16 +699,12 @@ namespace OS {
return new Promise(async (resolve, reject) => {
try {
const r = await this.onready();
try {
const d = await this._down();
announcer.ostrigger("VFS", {
m: "download",
file: this,
});
return resolve(d);
} catch (e) {
return reject(__e(e));
}
const d = await this._down();
announcer.ostrigger("VFS", {
m: "download",
file: this,
});
return resolve(d);
} catch (e_1) {
return reject(__e(e_1));
}
@ -748,16 +724,13 @@ namespace OS {
return new Promise(async (resolve, reject) => {
try {
const r = await this.onready();
try {
const data = await this._mv(d);
announcer.ostrigger("VFS", {
m: "move",
file: d.asFileHandle(),
});
return resolve(data);
} catch (e) {
return reject(__e(e));
}
const data = await this._mv(d);
announcer.ostrigger("VFS", {
m: "move",
file: d.asFileHandle(),
});
return resolve(data);
} catch (e_1) {
return reject(__e(e_1));
}
@ -778,16 +751,12 @@ namespace OS {
return new Promise(async (resolve, reject) => {
try {
const r = await this.onready();
try {
const d = await this._exec();
announcer.ostrigger("VFS", {
m: "execute",
file: this,
});
return resolve(d);
} catch (e) {
return reject(__e(e));
}
const d = await this._exec();
announcer.ostrigger("VFS", {
m: "execute",
file: this,
});
return resolve(d);
} catch (e_1) {
return reject(__e(e_1));
}
@ -1066,8 +1035,8 @@ namespace OS {
protected _wr(t: string): Promise<RequestResult> {
// t is base64 or undefined
return new Promise(async (resolve, reject) => {
if (t === "base64") {
try {
try {
if (t === "base64") {
const d = await API.handle.write(
this.path,
this.cache
@ -1080,11 +1049,7 @@ namespace OS {
);
}
return resolve(d);
} catch (e) {
return reject(__e(e));
}
} else {
try {
} else {
const r = await this.b64(t);
const result = await API.handle.write(
this.path,
@ -1102,9 +1067,9 @@ namespace OS {
);
}
return resolve(result);
} catch (e_2) {
return reject(__e(e_2));
}
} catch (e) {
return reject(__e(e));
}
});
}
@ -1945,14 +1910,12 @@ namespace OS {
try {
await API.requires("os://scripts/jszip.min.js");
const zip = new JSZip();
const fhd = src.asFileHandle();
const fhd = src.asFileHandle();
const meta = await fhd.onready();
if(meta.type === "file")
{
if (meta.type === "file") {
await aradd([src], zip, "/");
}
else
{
else {
const ret = await fhd.read();
await aradd(
ret.result.map((v: API.FileInfoType) => v.path), zip, "/");