Skip to content

Commit

Permalink
cleanup API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Sep 5, 2024
1 parent daec0cd commit f4f7874
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/lib/race.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export async function race(
}

const API = {
// TODO: validate user input
play() {
if (!store.getState().ticker.isRunning) {
ticker.start();
Expand All @@ -113,10 +112,12 @@ export async function race(
ticker.skipForward();
},
inc(value = 1) {
store.dispatch(actions.ticker.inc(+value));
if (!isNaN(Number(value))) value = 1;
store.dispatch(actions.ticker.inc(Number(value)));
},
dec(value = 1) {
store.dispatch(actions.ticker.dec(+value));
if (!isNaN(Number(value))) value = 1;
store.dispatch(actions.ticker.dec(Number(value)));
},
setDate(inputDate: string | Date) {
store.dispatch(actions.ticker.updateDate(getDateString(inputDate)));
Expand All @@ -134,13 +135,13 @@ export async function race(
d3.select(root)
.select('rect.' + safeName(name))
.classed('selected', true);
store.dispatch(actions.data.addSelection(name));
store.dispatch(actions.data.addSelection(String(name)));
},
unselect(name: string) {
d3.select(root)
.select('rect.' + safeName(name))
.classed('selected', false);
store.dispatch(actions.data.removeSelection(name));
store.dispatch(actions.data.removeSelection(String(name)));
},
unselectAll() {
d3.select(root).selectAll('rect').classed('selected', false);
Expand Down Expand Up @@ -212,12 +213,15 @@ export async function race(
}
},
onDate(date: string | Date, fn: ApiCallback) {
if (typeof fn !== 'function') {
throw new Error('The second argument must be a function');
}
const dateString = getDateString(date);
let lastDate = '';
const watcher = addApiSubscription(() => {
if (store.getState().ticker.currentDate === dateString && dateString !== lastDate) {
lastDate = store.getState().ticker.currentDate; // avoid infinite loop if fn dispatches action
fn.call(API, getTickDetails(store));
fn(getTickDetails(store));
}
lastDate = store.getState().ticker.currentDate;
});
Expand All @@ -228,8 +232,11 @@ export async function race(
};
},
on(event: EventType, fn: ApiCallback) {
if (typeof fn !== 'function') {
throw new Error('The second argument must be a function');
}
const watcher = events.addApiEventHandler(event, () => {
fn.call(API, getTickDetails(store));
fn(getTickDetails(store));
});
return {
remove() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function getText(

export function safeName(name: string) {
// replace non-alphanumeric with underscore
return name.replace(/[\W]+/g, '_');
return String(name).replace(/[\W]+/g, '_');
}

export function toggleClass(root: HTMLElement, selector: string, className: string) {
Expand Down

0 comments on commit f4f7874

Please sign in to comment.