Hey there! I've come across a function that is supposed to return an Array. In the function below,
this.cordovaFile.readAsArrayBuffer(this.cordovaFile.dataDirectory, storageId)
actually returns a Promise Array. I'm converting it into an Observable and storing it in the timesheetArray
variable. Now, timesheetArray
will return as an Observable array, but I simply want it to return as just an Array. Here's the code:
I could really use some assistance here. If it could return just an array, then I wouldn't have to make any changes anywhere else because this function is used throughout the application.
public getAllTimesheets(): TimesheetModel[] {
const storageId = TIMESHEET_KEYS.ALL_TIMESHEET;
const timesheetArray = from(
this.cordovaFile
.readAsArrayBuffer(this.cordovaFile.dataDirectory, storageId)
.then((compressedTimesheet) => {
const start = moment();
const uint8array = new Uint8Array(compressedTimesheet);
const jsonTimeSheet = this.LZString.decompressFromUint8Array(uint8array);
this.log.debug(`LocalStorageMaterialService: getMaterials() from files: Decompression took ${moment().subtract(start.valueOf()).valueOf()} ms`);
return <TimesheetModel[]> JSON.parse(jsonTimeSheet) || [];
})
.catch((error) => {
this.log.debug('LocalStorageMaterialService: Retrieving materials from file storage was not possible: ', JSON.stringify(error));
return [];
})
)
timesheetArray.subscribe((timesheet) => {
// How can I return an Array here?
});
}
Here's just one example of why I want to return an array instead of observable:
let matchedTimesheet = _.find<TimesheetModel>(this.getAllTimesheets(),
(timesheet) => travelToDate
&& timesheet.startOfWork.isSame(travelToDate.startDate.value, 'days')
);
In the above code snippet, it expects an array rather than an Observable. I could achieve this by subscribing here as well, but if the function were to return an array instead of an observable, then I would need to make changes everywhere.