Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
Limit scope of store
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Mar 19, 2016
1 parent 79c2385 commit bfeb739
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 41 deletions.
35 changes: 19 additions & 16 deletions src/utils/global-key-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
51 changes: 26 additions & 25 deletions src/utils/ipc-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}
}

0 comments on commit bfeb739

Please sign in to comment.