Skip to content

Commit

Permalink
cleaned up implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TzeYiing committed Feb 12, 2021
1 parent ebc7362 commit d77140a
Showing 1 changed file with 34 additions and 78 deletions.
112 changes: 34 additions & 78 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
import initSqlJs from "sql.js";
import Cards, { RawCard, Card } from "./features/cards";
import Links, { Link } from "./features/links";
import utils from "./utils";
import setState, { SetState } from "./setState";
interface Deps {
refreshCards: () => RawCard[];
pushState?: (state: State) => void;
createFile?: (card: Card) => void
}
export type State = {
viewingId: number;
viewing: {
card: Card;
inboundLinks: Link[];
outboundLinks: Link[];
} | null;
};
export type App = {
db: any;
refresh: any;
deps: Deps;
cards: any;
init: Promise<App>;
setState: SetState;
state: State;
init: (initArgs, Options) => Promise<App>;
state: Object;
db: Object;
_internals?: Internals;
};
type initArgs = {
features: Object;
stateColumns: string[];
buildState: (App) => Object;
};
type Options = {
internals: boolean;
};
type Internals = {
listTables: () => string[];
};

export default {
async init(deps: Deps) {
this.deps = deps;
async init(args, opts: Options = { internals: false }) {
if (opts?.internals == true) {
// set the _internals key
this._internals = {};
}

return initSqlJs()
.then((SQL: any): any => new SQL.Database())
.then(db => {
.then((db) => {
// update the schema
this.db = db;
// TODO breakup schemas to individual feature files
let sqlstr = `
create table if not exists cards(
id integer primary key,
parentDir text,
filename text,
basename text,
content text,
rootFilePath text
);
create table if not exists links(
toCardId integer references cards(id),
fromCardId integer references cards(id),
anchorText text
const stateCols = ["id integer primary key"].concat(
args?.stateColumns || []
);
let sqlstr = `
create table if not exists state(
id INTEGER PRIMARY KEY,
viewingId integer references cards(id)
${stateCols.join(",")}
);
--https://stackoverflow.com/questions/33104101/ensure-sqlite-table-only-has-one-row
Expand All @@ -68,47 +50,21 @@ export default {

this.db.run(sqlstr);
return this;
})
.then((app: App) => {
app.refresh();
return this;
});
},
refresh() {
let cards;

if (this.deps.refreshCards) {
cards = this.deps.refreshCards();
this.cards.insertCards(cards);
this.links.buildLinks();
}
if (this.deps.pushState) {
this.deps.pushState(this.state);
}
},
get cards() {
return Cards(this);
},
get links() {
return Links(this);
},
get setState() {
return setState(this);
},
// get cards() {
// return Cards(this);
// },
// get links() {
// return Links(this);
// },
get state() {
let sql = this.db.prepare(`select * from state;`);
const partialState = sql.getAsObject([]);
delete partialState.id;
const full = {
...partialState,
viewing: !partialState.viewingId
? null
: {
card: this.cards.getCard(partialState.viewingId),
inboundLinks: this.links.listInboundLinks(partialState.viewingId),
outboundLinks: this.links.listOutboundLinks(partialState.viewingId)
}
};
return full;
},
utils
};
} as App;

0 comments on commit d77140a

Please sign in to comment.