Skip to content

Commit

Permalink
refactor: reduce assertion code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Dec 1, 2023
1 parent 82abbb1 commit f3213f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ export class CommanderClient {
}

static registry() {
assert(IS_CLIENT, "Cannot access client registry from the server");
assert(this.started, "Commander has not been started yet");
this.assertAccess("registry");
return this.registryInstance;
}

static dispatcher() {
assert(IS_CLIENT, "Cannot access client dispatcher from the server");
assert(this.started, "Commander has not been started yet");
this.assertAccess("dispatcher");
return this.dispatcherInstance;
}

static options() {
assert(IS_CLIENT, "Cannot access client options from the server");
assert(this.started, "Commander has not been started yet");
this.assertAccess("options");
return this.optionsObject;
}

Expand All @@ -62,4 +59,9 @@ export class CommanderClient {
onHistoryUpdated: this.dispatcherInstance.getHistorySignal(),
};
}

private static assertAccess(name: string) {
assert(IS_CLIENT, `Client ${name} cannot be accessed from the server`);
assert(this.started, "Commander has not been started yet");
}
}
11 changes: 7 additions & 4 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ export class CommanderServer {
}

static registry() {
assert(IS_SERVER, "Cannot access server registry from the client");
assert(this.started, "Commander has not been started yet");
this.assertAccess("registry");
return this.registryInstance;
}

static dispatcher() {
assert(IS_SERVER, "Cannot access server dispatcher from the client");
assert(this.started, "Commander has not been started yet");
this.assertAccess("dispatcher");
return this.dispatcherInstance;
}

private static assertAccess(name: string) {
assert(IS_SERVER, `Server ${name} cannot be accessed from the server`);
assert(this.started, "Commander has not been started yet");
}
}

0 comments on commit f3213f2

Please sign in to comment.