Created
September 22, 2015 12:35
-
-
Save staltz/0e3cdf20a0b7696e7943 to your computer and use it in GitHub Desktop.
Cycle.js without sanity checks and comments in a single file. Don't use this, use normal Cycle.js. :-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Rx = require(`rx`) | |
function makeRequestProxies(drivers) { | |
let requestProxies = {} | |
for (let name in drivers) { | |
if (drivers.hasOwnProperty(name)) { | |
requestProxies[name] = new Rx.ReplaySubject(1) | |
} | |
} | |
return requestProxies | |
} | |
function callDrivers(drivers, requestProxies) { | |
let responses = {} | |
for (let name in drivers) { | |
if (drivers.hasOwnProperty(name)) { | |
responses[name] = drivers[name](requestProxies[name], name) | |
} | |
} | |
return responses | |
} | |
function attachDisposeToRequests(requests, replicationSubscription) { | |
Object.defineProperty(requests, `dispose`, { | |
enumerable: false, | |
value: () => { replicationSubscription.dispose() }, | |
}) | |
return requests | |
} | |
function makeDisposeResponses(responses) { | |
return function dispose() { | |
for (let name in responses) { | |
if (responses.hasOwnProperty(name) && | |
typeof responses[name].dispose === `function`) | |
{ | |
responses[name].dispose() | |
} | |
} | |
} | |
} | |
function attachDisposeToResponses(responses) { | |
Object.defineProperty(responses, `dispose`, { | |
enumerable: false, | |
value: makeDisposeResponses(responses), | |
}) | |
return responses | |
} | |
function replicateMany(observables, subjects) { | |
return Rx.Observable.create(observer => { | |
let subscription = new Rx.CompositeDisposable() | |
setTimeout(() => { | |
for (let name in observables) { | |
if (observables.hasOwnProperty(name) && | |
subjects.hasOwnProperty(name) && | |
!subjects[name].isDisposed) | |
{ | |
subscription.add( | |
observables[name] | |
.subscribe(subjects[name].asObserver()) | |
) | |
} | |
} | |
observer.onNext(subscription) | |
}, 1) | |
return function dispose() { | |
subscription.dispose() | |
for (let x in subjects) { | |
if (subjects.hasOwnProperty(x)) { | |
subjects[x].dispose() | |
} | |
} | |
} | |
}) | |
} | |
function run(main, drivers) { | |
let requestProxies = makeRequestProxies(drivers) | |
let responses = callDrivers(drivers, requestProxies) | |
let requests = main(responses) | |
let subscription = replicateMany(requests, requestProxies).subscribe() | |
let requestsWithDispose = attachDisposeToRequests(requests, subscription) | |
let responsesWithDispose = attachDisposeToResponses(responses) | |
return [requestsWithDispose, responsesWithDispose] | |
} | |
let Cycle = {run, Rx: Rx} | |
module.exports = Cycle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just one problem in comparison to Redux: