Skip to content

Commit

Permalink
refactor: from event -> procedural + async context (#137)
Browse files Browse the repository at this point in the history
This PR is a big internal refactor.

* Removes all the EventEmitters all around.
* Replaces the events with procedural calls to the providers.
* A new class is added AsyncContext to handle all the async calls to the context and have a correct error handling.
* More centralization in the context.
* Separates the RunSQLCommand to its own file query.ts inside the providers folder. (We should do the same with Copy and Run)
* Providers are now created inside the context.
* Fix for the expiring token issue.
  • Loading branch information
joacoc authored Nov 7, 2023
1 parent 7eea2bd commit 831b0a6
Show file tree
Hide file tree
Showing 17 changed files with 833 additions and 531 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ jobs:
sudo apt-get install -y libnss3-dev libasound2 libgdk-pixbuf2.0-dev libgtk-3-dev libxss-dev libatk1.0-0
- run: npm install
- run: xvfb-run -a npm test
if: runner.os == 'Linux'
- name: Run Tests on Linux
if: runner.os == 'Linux'
run: |
export MZ_CONFIG_PATH=$HOME/.config/materialize/test
xvfb-run -a npm test
5 changes: 4 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
"preLaunchTask": "${defaultBuildTask}",
"env": {
"MZ_CONFIG_PATH": "${env:HOME}/.config/materialize/test"
}
}
]
}
6 changes: 3 additions & 3 deletions src/clients/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class AdminClient {

async getToken() {
// TODO: Expire should be at the half of the expiring time.
if (!this.auth || (new Date(this.auth.expires) > new Date())) {
if (!this.auth || (new Date(this.auth.expires) < new Date())) {
const authRequest: AuthenticationRequest = {
clientId: this.appPassword.clientId,
secret: this.appPassword.secretKey
Expand Down Expand Up @@ -67,7 +67,7 @@ export default class AdminClient {
}

/// Returns the JSON Web Key Set (JWKS) from the well known endpoint: `/.well-known/jwks.json`
async getJwks() {
private async getJwks() {
const client = jwksClient({
jwksUri: this.jwksEndpoint
});
Expand All @@ -78,7 +78,7 @@ export default class AdminClient {

/// Verifies the JWT signature using a JWK from the well-known endpoint and
/// returns the user claims.
async getClaims() {
private async getClaims() {
console.log("[AdminClient]", "Getting Token.");
const token = await this.getToken();

Expand Down
1 change: 0 additions & 1 deletion src/clients/cloud.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fetch from "node-fetch";
import AdminClient from "./admin";
import * as vscode from 'vscode';
import { Errors } from "../utilities/error";

const DEFAULT_API_CLOUD_ENDPOINT = 'https://api.cloud.materialize.com';
Expand Down
8 changes: 4 additions & 4 deletions src/clients/sql.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Pool, QueryResult } from "pg";
import AdminClient from "./admin";
import CloudClient from "./cloud";
import { Context, EventType } from "../context";
import { Profile } from "../context/config";
import AsyncContext from "../context/asyncContext";

export default class SqlClient {
private pool: Promise<Pool>;
private adminClient: AdminClient;
private cloudClient: CloudClient;
private context: Context;
private context: AsyncContext;
private profile: Profile;

constructor(
adminClient: AdminClient,
cloudClient: CloudClient,
profile: Profile,
context: Context,
context: AsyncContext,
) {
this.adminClient = adminClient;
this.cloudClient = cloudClient;
Expand All @@ -39,7 +39,7 @@ export default class SqlClient {
});
} catch (err) {
console.error("[SqlClient]", "Error creating pool: ", err);
this.context.emit("event", { type: EventType.error, message: err });
rej(err);
}
};

Expand Down
Loading

0 comments on commit 831b0a6

Please sign in to comment.