Utilities (processFC.js)
Utilities (processFC.js)
Section titled “Utilities (processFC.js)”The lib/processFC.js
file contains utility functions that assist UnderDeckLib
in specific tasks, such as processing received WebSocket messages and managing file uploads to the cloud. Although these functions are used internally by the UnderDeck
class, understanding their functionality can be useful for debugging or for comprehending the data flow within the library.
Functions
Section titled “Functions”ProcessWebSocketMessages(Client, Dto)
Section titled “ProcessWebSocketMessages(Client, Dto)”ProcessFC.ProcessWebSocketMessages(clientInstance, receivedDto);
- Description: This function is responsible for processing messages received through the WebSocket connection. It analyzes the data object (
Dto
) and, based on theMethod
and the sender (From
), emits appropriate events on theClient
instance (usually theUnderDeck
instance). For example, if abroadcast
message with theClientUpdated
method is received, it will trigger the client account update and emit theClientUpdated
event. - Parameters:
Client
(UnderDeck
): The instance of theUnderDeck
class that received the WebSocket message.Dto
(object
): The data object received via WebSocket, containingobject
(the parsed JSON),string
(the raw message), andreply
(a function to reply to the message).
- Emitted Events (by the
Client
instance):SocketMessage
: Emitted for generic WebSocket messages.ClientUpdated
: Emitted when a broadcast message indicates that the client has been updated.SocketError
: Emitted in case of an error during message processing.
UploadFilesToCloud(Client, Data, callBackpercent)
Section titled “UploadFilesToCloud(Client, Data, callBackpercent)”const uploadResult = await ProcessFC.UploadFilesToCloud(clientInstance, { Itens: [{ key: "doc", value: "my_document" }], FilesToUpload: [ { files: [{ dirFile: "/path/to/local/file1.txt" }] }, { files: [{ dirFile: "/path/to/local/file2.jpg" }] } ]}, (progress) => { console.log(`Upload progress: ${progress.percent}%`);});console.log("Upload result:", uploadResult);
- Description: Manages the process of uploading files to the UnderDeck cloud storage service. This function iterates over the files specified in the
Data
object, reads each file locally, and sends them to the upload API. It also supports a callback to report upload progress, which is useful for user interfaces that need to display the status of the process. - Parameters:
Client
(UnderDeck
): The instance of theUnderDeck
class (used to accessClient.Api
andClient.User
).Data
(object
): An object containing the data to be sent. It should have the following structure:{Itens: [], // Array of JSON items to be synchronizedFilesToUpload: [{ files: [{ dirFile: "path/to/local/file", url: "optional_cloud_url" }] }]}callBackpercent
(function
, optional): A callback function that is invoked periodically during the upload. It receives an object with progress information, includingpercent
(total percentage),pos1
,posMax1
,pos2
,posMax2
.
- Returns:
Promise<Array<object>>
A promise that resolves with an array of file objects, where each file object now includes theurl
of the file in the cloud after successful upload. In case of a file upload failure, it may be removed from the resulting array.