mirror of
https://github.com/He4eT/tabswiper.git
synced 2026-05-04 16:47:23 +00:00
popup/tabswiper: extract store
This commit is contained in:
parent
bf02902beb
commit
9051260bcf
1 changed files with 55 additions and 0 deletions
55
popup/tabswiper/modules/store.js
Normal file
55
popup/tabswiper/modules/store.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
export const init = ({
|
||||
tabs: browserTabs,
|
||||
onStateUpdate,
|
||||
}) => {
|
||||
/* Initial state */
|
||||
const state = {
|
||||
tabs: [],
|
||||
skipped: [],
|
||||
currentTab: null,
|
||||
}
|
||||
|
||||
/* */
|
||||
|
||||
const fetchTabs = () =>
|
||||
browserTabs.query({ currentWindow: true })
|
||||
.then((tabs) => tabs.reverse())
|
||||
.then((tabs) => void (state.tabs = tabs))
|
||||
|
||||
const setCurrent = () => {
|
||||
const untouchedTabs = state.tabs
|
||||
.filter(({ id }) => state.skipped.includes(id) === false)
|
||||
state.currentTab = untouchedTabs[0] ?? null
|
||||
}
|
||||
|
||||
/* */
|
||||
|
||||
const updateState = () =>
|
||||
fetchTabs()
|
||||
.then(setCurrent)
|
||||
.then(() => onStateUpdate(state))
|
||||
|
||||
updateState()
|
||||
|
||||
/* */
|
||||
|
||||
return {
|
||||
getCurrentState() {
|
||||
return state
|
||||
},
|
||||
actions: {
|
||||
keepTab() {
|
||||
state.skipped.push(state.currentTab.id)
|
||||
updateState()
|
||||
},
|
||||
goToTab() {
|
||||
browserTabs.update(state.currentTab.id, { active: true })
|
||||
.then(updateState)
|
||||
},
|
||||
closeTab() {
|
||||
browserTabs.remove(state.currentTab.id)
|
||||
.then(updateState)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue