Skip to content

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.

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 the Method and the sender (From), emits appropriate events on the Client instance (usually the UnderDeck instance). For example, if a broadcast message with the ClientUpdated method is received, it will trigger the client account update and emit the ClientUpdated event.
  • Parameters:
    • Client (UnderDeck): The instance of the UnderDeck class that received the WebSocket message.
    • Dto (object): The data object received via WebSocket, containing object (the parsed JSON), string (the raw message), and reply (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 the UnderDeck class (used to access Client.Api and Client.User).
    • Data (object): An object containing the data to be sent. It should have the following structure:
      {
      Itens: [], // Array of JSON items to be synchronized
      FilesToUpload: [
      { 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, including percent (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 the url of the file in the cloud after successful upload. In case of a file upload failure, it may be removed from the resulting array.