Skip to content

Commit

Permalink
Merge pull request #1771 from yuvipanda/dead-code
Browse files Browse the repository at this point in the history
JS: Remove dead state transition validation code
  • Loading branch information
consideRatio authored Oct 9, 2023
2 parents 65704c0 + e2985f7 commit a769bc1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 44 deletions.
22 changes: 5 additions & 17 deletions binderhub/static/js/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
/* If this file gets over 200 lines of code long (not counting docs / comments), start using a framework
State transitions that are valid are:
start -> waiting
start -> built
start -> failed
waiting -> building
waiting -> failed
building -> pushing
building -> failed
pushing -> built
pushing -> failed
*/
import ClipboardJS from 'clipboard';
import 'event-source-polyfill';
Expand Down Expand Up @@ -54,7 +44,7 @@ function build(providerSpec, log, fitAddon, path, pathType) {
const buildEndpointUrl = new URL("build", new URL(BASE_URL, window.location.href));
const image = new BinderRepository(providerSpec, buildEndpointUrl, buildToken);

image.onStateChange('*', function(oldState, newState, data) {
image.onStateChange('*', function(data) {
if (data.message !== undefined) {
log.writeAndStore(data.message);
fitAddon.fit();
Expand Down Expand Up @@ -92,16 +82,14 @@ function build(providerSpec, log, fitAddon, path, pathType) {
image.close();
});

image.onStateChange('built', function(oldState) {
if (oldState === null) {
$('#phase-already-built').removeClass('hidden');
$('#phase-launching').removeClass('hidden');
}
image.onStateChange('built', function() {
$('#phase-already-built').removeClass('hidden');
$('#phase-launching').removeClass('hidden');
$('#phase-launching').removeClass('hidden');
updateFavicon(BASE_URL + "favicon_success.ico");
});

image.onStateChange('ready', function(oldState, newState, data) {
image.onStateChange('ready', function(data) {
image.close();
// If data.url is an absolute URL, it'll be used. Else, it'll be interpreted
// relative to current page's URL.
Expand Down
28 changes: 1 addition & 27 deletions js/packages/binderhub-client/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export class BinderRepository {
this.buildUrl.searchParams.append("build_token", buildToken);
}
this.callbacks = {};
this.state = null;
}

/**
Expand Down Expand Up @@ -128,39 +127,14 @@ export class BinderRepository {
}
}

/**
* @param {string} oldState Old state the building process was in
* @param {string} newState New state the building process is in
* @returns True if transition from oldState to newState is valid, False otherwise
*/
validateStateTransition(oldState, newState) {
if (oldState === "start") {
return (
newState === "waiting" || newState === "built" || newState === "failed"
);
} else if (oldState === "waiting") {
return newState === "building" || newState === "failed";
} else if (oldState === "building") {
return newState === "pushing" || newState === "failed";
} else if (oldState === "pushing") {
return newState === "built" || newState === "failed";
} else {
return false;
}
}

_changeState(state, data) {
[state, "*"].map(key => {
const callbacks = this.callbacks[key];
if (callbacks) {
for (let i = 0; i < callbacks.length; i++) {
callbacks[i](this.state, state || this.state, data);
callbacks[i](data);
}
}
});

if (state && this.validateStateTransition(this.state, state)) {
this.state = state;
}
}
}

0 comments on commit a769bc1

Please sign in to comment.