Skip to content

Commit

Permalink
Added EagleStorage class to contain IndexedDB work
Browse files Browse the repository at this point in the history
  • Loading branch information
james-strauss-uwa committed Feb 26, 2025
1 parent 20a058e commit 1e42e75
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/EagleStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Repositories } from "./Repositories";
import { Repository } from "./Repository";


export class EagleStorage {

public static db: IDBDatabase;

static init(){
console.log("EagleStorage.init()");

const request = indexedDB.open("EAGLE");
request.onerror = (event) => {
console.error("Why didn't you allow my web app to use IndexedDB?!");
};
request.onsuccess = (event) => {
EagleStorage.db = (<any>event.target).result;

EagleStorage.db.onerror = (event) => {
// generic error handler for all errors targeted at this database's requests!
console.error(`Database error: ${(<any>event.target).error?.message}`);
};
};

request.onupgradeneeded = (event) => {
console.log("onupgradeneeded");
const db = (<any>event.target).result;

// create an objectStore for this database
const objectStore = db.createObjectStore("repositories", {keyPath: "id"});

// Use transaction oncomplete to make sure the objectStore creation is
// finished before adding data into it.
objectStore.transaction.oncomplete = () => {
console.log("oncomplete");

// Store values in the newly created objectStore.
const customerObjectStore = db.transaction("repositories", "readwrite").objectStore("repositories");

// read list of repositories from localStorage
const githubRepos = Repositories.listCustomRepositories(Repository.Service.GitHub);
const gitlabRepos = Repositories.listCustomRepositories(Repository.Service.GitLab);
console.log("found ", githubRepos.length + gitlabRepos.length, "repos");

githubRepos.forEach((repo) => {
customerObjectStore.add(Repository.toJson(repo));
});
gitlabRepos.forEach((repo) => {
customerObjectStore.add(Repository.toJson(repo));
});
};
}
}
}
11 changes: 11 additions & 0 deletions src/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ export class Repository {

return fileNameA.toLowerCase() > fileNameB.toLowerCase() ? 1 : -1;
}

public static toJson(repository: Repository) : object {
const result : any = {};

result.id = repository._id;
result.service = repository.service;
result.name = repository.name;
result.branch = repository.branch;

return result;
}
}

export namespace Repository {
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { CategoryData } from './CategoryData';
import { Daliuge } from './Daliuge';
import { Eagle } from './Eagle';
import { EagleConfig } from "./EagleConfig";
import { EagleStorage } from "./EagleStorage";
import { Errors } from './Errors';
import { GitHub } from './GitHub';
import { GitLab } from './GitLab';
Expand Down Expand Up @@ -78,6 +79,7 @@ $(function(){
(<any>window).Daliuge = Daliuge;
(<any>window).Eagle = Eagle;
(<any>window).EagleConfig = EagleConfig;
(<any>window).EagleStorage = EagleStorage;
(<any>window).Errors = Errors;
(<any>window).GraphConfig = GraphConfig;
(<any>window).GraphConfigurationsTable = GraphConfigurationsTable;
Expand Down Expand Up @@ -151,6 +153,7 @@ $(function(){
document.onkeydown = KeyboardShortcut.processKey;
document.onkeyup = KeyboardShortcut.processKey;

EagleStorage.init();
loadRepos();

// auto load a tutorial, if specified on the url
Expand Down
1 change: 1 addition & 0 deletions src/require-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require.config({
"GraphConfig": "./static/built/GraphConfig",
"Eagle": "./static/built/Eagle",
"EagleConfig": "./static/built/EagleConfig",
"EagleStorage": "./static/built/EagleStorage",
"Utils": "./static/built/Utils",
"Modals": "./static/built/Modals",
"GraphUpdater": "./static/built/GraphUpdater",
Expand Down

0 comments on commit 1e42e75

Please sign in to comment.