Skip to content

Commit

Permalink
feat(content): add redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Apr 16, 2018
1 parent 5735c60 commit 956d2af
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/content/redux/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import rootReducer from './modules'

import { listenConfig } from './modules/config'
import { listenSelection } from './modules/selection'

export default () => {
const composeEnhancers = window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] || compose

const store = createStore(
rootReducer,
composeEnhancers(applyMiddleware(thunk))
)

store.dispatch(listenConfig())
store.dispatch(listenSelection())

return store
}
42 changes: 42 additions & 0 deletions src/content/redux/modules/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { appConfigFactory, AppConfig } from '@/app-config'
import { addAppConfigListener } from '@/_helpers/config-manager'

/*-----------------------------------------------*\
Actions
\*-----------------------------------------------*/

const enum Actions {
NEW_CONFIG = 'configs/NEW_CONFIG'
}

/*-----------------------------------------------*\
State
\*-----------------------------------------------*/

export type ConfigState = AppConfig

export default function reducer (state = appConfigFactory(), action) {
switch (action.type) {
case Actions.NEW_CONFIG:
return action.payload
default:
return state
}
}

/*-----------------------------------------------*\
Action Creators
\*-----------------------------------------------*/

/** When app config is updated */
export const newConfig = config => ({ type: Actions.NEW_CONFIG, payload: config })

/*-----------------------------------------------*\
Side Effects
\*-----------------------------------------------*/

export function listenConfig () {
return dispatch => {
addAppConfigListener(({ config }) => dispatch(newConfig(config)))
}
}
19 changes: 19 additions & 0 deletions src/content/redux/modules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { combineReducers } from 'redux'
import config, { ConfigState } from './config'
import selection, { SelectionState } from './selection'
import dictionaries, { DictionariesState } from './dictionaries'
import widget, { WidgetState } from './widget'

export default combineReducers({
config,
selection,
dictionaries,
widget,
})

export type StoreState = {
config: ConfigState
selection: SelectionState
dictionaries: DictionariesState
widget: WidgetState
}
59 changes: 59 additions & 0 deletions src/content/redux/modules/selection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { message } from '@/_helpers/browser-api'
import { MsgSelection, MsgType } from '@/typings/message'

/*-----------------------------------------------*\
Actions
\*-----------------------------------------------*/

const enum Actions {
NEW_SELECTION = 'selection/NEW_SELECTION'
}

/*-----------------------------------------------*\
State
\*-----------------------------------------------*/

export type SelectionState = MsgSelection

const initState: SelectionState = {
type: MsgType.Selection,
selectionInfo: {
text: '',
context: '',
title: '',
url: '',
favicon: '',
trans: '',
note: '',
},
mouseX: 0,
mouseY: 0,
dbClick: false,
ctrlKey: false,
}

export default function reducer (state = initState, action) {
switch (action.type) {
case Actions.NEW_SELECTION:
return action.payload
default:
return state
}
}

/*-----------------------------------------------*\
Action Creators
\*-----------------------------------------------*/

/** When new selection is made */
export const newSelection = selection => ({ type: Actions.NEW_SELECTION, payload: selection })

/*-----------------------------------------------*\
Side Effects
\*-----------------------------------------------*/

export function listenSelection () {
return dispatch => {
message.self.addListener(MsgType.Selection, message => dispatch(newSelection(message)))
}
}

0 comments on commit 956d2af

Please sign in to comment.