diff --git a/src/utils/global-key-bind.js b/src/utils/global-key-bind.js index a2b5a68..8f8554d 100644 --- a/src/utils/global-key-bind.js +++ b/src/utils/global-key-bind.js @@ -5,11 +5,18 @@ import TwitterClient from './twitter-client'; export default class GlobalKeyBind { static subscribe(store) { - new GlobalKeyBind().subscribe(store); + new GlobalKeyBind(store).subscribe(); } - subscribe(store) { - this.store = store; + constructor(store) { + this.dispatch = store.dispatch; + this.state = new RichState(store); + store.subscribe(() => { + this.state = new RichState(store); + }); + } + + subscribe() { document.addEventListener('keydown', (event) => { if (event.keyCode === Keycode.TAB) { this.handleTab(event); @@ -51,10 +58,9 @@ export default class GlobalKeyBind { if (event.altKey || event.metaKey) return; event.preventDefault(); - let state = new RichState(store); - let tweet = state.findNextTweet(); + let tweet = this.state.findNextTweet(); if (!tweet) return null; - store.dispatch(Actions.tweets.selectTweet(tweet, state.activeTab(), state.activeAccount())); + this.dispatch(Actions.tweets.selectTweet(tweet, this.state.activeTab(), this.state.activeAccount())); let visibleLimit = document.body.clientHeight; let activeBottom = document.querySelector('.timeline.active .tweets.active .tweet.active').getBoundingClientRect().bottom; @@ -68,10 +74,9 @@ export default class GlobalKeyBind { if (event.altKey || event.metaKey) return; event.preventDefault(); - let state = new RichState(store); - let tweet = state.findPrevTweet(); + let tweet = this.state.findPrevTweet(); if (!tweet) return null; - store.dispatch(Actions.tweets.selectTweet(tweet, state.activeTab(), state.activeAccount())); + this.dispatch(Actions.tweets.selectTweet(tweet, this.state.activeTab(), this.state.activeAccount())); let activeTop = document.querySelector('.timeline.active .tweets.active .tweet.active').getBoundingClientRect().top; let visibleLimit = document.querySelector('.timeline.active .tweets.active').getBoundingClientRect().top; @@ -84,24 +89,22 @@ export default class GlobalKeyBind { handleF(event) { event.preventDefault(); - let state = new RichState(store); - let client = new TwitterClient(state.activeAccount()); - let active = state.activeTweet(); + let client = new TwitterClient(this.state.activeAccount()); + let active = this.state.activeTweet(); if (!active) return null; client.favoriteStatus(active.id_str, (tweet) => { - store.dispatch(Actions.tweets.addTweet(tweet, state.activeAccount(), state.activeTab())); + this.dispatch(Actions.tweets.addTweet(tweet, this.state.activeAccount(), this.state.activeTab())); }); } handleZero(event) { event.preventDefault(); - let state = new RichState(store); - let tweet = state.findFirstTweet(); + let tweet = this.state.findFirstTweet(); if (!tweet) return null; - store.dispatch(Actions.tweets.selectTweet(tweet, state.activeTab(), state.activeAccount())); + this.dispatch(Actions.tweets.selectTweet(tweet, this.state.activeTab(), this.state.activeAccount())); let element = document.querySelector('.timeline.active .tweets.active'); element.scrollTop = 0; } diff --git a/src/utils/ipc-action.js b/src/utils/ipc-action.js index a7af053..aa0b28b 100644 --- a/src/utils/ipc-action.js +++ b/src/utils/ipc-action.js @@ -5,68 +5,69 @@ import TwitterClient from './twitter-client'; export default class IpcAction { static subscribe(store) { - new IpcAction().subscribe(store); + new IpcAction(store).subscribe(); + } + + constructor(store) { + this.dispatch = store.dispatch; + this.state = new RichState(store); + store.subscribe(() => { + this.state = new RichState(store); + }); } subscribe(store) { ipcRenderer.on('invoke-reply', (event) => { - let state = new RichState(store); - let tweet = state.activeTweet(); + let tweet = this.state.activeTweet(); if (!tweet) return null; - store.dispatch(Actions.texts.setText(`@${tweet.user.screen_name} `)); - store.dispatch(Actions.tweets.setInReplyTo(tweet)); + this.dispatch(Actions.texts.setText(`@${tweet.user.screen_name} `)); + this.dispatch(Actions.tweets.setInReplyTo(tweet)); // FIXME: Use better way to focus document.getElementById('tweet_editor').focus(); }); ipcRenderer.on('invoke-retweet', (event) => { - let state = new RichState(store); - let client = new TwitterClient(state.activeAccount()); - let active = state.activeTweet(); + let client = new TwitterClient(this.state.activeAccount()); + let active = this.state.activeTweet(); if (!active) return null; if (window.confirm(`Are you sure to retweet?: ${active.text}`)) { client.retweetStatus(active.id_str, (tweet) => { - store.dispatch(Actions.tweets.addTweet(tweet, state.activeAccount(), state.activeTab())); + this.dispatch(Actions.tweets.addTweet(tweet, this.state.activeAccount(), state.activeTab())); }); } }); ipcRenderer.on('invoke-delete', (event) => { - let state = new RichState(store); - let client = new TwitterClient(state.activeAccount()); - let active = state.activeTweet(); + let client = new TwitterClient(this.state.activeAccount()); + let active = this.state.activeTweet(); if (!active) return null; client.deleteStatus(active.id_str, (tweet) => { - store.dispatch(Actions.tweets.removeTweet(tweet, state.activeAccount(), state.activeTab())); + this.dispatch(Actions.tweets.removeTweet(tweet, this.state.activeAccount(), state.activeTab())); }); }); ipcRenderer.on('select-next-tab', (event) => { - let state = new RichState(store); - let tab = state.nextTab(); - store.dispatch(Actions.tabs.selectTab(tab, state.activeAccount())); + let tab = this.state.nextTab(); + this.dispatch(Actions.tabs.selectTab(tab, this.state.activeAccount())); }); ipcRenderer.on('select-prev-tab', (event) => { - let state = new RichState(store); - let tab = state.prevTab(); - store.dispatch(Actions.tabs.selectTab(tab, state.activeAccount())); + let tab = this.state.prevTab(); + this.dispatch(Actions.tabs.selectTab(tab, this.state.activeAccount())); }); ipcRenderer.on('select-next-account', (event) => { - let state = new RichState(store); - let index = state.nextAccountIndex(); - store.dispatch(Actions.accounts.activateAccount(index)); + let index = this.state.nextAccountIndex(); + this.dispatch(Actions.accounts.activateAccount(index)); }); ipcRenderer.on('select-prev-account', (event) => { - let state = new RichState(store); - let index = state.prevAccountIndex(); - store.dispatch(Actions.accounts.activateAccount(index)); + let index = this.state.prevAccountIndex(); + this.dispatch(Actions.accounts.activateAccount(index)); }); } }