parent
6e6e306275
commit
003742ec51
11 changed files with 179 additions and 4 deletions
@ -0,0 +1,15 @@ |
||||
import MarkInteractor from '../usecases/mark'; |
||||
|
||||
export default class MarkController { |
||||
constructor() { |
||||
this.markInteractor = new MarkInteractor(); |
||||
} |
||||
|
||||
setGlobal(key, x, y) { |
||||
this.markInteractor.setGlobal(key, x, y); |
||||
} |
||||
|
||||
jumpGlobal(key) { |
||||
this.markInteractor.jumpGlobal(key); |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
export default class GlobalMark { |
||||
constructor(tabId, x, y) { |
||||
this.tabId0 = tabId; |
||||
this.x0 = x; |
||||
this.y0 = y; |
||||
} |
||||
|
||||
get tabId() { |
||||
return this.tabId0; |
||||
} |
||||
|
||||
get x() { |
||||
return this.x0; |
||||
} |
||||
|
||||
get y() { |
||||
return this.y0; |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
import MemoryStorage from '../infrastructures/memory-storage'; |
||||
import GlobalMark from 'background/domains/global-mark'; |
||||
|
||||
const MARK_KEY = 'mark'; |
||||
|
||||
export default class MarkRepository { |
||||
constructor() { |
||||
this.cache = new MemoryStorage(); |
||||
} |
||||
|
||||
getMark(key) { |
||||
let marks = this.getOrEmptyMarks(); |
||||
let data = marks[key]; |
||||
if (!data) { |
||||
return Promise.resolve(undefined); |
||||
} |
||||
let mark = new GlobalMark(data.tabId, data.x, data.y); |
||||
return Promise.resolve(mark); |
||||
} |
||||
|
||||
setMark(key, mark) { |
||||
let marks = this.getOrEmptyMarks(); |
||||
marks[key] = { tabId: mark.tabId, x: mark.x, y: mark.y }; |
||||
this.cache.set(MARK_KEY, marks); |
||||
|
||||
return Promise.resolve(); |
||||
} |
||||
|
||||
getOrEmptyMarks() { |
||||
return this.cache.get(MARK_KEY) || {}; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,29 @@ |
||||
import GlobalMark from '../domains/global-mark'; |
||||
import TabPresenter from '../presenters/tab'; |
||||
import MarkRepository from '../repositories/mark'; |
||||
import ConsolePresenter from '../presenters/console'; |
||||
|
||||
export default class MarkInteractor { |
||||
constructor() { |
||||
this.tabPresenter = new TabPresenter(); |
||||
this.markRepository = new MarkRepository(); |
||||
this.consolePresenter = new ConsolePresenter(); |
||||
} |
||||
|
||||
async setGlobal(key, x, y) { |
||||
let tab = await this.tabPresenter.getCurrent(); |
||||
let mark = new GlobalMark(tab.id, x, y); |
||||
return this.markRepository.setMark(key, mark); |
||||
} |
||||
|
||||
async jumpGlobal(key) { |
||||
let current = await this.tabPresenter.getCurrent(); |
||||
|
||||
let mark = await this.markRepository.getMark(key); |
||||
if (!mark) { |
||||
return this.consolePresenter.showError(current.id, 'Mark is not set'); |
||||
} |
||||
// TODO scroll pages and handle if tab is gone
|
||||
return this.tabPresenter.select(mark.tabId); |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
import GlobalMark from 'background/domains/global-mark'; |
||||
|
||||
describe("background/domains/global-mark", () => { |
||||
describe("constructor and getter", () => { |
||||
let mark = new GlobalMark(1, 10, 30); |
||||
expect(mark.tabId).to.equal(1); |
||||
expect(mark.x).to.equal(10); |
||||
expect(mark.y).to.equal(30); |
||||
}); |
||||
}); |
@ -0,0 +1,23 @@ |
||||
import MarkRepository from 'background/repositories/mark'; |
||||
import GlobalMark from 'background/domains/global-mark'; |
||||
|
||||
describe("background/repositories/version", () => { |
||||
let repository; |
||||
|
||||
beforeEach(() => { |
||||
repository = new MarkRepository; |
||||
}); |
||||
|
||||
it('get and set', async() => { |
||||
let mark = new GlobalMark(1, 10, 30); |
||||
|
||||
repository.setMark('A', mark); |
||||
|
||||
let got = await repository.getMark('A'); |
||||
expect(got).to.be.a('object'); |
||||
expect(got.tabId).to.equal(1); |
||||
|
||||
got = await repository.getMark('B'); |
||||
expect(got).to.be.undefined; |
||||
}); |
||||
}); |
Reference in new issue