Skip to content

Instantly share code, notes, and snippets.

@rsukale
Created July 13, 2016 19:14
Show Gist options
  • Save rsukale/711e1d7136711a8eaa0354a2e9800c0e to your computer and use it in GitHub Desktop.
Save rsukale/711e1d7136711a8eaa0354a2e9800c0e to your computer and use it in GitHub Desktop.
/* global createStateDispatcher */
/* exported adminContentsDispatcher, adminContents$ */
// ---|------|
const adminRecords$ = createStateDispatcher(stream,{
load: {
subject: something,
reducer(state, payload) {
return {...payload};
}
},
click: {
subject: somethingElse,
reducer(state, payload) {
return {...payload};
}
},
click(state, payload) {
return {...payload};
},
remove(state, payload) {
return {...state, rows: state.rows.filter(row => row.id !== payload.id)};
},
loading(state, payload) {
const {id, isLoading = true} = payload;
function rowMap(row) {
return (row.id === id) ? {...row, isLoading: isLoading} : row;
}
return {...state, rows: state.rows.map(rowMap)};
}
}, {rows: []});
tablePlusPlus = table$.merge(... otherStream$)
//const adminRecords$ = adminRecords.streamState({rows: []});
adminRecords.dispatch('click', payload)
adminRecords.dispatch('leftClick', payload)
leftGuy.dispatch('sumethingElse')
function createStateDispatcher(observable$, reducers, initialState) {
}
table$.next(payload)
state$ = Observable.of([{event: 'click', payload}])
stateFromClick$ = Observable
.fromEvent('click', document)
.map(() -> {event: 'click', {}})
streamX = createStateDispatcher(stateFromClick$, reducers, {})
streamY = createStateDispatcher(stateFromClick$, reducersY, {})
bigSubject = new Rx.Subject()
bigSubject.subscribe(({event, payload}) => {
tableSubject2$.next({event: load, payload})
tableSubject1$.next({event: load, payload})
})
// 1) create a subject$ instance (we can have a custom class)
// 2) create createStateDispatcher passing:
// 2.1) observable that maps to {event: 'eventType', payload: {}}
// 2.2) reducers object {click(state, payload) { ... }, ...}
// 2.3) initialState object {...}
// 3) observer components will listen to that state$
// 4) invoke subject.next({event, payload}) in responce to actions
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment