Skip to content

Instantly share code, notes, and snippets.

@Nicktho
Nicktho / gist:bf19350d3b481ea96f89
Last active August 29, 2015 14:19
fetch() in swift
class fetch {
init(_ url: String, cb: (data: String) -> ()) {
NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
cb(data: NSString(data: data, encoding: NSUTF8StringEncoding) as! String)
}.resume()
}
}
fetch("http://www.google.com") {(data: String) in
println(data);
@Nicktho
Nicktho / design.js
Last active November 4, 2015 12:25
Redux actions and app state design for api interaction
// ## FETCH USERS (read all)
// start
{ type: 'FETCH_USERS', payload: { loading: true } }
{
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
loading: true,
export function createStore(reducer, initialState) {
let currentState = initialState;
return {}
}
describe('createStore', () => {
describe('#getState', () => {
it('should return the current state', () => {
const state = { notes: [ 'hello world', 'hello' ] };
const store = createStore(x => x, state);
expect(store.getState()).to.equal(state);
});
});
export function createStore(reducer, initialState) {
let currentState = initialState;
function getState() {
return currentState;
}
return { getState };
})
describe('#dispatch', () => {
const reducer = (state = [ 'hello world' ], action) => {
switch(action.type) {
case 'ADD_NOTE':
return [ ...state, action.payload ];
default:
return state;
}
};
export function createStore(reducer, initialState) {
let currentState = initialState;
function getState() {
return currentState;
}
function dispatch(action) {
currentState = reducer(currentState, action);
}
describe('#subscribe', () => {
it('should add the passed function to the listeners', () => {
const store = createStore(x => x, {});
let called = false;
store.subscribe(() => called = true);
store.dispatch({ type: '' });
expect(called).to.be.true;
export function createStore(reducer, initialState) {
let listeners = [];
let currentState = initialState;
function getState() {
return currentState;
}
function subscribe(listener) {
listeners.push(listener);
describe('combineReducers', () => {
it('should combine multiple reducers to a single reducer', () => {
const notes = (state = [ 'hello world' ], action) => {
switch(action.type) {
case 'ADD_NOTE':
return [ ...state, action.payload ];
default:
return state;
}
};