Skip to content

Commit

Permalink
fix: resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-cucu committed Mar 5, 2024
1 parent c418d14 commit f5f48ad
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
24 changes: 16 additions & 8 deletions api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
Error as MacaroonError,
MacaroonObject,
} from "@canonical/macaroon-bakery/dist/macaroon";
import type { Callback, JujuRequest } from "../generator/interfaces";
import type {
Callback,
CloseCallback,
JujuRequest,
} from "../generator/interfaces";
import {
ClassType,
Facade,
Expand All @@ -34,7 +38,7 @@ export const CLIENT_VERSION = "3.3.2";

export interface ConnectOptions {
bakery?: Bakery | null;
closeCallback: Callback<number>;
closeCallback: CloseCallback;
debug?: boolean;
facades?: (ClassType<Facade> | GenericFacade)[];
onWSCreated?: (ws: WebSocket) => void;
Expand Down Expand Up @@ -368,7 +372,7 @@ class Client {
another callback. It is responsibility of the callback to call the
provided callback if present.
*/
logout(callback?: Callback<Client>) {
logout(callback?: (code: number, callback: CloseCallback) => void) {
this._transport.close(callback);
}

Expand Down Expand Up @@ -414,10 +418,10 @@ export class Transport {
_ws: WebSocket;
_counter: number;
_callbacks: { [k: number]: Callback<any> };
_closeCallback: Callback<number>;
_closeCallback: CloseCallback;
_debug: boolean;

constructor(ws: WebSocket, closeCallback: Callback<number>, debug: boolean) {
constructor(ws: WebSocket, closeCallback: CloseCallback, debug: boolean) {
this._ws = ws;
this._counter = 0;
this._callbacks = {};
Expand All @@ -433,7 +437,7 @@ export class Transport {
if (this._debug) {
console.debug("close:", evt.code, evt.reason);
}
this._closeCallback(toError(evt.code.toString()));
this._closeCallback(evt.code);
};
}

Expand All @@ -447,7 +451,11 @@ export class Transport {
@param resolve Function called when the request is successful.
@param reject Function called when the request is not successful.
*/
write(req: JujuRequest, resolve: Function, reject: (error: any) => void) {
write(
req: JujuRequest,
resolve: (value: any) => void,
reject: (error: Error) => void
) {
// Check that the connection is ready and sane.
const state = this._ws.readyState;
if (state !== 1) {
Expand Down Expand Up @@ -477,7 +485,7 @@ export class Transport {
callback receives the close code and optionally another callback. It is
responsibility of the callback to call the provided callback if present.
*/
close(callback?: Callback<any>) {
close(callback?: (code: number, callback: CloseCallback) => void) {
const closeCallback = this._closeCallback;
this._closeCallback = (code) => {
if (callback) {
Expand Down
6 changes: 0 additions & 6 deletions api/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,6 @@ function wrapClient(cls: any) {
method which can be provided a callback receiving an error.
*/
cls.prototype.watch = function (callback: Callback<any>): object | undefined {
if (!callback) {
callback = (_error: CallbackError, _result?: any) => {};
}
// Check that the AllWatcher facade is loaded, as we will use it.
const allWatcher = this._info.getFacade("allWatcher");
if (!allWatcher) {
Expand Down Expand Up @@ -341,9 +338,6 @@ function wrapController(cls: any) {
method which can be provided a callback receiving an error.
*/
cls.prototype.watch = function (callback: Callback<any>): object | undefined {
if (!callback) {
callback = (_error: CallbackError, _result?: any) => {};
}
// Check that the AllModelWatcher facade is loaded, as we will use it.
const allModelWatcher: any = this._info.getFacade("allModelWatcher");
if (!allModelWatcher) {
Expand Down
2 changes: 0 additions & 2 deletions api/tests/test-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ describe("connect", () => {

it("login redirection error failure via promise", (done) => {
connect("wss://1.2.3.4", options).then((juju: Client) => {
// juju._admin.redirectInfo = jest.fn().mockImplementation(() => null);
juju
?.login({})
.then(() => fail)
Expand Down Expand Up @@ -206,7 +205,6 @@ describe("connect", () => {

it("login generic redirection error failure via promise", (done) => {
connect("wss://1.2.3.4", options).then((juju: Client) => {
// juju._admin.redirectInfo = jest.fn().mockImplementation(() => null);
juju
?.login({})
.then(() => fail)
Expand Down
8 changes: 3 additions & 5 deletions api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,17 @@ export function autoBind(obj: { [k: string]: any }): void {
export function createAsyncHandler<T>(
callback: Callback<T> | undefined,
resolve: (value: T) => void,
reject: (value: CallbackError) => void,
reject: (error: CallbackError) => void,
transform?: (value: T) => T
): { resolve: Function; reject: Function } {
): { resolve: (value: T) => void; reject: (error: CallbackError) => void } {
return {
resolve: (value: T) => {
if (transform) {
value = transform(value!);
}
callback ? callback(null, value) : resolve(value!);
},
reject: (error: CallbackError) => {
callback ? callback(error) : reject(error);
},
reject: callback ? callback : reject,
};
}

Expand Down
1 change: 1 addition & 0 deletions generator/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ export interface JujuRequest {

export type CallbackError = Error | null;
export type Callback<T> = (error: CallbackError, value?: T) => void;
export type CloseCallback = (code: number) => void;

0 comments on commit f5f48ad

Please sign in to comment.