From 5101afe1b9d5da45a38a2f1f3bdfc71ebbd28a93 Mon Sep 17 00:00:00 2001 From: DanyLE Date: Tue, 31 Jan 2023 20:34:07 +0100 Subject: [PATCH] allow to specify user data in some low level VFS interface API --- d.ts/antos.d.ts | 25 ++++++++++---------- src/core/vfs.ts | 62 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 31 deletions(-) diff --git a/d.ts/antos.d.ts b/d.ts/antos.d.ts index f9058b6..73a82a3 100644 --- a/d.ts/antos.d.ts +++ b/d.ts/antos.d.ts @@ -3634,12 +3634,13 @@ interface HTMLElement { * element * * This will trigger the `dragging` and `drop` event on the enabled - * on the element when the mouse is down, then move + * element when the mouse is down, move, then up, then move * * The event can be listened using the traditional way, * Example: * ``` * elem.addEventListener('dragging', (e) => { }, false); + * elem.addEventListener('drop', (e) => { }, false); * ``` * * @meberof HTMLElement @@ -9718,17 +9719,18 @@ declare namespace OS { * will return the meta-data of all files inside of the directory. * Otherwise, file content will be returned * - * @param {string} t data type + * @param {any} formal t data type * - jsonp: the response is an json object * - script: the response is a javascript code * - xml, html: the response is a XML/HTML object * - text: plain text * - binary + * - other user case may be user specific data * * @returns {Promise} a promise on the file content * @memberof BaseFileHandle */ - read(t?: string): Promise; + read(t?: any): Promise; /** * Write the file cache to the actual file * @@ -9757,11 +9759,11 @@ declare namespace OS { * Delete the file * * This function calls the [[_rm]] function to perform the operation - * + * @param {any} d user data * @returns {Promise} promise on the operation result * @memberof BaseFileHandle */ - remove(): Promise; + remove(data?: any): Promise; /** * Upload a file to the current directory * @@ -9848,11 +9850,11 @@ declare namespace OS { * that supports the operation * * @protected - * @param {string} t data type, see [[read]] + * @param {any} t data type, see [[read]] * @returns {Promise} * @memberof BaseFileHandle */ - protected _rd(t: string): Promise; + protected _rd(t: any): Promise; /** * Low level protocol-specific write operation * @@ -9861,11 +9863,10 @@ declare namespace OS { * * @protected * @param {string} t data type, see [[write]] - * @param {*} [d] * @returns {Promise} * @memberof BaseFileHandle */ - protected _wr(t: string, d?: any): Promise; + protected _wr(t: string): Promise; /** * Low level protocol-specific sub-directory creation * @@ -9884,10 +9885,11 @@ declare namespace OS { * This function should be overridden by the file handle class * that supports the operation * + * @param {*} [d] any user data * @returns {Promise} * @memberof BaseFileHandle */ - protected _rm(): Promise; + protected _rm(d: any): Promise; /** * Low level protocol-specific move operation * @@ -10301,11 +10303,10 @@ declare namespace OS { * * @protected * @param {string} t data type, see [[write]] - * @param {string} d file data * @returns {Promise} * @memberof SharedFileHandle */ - protected _wr(t: string, d: string): Promise; + protected _wr(t: string): Promise; /** * Un-publish the file * diff --git a/src/core/vfs.ts b/src/core/vfs.ts index 656b22f..bae1f50 100644 --- a/src/core/vfs.ts +++ b/src/core/vfs.ts @@ -575,17 +575,18 @@ namespace OS { * will return the meta-data of all files inside of the directory. * Otherwise, file content will be returned * - * @param {string} t data type + * @param {any} formal t data type * - jsonp: the response is an json object * - script: the response is a javascript code * - xml, html: the response is a XML/HTML object * - text: plain text * - binary + * - other user case may be user specific data * * @returns {Promise} a promise on the file content * @memberof BaseFileHandle */ - read(t?: string): Promise { + read(t?: any): Promise { return new Promise(async (resolve, reject) => { try { const r = await this.onready(); @@ -648,15 +649,15 @@ namespace OS { * Delete the file * * This function calls the [[_rm]] function to perform the operation - * + * @param {any} d user data * @returns {Promise} promise on the operation result * @memberof BaseFileHandle */ - remove(): Promise { + remove(data?: any): Promise { return new Promise(async (resolve, reject) => { try { const r = await this.onready(); - const d = await this._rm(); + const d = await this._rm(data); announcer.ostrigger("VFS", "remove",this); return resolve(d); } catch (e_1) { @@ -830,11 +831,11 @@ namespace OS { * that supports the operation * * @protected - * @param {string} t data type, see [[read]] + * @param {any} t data type, see [[read]] * @returns {Promise} * @memberof BaseFileHandle */ - protected _rd(t: string): Promise { + protected _rd(t: any): Promise { return this.unsupported("read"); } @@ -846,11 +847,10 @@ namespace OS { * * @protected * @param {string} t data type, see [[write]] - * @param {*} [d] * @returns {Promise} * @memberof BaseFileHandle */ - protected _wr(t: string, d?: any): Promise { + protected _wr(t: string): Promise { return this.unsupported("write"); } @@ -874,10 +874,11 @@ namespace OS { * This function should be overridden by the file handle class * that supports the operation * + * @param {*} [d] any user data * @returns {Promise} * @memberof BaseFileHandle */ - protected _rm(): Promise { + protected _rm(d: any): Promise { return this.unsupported("remove"); } @@ -1875,22 +1876,45 @@ namespace OS { * * @protected * @param {string} t data type, see [[write]] - * @param {string} d file data * @returns {Promise} * @memberof SharedFileHandle */ - protected _wr(t: string, d: string): Promise { + protected _wr(t: string): Promise { return new Promise(async (resolve, reject) => { try { - const r = await API.handle.write(this.path, d); - if (r.error) { - return reject( - API.throwe( - __("{0}: {1}", r.error, this.path) - ) + + if (t === "base64") { + const d = await API.handle.write( + this.path, + this.cache ); + if (d.error) { + return reject( + API.throwe( + __("{0}: {1}", d.error, this.path) + ) + ); + } + return resolve(d); + } else { + const r = await this.b64(t); + const result = await API.handle.write( + this.path, + r as string + ); + if (result.error) { + return reject( + API.throwe( + __( + "{0}: {1}", + result.error, + this.path + ) + ) + ); + } + return resolve(result); } - return resolve(r); } catch (e) { return reject(__e(e)); }