Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router: support conditional navigations and empty experiment names #5216

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions tensorboard/webapp/app_routing/internal_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ export function parseCompareExperimentStr(
}
const name = nameToIdStr.slice(0, colonIndex);
const id = nameToIdStr.slice(colonIndex + 1);

if (!name) {
throw new Error(`Expect name to be non-falsy: ${nameToIdStr}`);
}

if (!id) {
throw new Error(`Expect id to be non-falsy: ${nameToIdStr}`);
}
Expand Down
13 changes: 12 additions & 1 deletion tensorboard/webapp/app_routing/internal_utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,21 @@ describe('app_routing/utils', () => {
).toEqual([{name: 'exp1', id: 'foo:my-universe:123'}]);
});

it('parses experiments with no name', () => {
expect(
utils.parseCompareExperimentStr(
'exp1:foo,:universe:bar,:baz:my-universe:123'
)
).toEqual([
{name: 'exp1', id: 'foo'},
{name: '', id: 'universe:bar'},
{name: '', id: 'baz:my-universe:123'},
]);
});

it('throws error when the map is malformed', () => {
expect(() => utils.parseCompareExperimentStr('exp1')).toThrow();
expect(() => utils.parseCompareExperimentStr('exp1:')).toThrow();
expect(() => utils.parseCompareExperimentStr('foo,bar,baz')).toThrow();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class ProgrammaticalNavigationModule {
>(
providerFactory: () => {
actionCreator: AC;
lambda: (action: U) => ProgrammaticalNavigation;
lambda: (action: U) => ProgrammaticalNavigation | null;
}
): ModuleWithProviders<ProgrammaticalNavigationModule> {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ describe('programmatical navigation module test', () => {
expect(module.getNavigation(otherTestAction())).toBe(null);
});

it('returns null when lambda does not provide a navigation', async () => {
function provider() {
return {
actionCreator: testAction,
lambda: (action: typeof testAction) => null,
};
}

await TestBed.configureTestingModule({
imports: [
ProgrammaticalNavigationModule.registerProgrammaticalNavigation(
provider
),
],
}).compileComponents();

const module = TestBed.inject(ProgrammaticalNavigationModule);
expect(module.getNavigation(testAction())).toBe(null);
});

it('throws invariant error if same action is registered twice', async () => {
function provider1() {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ export type ProgrammaticalNavigation =

export interface NavigationLambda {
actionCreator: ActionCreator<string, Creator>;
lambda(action: Action): ProgrammaticalNavigation;
/**
* The function that determines the navigation to be performed when the action
* is triggered. May return `null` to indicate that no navigation should
* occur.
*/
lambda(action: Action): ProgrammaticalNavigation | null;
}
4 changes: 3 additions & 1 deletion tensorboard/webapp/app_routing/store_only_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export function getCompareExperimentIdAliasSpec(
const idToDisplayName = new Map<string, string>();
const nameAndIds = parseCompareExperimentStr(routeParams.experimentIds);
for (const {id, name} of nameAndIds) {
idToDisplayName.set(id, name);
if (name) {
idToDisplayName.set(id, name);
}
}
return idToDisplayName;
}
Expand Down
7 changes: 7 additions & 0 deletions tensorboard/webapp/app_routing/store_only_utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ describe('app_routing store_only_utils test', () => {
expect(map).toEqual(new Map([['123', 'b']]));
});

it('does not include empty aliases', () => {
const map = getCompareExperimentIdAliasSpec({
experimentIds: 'a:123,:345',
});
expect(map).toEqual(new Map([['123', 'a']]));
});

it('throws when it is empty', () => {
expect(() =>
getCompareExperimentIdAliasSpec({
Expand Down