Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
feat(tabs): switched from timestamp ids to unique ids
Browse files Browse the repository at this point in the history
  • Loading branch information
herteleo committed Apr 20, 2019
1 parent 3fa8cb1 commit a1d81da
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"electron-window-state": "^5.0.3",
"is-url-relative-without-domain": "^2.0.0",
"lodash-id": "^0.14.0",
"lowdb": "^1.0.0",
"prism-themes": "^1.0.1",
"prismjs": "^1.15.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/TabMain.vue
Expand Up @@ -35,7 +35,7 @@ export default {
},
computed: {
isActive() {
return parseInt(this.$route.params.id, 0) === this.item.id;
return this.$route.params.id === this.item.id;
},
webview() {
return this.$refs.webview.$refs.webview;
Expand Down
2 changes: 2 additions & 0 deletions src/db.js
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import { homedir } from 'os';
import path from 'path';
import lowdb from 'lowdb';
import lodashId from 'lodash-id';
import FileSync from 'lowdb/adapters/FileSync';

export default class {
Expand Down Expand Up @@ -30,6 +31,7 @@ export default class {
init() {
const adapter = new FileSync(path.join(this.options.path, this.options.file));
this.db = lowdb(adapter);
this.db._.mixin(lodashId);

this.db.defaults(this.options.defaults).write();
}
Expand Down
35 changes: 16 additions & 19 deletions src/store/modules/Tabs.js
Expand Up @@ -16,10 +16,10 @@ export default {
},
getters: {
active({ activeId, dbUpdated }) {
return db(dbUpdated).get('tabs').find(item => item.id === activeId).value() || {};
return db(dbUpdated).get('tabs').getById(activeId).value() || {};
},
byId({ dbUpdated }) {
return id => db(dbUpdated).get('tabs').find(item => item.id === id).value() || {};
return id => db(dbUpdated).get('tabs').getById(id).value() || {};
},
list({ dbUpdated }) {
return db(dbUpdated)
Expand All @@ -38,23 +38,23 @@ export default {
state.activeId = id;
state.dbUpdated = Date.now();
},
create(state, item) {
db()
.get('tabs')
.push(item)
.write();

create(state, tab) {
/*
the actual tab creation is processed in action
because mutations don't return values but we need the id
to forward the user to the newly created tab
*/
db()
.get('sorting')
.push(item.id)
.push(tab.id)
.write();

state.dbUpdated = Date.now();
},
delete(state, item) {
db()
.get('tabs')
.remove(i => i.id === item.id)
.removeById(item.id)
.write();

db()
Expand All @@ -72,21 +72,18 @@ export default {
update(state, { id, data }) {
db()
.get('tabs')
.find(i => i.id === id).assign(data)
.updateById(id, data)
.write();

state.dbUpdated = Date.now();
},
},
actions: {
create({ commit }, payload = {}) {
const id = Date.now();
const item = {
id,
...payload,
};
commit('create', item);
return item;
create({ commit }, item) {
const tab = db().get('tabs').insert(item).write();

commit('create', tab);
return tab;
},
},
};

0 comments on commit a1d81da

Please sign in to comment.