Skip to content

Commit

Permalink
feat(content): add query panel state
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Jun 17, 2018
1 parent 71955a4 commit c92a7d0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/content/redux/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import { startUpAction as widgetStartUp } from './modules/widget'
import { startUpAction as dictionariesStartUp } from './modules/dictionaries'

import { message } from '@/_helpers/browser-api'
import { MsgType, MsgIsPinned } from '@/typings/message'
import { MsgType, MsgIsPinned, MsgQueryPanelState } from '@/typings/message'

import { Observable } from 'rxjs/Observable'
import { map } from 'rxjs/operators/map'
import { distinctUntilChanged } from 'rxjs/operators/distinctUntilChanged'

import get from 'lodash/get'

export default () => {
const composeEnhancers = window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] || compose
Expand All @@ -24,14 +30,29 @@ export default () => {
store.dispatch<any>(dictionariesStartUp())

// sync state
store.subscribe(() => {
const state = store.getState()
const storeState$ = new Observable<StoreState>(observer => {
store.subscribe(() => observer.next(store.getState()))
})

storeState$.pipe(
map(state => state.widget.isPinned),
distinctUntilChanged()
).subscribe(isPinned => {
message.self.send<MsgIsPinned>({
type: MsgType.IsPinned,
isPinned: state.widget.isPinned,
isPinned,
})
})

message.addListener<MsgQueryPanelState>(MsgType.QueryPanelState, queryStoreState)
message.self.addListener<MsgQueryPanelState>(MsgType.QueryPanelState, queryStoreState)

function queryStoreState ({ path }: MsgQueryPanelState) {
return Promise.resolve(path && typeof path === 'string'
? get(store.getState(), path)
: store.getState()
)
}

return store
}
9 changes: 9 additions & 0 deletions src/typings/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const enum MsgType {
/** Word page */
EditWord,

/** Query panel state */
QueryPanelState,

/**
* Background proxy sends back underlyingly
*/
Expand Down Expand Up @@ -167,3 +170,9 @@ export interface MsgIsPinned {
type: MsgType.IsPinned
isPinned: boolean
}

export interface MsgQueryPanelState {
type: MsgType.QueryPanelState,
/** object path, default returns the whole state */
path?: string
}

0 comments on commit c92a7d0

Please sign in to comment.