Skip to content

Commit

Permalink
fix singular rates with a value (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed Jun 8, 2023
2 parents 85cc063 + 4409438 commit 00a2af9
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/resources/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@
// limitations under the License.
import { EventMiddleware, Faas, ScheduleMiddleware } from '../faas';

const Frequencies = [
'day',
'days',
'hour',
'hours',
'minute',
'minutes',
] as const;
const Frequencies = ['days', 'hours', 'minutes'] as const;

export type Frequency = (typeof Frequencies)[number];

Expand Down Expand Up @@ -139,12 +132,20 @@ class Schedule {
rate: string,
...middleware: ScheduleMiddleware[]
): Promise<void> => {
// handle singular frequencies. e.g. schedule('something').every('day')
// handle singular frequencies without a value, e.g. schedule('something').every('day')
if (Frequencies.indexOf(`${rate}s` as Frequency) !== -1) {
rate = `1 ${rate}s`; // 'day' becomes '1 days'
}

const r = new Rate(this, rate, ...middleware);
// handle singular frequencies with a value, e.g. schedule('something').every('1 day')
const rateParts = rate.split(' ');
const value = rateParts[0];
let unit = rateParts[1];
if (Frequencies.indexOf(`${unit}s` as Frequency) !== -1) {
unit = `${unit}s`; // 'day' becomes 'days'
}

const r = new Rate(this, `${value} ${unit}`, ...middleware);
// Start the new rate immediately
return r['start']();
};
Expand Down

0 comments on commit 00a2af9

Please sign in to comment.