Skip to content

Commit

Permalink
Add memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
BrainMaestro committed Feb 13, 2017
1 parent 43a71aa commit 0ea1139
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cerebro-stackoverflow",
"version": "0.1.4",
"version": "0.2.0",
"description": "Cerebro plugin to find answers to questions on stack overflow",
"license": "MIT",
"repository": "BrainMaestro/cerebro-stackoverflow",
Expand Down
32 changes: 10 additions & 22 deletions src/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,24 @@ class Preview extends React.Component {
}

componentDidMount() {
searchGoogle(this.props.term, (err, res) => {
if (err) {
return this.setState({ error: { message: err, type: 'google' }});
}

this.setState({ links: res.body.items });
});
searchGoogle(this.props.term)
.then(res => this.setState({ links: res.body.items }))
.catch(err => this.setState({ error: { message: err, type: 'google' }}));
}

handleApiSearch() {
searchApi(this.props.term, (err, res) => {
if (err) {
return this.setState({ error: { message: err, type: 'api' }});
}

this.setState({
searchApi(this.props.term)
.then(res => this.setState({
links: res.body.items,
error: { message: null, type: null }
});
});
}))
.catch(err => this.setState({ error: { message: err, type: 'api' }}));
}

handleClick(link) {
get(link.question_id, (err, res) => {
if (err) {
return this.setState({ error: { message: err, type: 'api' }});
}

this.setState({ question: res.body.items[0] });
});
get(link.question_id)
.then(res => this.setState({ question: res.body.items[0] }))
.catch(err => this.setState({ error: { message: err, type: 'api' }}));
}

handleGoBack() {
Expand Down
10 changes: 3 additions & 7 deletions src/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ class Question extends React.Component {
}

componentDidMount() {
get(this.props.question.question_id, (err, res) => {
if (err) {
return this.setState({ error: { message: err, type: 'api' }});
}

this.setState({ answers: res.body.items });
}, true);
get(this.props.question.question_id, true)
.then(res => this.setState({ answers: res.body.items }))
.catch(err => this.setState({ error: { message: err, type: 'api' }}));
}

renderAnswers() {
Expand Down
35 changes: 17 additions & 18 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const google = require('google');
const request = require('superagent');
const { memoize } = require('cerebro-tools');
google.resultsPerPage = 10;

const defaultQuery = {
Expand All @@ -9,46 +10,44 @@ const defaultQuery = {

const baseUrl = 'https://api.stackexchange.com/2.2';

function searchGoogle(term, callback) {
const searchGoogle = memoize(term => {
term = encodeURIComponent(term);
google(`${term} site:stackoverflow.com`, (err, res) => {
if (err) {
callback(err, res);
}

get(res.links, callback);
return new Promise((resolve, reject) => {
google(`${term} site:stackoverflow.com`, (err, res) => {
err ? reject(err) : resolve(get(res.links));
});
});
}
});

function searchApi(term, callback) {
const searchApi = memoize(term => {
term = encodeURIComponent(term);
const url = `${baseUrl}/search`;
request

return request
.get(url)
.query(defaultQuery)
.query({
sort: 'activity',
intitle: term,
})
.end(callback);
}
});
});

function get(questionId, callback, answers = false) {
const get = (questionId, answers = false) => {
questionId = Array.isArray(questionId) ? parseQuestionId(questionId) : questionId;
let url = `${baseUrl}/questions/${questionId}`;
if (answers) {
url += '/answers';
}

request
return request
.get(url)
.query(defaultQuery)
.query({
filter: 'withbody',
sort: 'votes',
})
.end(callback);
}
});
};

function parseQuestionId(links) {
const re = /.*stackoverflow.com\/questions\/(\d+)\//;
Expand All @@ -69,6 +68,6 @@ function parseQuestionId(links) {

module.exports = {
get,
searchGoogle,
searchApi,
searchGoogle,
};

0 comments on commit 0ea1139

Please sign in to comment.