Skip to content

Commit

Permalink
wip lazy loading luxon on models.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ichim-david committed Jun 3, 2024
1 parent 355e51a commit e8201a9
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions searchlib/lib/models.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import registry from '@eeacms/search/registry';
import { DateTime } from 'luxon';

function getHighlight(hit, fieldName) {
if (
Expand Down Expand Up @@ -116,39 +115,53 @@ export class BasicModel {
}

export class ResultModel extends BasicModel {
constructor(record, config, field_filters) {
super(record, config, field_filters);
this._getDateTime = this._getDateTime.bind(this);
}

async _getDateTime() {
if (!this._DateTime) {
const { DateTime } = await import('luxon');
this._DateTime = DateTime;
}
return this._DateTime;
}

async _getDateTimeValue(fieldName) {
const DateTime = await this._getDateTime();
const raw = this._result[fieldName]?.raw;
return raw ? DateTime.fromISO(raw) : null;
}

async _getDaysSinceIssued() {
const DateTime = await this._getDateTime();
const issued = (await this._getDateTimeValue('issued')) ?? DateTime.local();
return DateTime.local().diff(issued, 'days').as('days');
}

get daysSinceIssued() {
const raw = this._result['issued']?.raw;
const issued = raw ? DateTime.fromISO(raw) : DateTime.local();
const res = DateTime.local().diff(issued, 'days').as('days');
return res;
return this._getDaysSinceIssued();
}

get id() {
return this.id?.raw;
}

get isNew() {
const raw = this._result['issued']?.raw;
const issued = raw ? DateTime.fromISO(raw) : DateTime.local();
const res = DateTime.local().diff(issued, 'days').as('days');

return res < 30;
return this._getDaysSinceIssued().then(
(daysSinceIssued) => daysSinceIssued < 30,
);
}

get issued() {
const raw = this._result['issued']?.raw;
return raw ? DateTime.fromISO(raw) : null;
return this._getDateTimeValue('issued');
}

get expires() {
const raw = this._result['expires']?.raw;
return raw ? DateTime.fromISO(raw) : null;
return this._getDateTimeValue('expires');
}

// get isExpired() {
// return this.expires ? this.expires < DateTime.local() : false;
// }

get isExpired() {
return this._result?.['expires']?.raw
? Date.parse(this._result['expires']?.raw) < Date.now()
Expand Down

0 comments on commit e8201a9

Please sign in to comment.