Skip to content

Commit

Permalink
fix: make WorldEvent async, parseAsyncArray build usage
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiTenno committed Jun 23, 2024
1 parent 67bc70e commit 405ec91
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 44 deletions.
7 changes: 6 additions & 1 deletion .commitlintrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ export default {
],
rules: {
'body-max-line-length': [
RuleConfigSeverity.Error
RuleConfigSeverity.Disabled
],
'subject-case': [
RuleConfigSeverity.Error,
'never',
['sentence-case', 'start-case']
]
}
}
4 changes: 2 additions & 2 deletions lib/WorldState.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function parseArray(ParserClass, dataArray, deps, uniqueField) {

/**
* Parse array of objects that requires async parsing
* @param {object} ParserClass class for parsing data
* @param {object} ParserClass class for parsing data - must expose a static build method
* @param {Array<BaseContentObject>} dataArray array of raw data
* @param {Dependency} deps shared dependency object
* @param {*} [uniqueField] field to treat as unique
Expand All @@ -83,7 +83,7 @@ export const parseAsyncArray = async (ParserClass, dataArray, deps, uniqueField)
const arr = [];
// eslint-disable-next-line no-restricted-syntax
for await (const d of dataArray ?? []) {
arr.push(await new ParserClass(d, deps));
arr.push(await ParserClass.build(d, deps));
}
if (uniqueField) {
const utemp = {};
Expand Down
2 changes: 1 addition & 1 deletion lib/models/SyndicateJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class SyndicateJob extends WorldstateObject {
* @param {object} deps The dependencies object
* @param {string} deps.locale Locale to use for translations
*
* This DOES NOT populate the reward pool or the
* This DOES NOT populate the reward pool
*/
constructor(data, expiry, { locale = 'en' }) {
super({
Expand Down
40 changes: 21 additions & 19 deletions lib/models/SyndicateMission.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ import WorldstateObject from './WorldstateObject.js';
* @augments {WorldstateObject}
*/
export default class SyndicateMission extends WorldstateObject {
/**
* Build a new SyndicateMission with async operations & data
* @param {object} data The syndicate mission data
* @param {object} deps The dependencies object
* @param {string} deps.locale Locale to use for translations
* @returns {Promise.<SyndicateMission>} SyndicateMission object w/ async resolution of jobs
*/
static async build(data, deps) {
const syndicateMission = new SyndicateMission(data, deps);
if (data.Jobs) {
const jobs = [];
// eslint-disable-next-line no-restricted-syntax
for await (const job of data.Jobs ?? []) {
jobs.push(await SyndicateJob.build(job, syndicateMission.expiry, deps));
}
syndicateMission.jobs = jobs;
}

return syndicateMission;
}

/**
* @param {object} data The syndicate mission data
* @param {object} deps The dependencies object
Expand All @@ -18,10 +39,6 @@ export default class SyndicateMission extends WorldstateObject {
constructor(data, { locale = 'en' } = { locale: 'en' }) {
super(data);

const deps = {
locale,
};

/**
* The date and time at which the syndicate mission starts
* @type {Date}
Expand Down Expand Up @@ -69,21 +86,6 @@ export default class SyndicateMission extends WorldstateObject {
* @type {string} time delta string from now to the expiry
*/
this.eta = this.getETAString();

// Async stuff!
// eslint-disable-next-line no-constructor-return
return Promise.resolve(
(async () => {
if (data.Jobs) {
// eslint-disable-next-line no-restricted-syntax
for await (const job of data.Jobs ?? []) {
this.jobs.push(await new SyndicateJob(job, this.expiry, deps));
}
}

return this;
})()
);
}

/**
Expand Down
53 changes: 32 additions & 21 deletions lib/models/WorldEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ import Reward from './Reward.js';
* @augments {WorldstateObject}
*/
export default class WorldEvent extends WorldstateObject {
/**
* Asynchronously build a new WorldEvent
* @param {object} data The event data
* @param {object} deps The dependencies object
* @param {string} deps.locale Locale to use for translations
* @returns {Promise.<WorldEvent>} The created WorldEvent object
*/
static async build(data, deps) {
const event = new WorldEvent(data, deps);
if (data.Jobs) {
const jobs = [];
// eslint-disable-next-line no-restricted-syntax
for await (const job of data.Jobs ?? []) {
jobs.push(await SyndicateJob.build(job, event.expiry, deps));
}
event.jobs = jobs;
}
if (data.PreviousJobs) {
const previousJobs = [];
// eslint-disable-next-line no-restricted-syntax
for await (const job of data.PreviousJobs ?? []) {
previousJobs.push(await SyndicateJob.build(job, event.expiry, deps));
}
event.previousJobs = previousJobs;
}
return event;
}

/**
* @param {object} data The event data
* @param {object} deps The dependencies object
Expand Down Expand Up @@ -201,7 +229,7 @@ export default class WorldEvent extends WorldstateObject {
*/
this.isCommunity = data.Community;

/**
/*
* Affectors for this mission
* @type {string[]}
*/
Expand Down Expand Up @@ -237,26 +265,9 @@ export default class WorldEvent extends WorldstateObject {
activation: parseDate(data.NextAltActivation),
};

// Async stuff!
// eslint-disable-next-line no-constructor-return
return Promise.resolve(
(async () => {
if (data.JobAffiliationTag) {
this.affiliatedWith = syndicate(data.JobAffiliationTag, locale);
if (data.Jobs) {
// eslint-disable-next-line no-restricted-syntax
for await (const job of data.Jobs ?? []) {
this.jobs.push(await new SyndicateJob(job, this.expiry, opts));
}
// eslint-disable-next-line no-restricted-syntax
for await (const job of data.PreviousJobs ?? []) {
this.previousJobs.push(await new SyndicateJob(job, this.expiry, opts));
}
}
}
return this;
})()
);
if (data.JobAffiliationTag) {
this.affiliatedWith = syndicate(data.JobAffiliationTag, locale);
}
}

/**
Expand Down

0 comments on commit 405ec91

Please sign in to comment.