Skip to content

Commit

Permalink
feat: make datagenerator async (RomRider#157)
Browse files Browse the repository at this point in the history
* deps: update some deps

* chore: remove pre/post tasks on debug

* CHG: make datagenerator async

* chore: linting

Co-authored-by: Jérôme Wiedemann <jerome@wnetworks.org>
  • Loading branch information
koying and RomRider authored May 21, 2021
1 parent 60f5cd8 commit b9049fc
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/graphEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default class GraphEntry {
let history: EntityEntryCache | undefined = undefined;

if (this._config.data_generator) {
history = this._generateData(start, end);
history = await this._generateData(start, end);
} else {
this._realStart = new Date(start);
this._realEnd = new Date(end);
Expand Down Expand Up @@ -320,17 +320,20 @@ export default class GraphEntry {
return this._hass?.callApi('GET', url);
}

private _generateData(start: Date, end: Date): EntityEntryCache {
private async _generateData(start: Date, end: Date): Promise<EntityEntryCache> {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
let data;
try {
data = new Function(
const datafn = new AsyncFunction(
'entity',
'start',
'end',
'hass',
'moment',
`'use strict'; ${this._config.data_generator}`,
).call(this, this._entityState, start, end, this._hass, moment);
);
data = await datafn(this._entityState, start, end, this._hass, moment);
} catch (e) {
const funcTrimmed =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down

0 comments on commit b9049fc

Please sign in to comment.