# JSProgressMonitor

## Overview

This class is returned to the Servoy client from the FileProvider js\_streamFilesFromServer or js\_streamFilesToServer method\
It is used to monitor the uploads/downloads calling back a Servoy method that can then get the status of the transfer(s)\
It is called repeatedly by a Timer when scheduled with a fixed interval to callback the Servoy method provided.

## Methods Summarized

| Type                                                                                                          | Name                                                                                           | Summary                                                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                                | [cancel()](#cancel)                                                                            | Cancels the transfer process.                                                                                                                                           |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                                  | [getCurrentBytesToTransfer()](#getcurrentbytestotransfer)                                      | Returns the number of bytes to transfer for the current file.                                                                                                           |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                                  | [getCurrentFileIndex()](#getcurrentfileindex)                                                  | Returns the index of the current file being transferred.                                                                                                                |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                                  | [getCurrentTransferredBytes()](#getcurrenttransferredbytes)                                    | Returns the number of bytes already transferred for the current file.                                                                                                   |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                                  | [getCurrentTransferredFileName()](#getcurrenttransferredfilename)                              | Returns the name of the current file being transferred.                                                                                                                 |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                                  | [getTotalBytesToTransfer()](#gettotalbytestotransfer)                                          | Returns the total bytes to transfer to or from the server (sum of all the files size)                                                                                   |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                                  | [getTotalFilesToTransfer()](#gettotalfilestotransfer)                                          | Returns the total number of files to transfer.                                                                                                                          |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                                  | [getTotalTransferredBytes()](#gettotaltransferredbytes)                                        | Returns the total bytes already transferred (for all files)                                                                                                             |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                                | [isCanceled()](#iscanceled)                                                                    | Returns true if the process was canceled.                                                                                                                               |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                                | [isFinished()](#isfinished)                                                                    | Returns true if the process is finished.                                                                                                                                |
| [JSProgressMonitor](https://docs.servoy.com/reference/servoyextensions/server-plugins/file/jsprogressmonitor) | [setProgressCallBack(function, interval)](#setprogresscallback-function-interval)              | Sets a method to be called repeatedly at the given interval (in seconds), the method will receive an instance of this JSProgressMonitor updated with the latest values. |
| [JSProgressMonitor](https://docs.servoy.com/reference/servoyextensions/server-plugins/file/jsprogressmonitor) | [setProgressCallBack(function, interval, delay)](#setprogresscallback-function-interval-delay) | Sets a method to be called repeatedly at the given interval (in seconds), the method will receive an instance of this JSProgressMonitor updated with the latest values. |

## Methods Detailed

### cancel()

Cancels the transfer process.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)

**Sample**

```js
monitor.cancel();
```

### getCurrentBytesToTransfer()

Returns the number of bytes to transfer for the current file.

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### getCurrentFileIndex()

Returns the index of the current file being transferred.

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### getCurrentTransferredBytes()

Returns the number of bytes already transferred for the current file.

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### getCurrentTransferredFileName()

Returns the name of the current file being transferred.

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### getTotalBytesToTransfer()

Returns the total bytes to transfer to or from the server (sum of all the files size)

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### getTotalFilesToTransfer()

Returns the total number of files to transfer.

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### getTotalTransferredBytes()

Returns the total bytes already transferred (for all files)

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### isCanceled()

Returns true if the process was canceled.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### isFinished()

Returns true if the process is finished.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)

**Sample**

```js
application.output('total transferred: ' + monitor.getTotalTransferredBytes() + ' / ' + monitor.getTotalBytesToTransfer());
application.output('current file: ' + monitor.getCurrentTransferredFileName() + ' ( ' + monitor.getCurrentFileIndex() + ' / ' + monitor.getTotalFilesToTransfer() + ' )');
application.output('current bytes transferred: '+monitor.getCurrentTransferredBytes() + ' / ' + monitor.getCurrentBytesToTransfer());
if (monitor.isCanceled()) {
	application.output('canceled!')
}
if (monitor.isFinished()) {
	application.output('finished!')
}
```

### setProgressCallBack(function, interval)

Sets a method to be called repeatedly at the given interval (in seconds), the method will receive an instance of this JSProgressMonitor updated with the latest values. Can use an optional delay (for testing purpose in developer).

**Parameters**

* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **function** the Function to call back at the specified interval
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **interval** the interval (in seconds) to use

**Returns:** [JSProgressMonitor](https://docs.servoy.com/reference/servoyextensions/server-plugins/file/jsprogressmonitor) this for chaining

**Sample**

```js
// call the progressCallbackFuntion every 2 and a half seconds (with a delay of 200ms in developer):
monitor.setProgressCallBack(progressCallbackFunction, 2.5, (application.isInDeveloper() ? 200 : 0));
```

### setProgressCallBack(function, interval, delay)

Sets a method to be called repeatedly at the given interval (in seconds), the method will receive an instance of this JSProgressMonitor updated with the latest values. Can use an optional delay (for testing purpose in developer).

**Parameters**

* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **function** the Function to call back at the specified interval
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **interval** the interval (in seconds) to use
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **delay** adds a delay for testing purpose in Developer

**Returns:** [JSProgressMonitor](https://docs.servoy.com/reference/servoyextensions/server-plugins/file/jsprogressmonitor) this for chaining

**Sample**

```js
// call the progressCallbackFuntion every 2 and a half seconds (with a delay of 200ms in developer):
monitor.setProgressCallBack(progressCallbackFunction, 2.5, (application.isInDeveloper() ? 200 : 0));
```

***
