Skip to content

Instantly share code, notes, and snippets.

View JamesTheHacker's full-sized avatar

0xDEADBEEF JamesTheHacker

  • United Kingdom
View GitHub Profile
/*
* This class is a high level view of how Redux works under the hood.
* This example includes no error handling or immutability and is used
* purely as an example to demonstrate how Redux works.
*/
class FauxRedux {
// Just like Redux we accept a reducer, and some initial state
constructor(reducer, initialState = {}) {
this.store = [ initialState ];
const initialState = {
fetchingTweets: false,
tweets: []
};
const store = new FauxRedux(reducer, initialState);
store.dispatch({
type: ‘TWEETS_RECEIVED’,
isFetching: false,
tweets: [
{
username: ‘JamesJefferyUK’,
message: ‘💜 I ❤ Redux!’
},
{
username: ‘JamesJefferyUK’,
// This is a reducer, same as Redux's
const reducer = (state, action) => {
switch(action.type) {
case 'FETCHING_TWEETS':
return {...state, fetchingTweets: true};
break;
case 'TWEETS_RECEIVED':
return { ...state,
fetchingTweets: false,
tweets: action.tweets
// Subscribe to receive notifications
store.subscribe(action => {
// Check the type of action
if(action == ‘TWEETS_RECEIVED’) {
// Clear notification
document.getElementById(‘notification’).innerText = ‘’;
// Construct the HTML for the tweets list
// Returns the last element from the state store
getState() {
return this.store[this.store.length - 1];
}
// This is a reducer, same as Redux's
const reducer = (state, action) => {
switch(action.type) {
case 'FETCHING_TWEETS':
return {...state, fetchingTweets: true};
break;
case 'TWEETS_RECEIVED':
return { ...state,
fetchingTweets: false,
tweets: action.tweets
constructor(reducer, initialState = {}) {
this.store = [ initialState ];
this.reducer = reducer;
this.subscribers = [];
}
const store = createStore(
reducer,
initialState
);
const cheerio = require('cheerio');
const google = require('google');
const request = require('superagent');
const util = require('util');
const misconfiguredCORS = require('./plugins/misconfiguredCORS');
google.resultsPerPage = 100;
const g = util.promisify(google);