Skip to content

Commit

Permalink
Version bump, updated lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Azevedo committed Mar 9, 2015
1 parent 568a3bd commit faf6792
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 54 deletions.
69 changes: 34 additions & 35 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
component: routeComponent,
context: context
});
config.start(initialState, function(){});
config.start(initialState);
root = React.render(rootElement, rootDomNode);
initialState.onChange(function(){
return root.setState({
Expand All @@ -52,43 +52,42 @@
return routes.start(config.routes(), root, initialState);
},
render: function(path){
return new bluebird(function(res, rej){
var initialState, ref$, routeComponent, context, routeInit, rootElement;
initialState = cursor(config.getInitialState());
ref$ = routes.resolve(path, config.routes()), routeComponent = ref$[0], context = ref$[1], routeInit = ref$[2];
rootElement = appComponent({
initialState: initialState,
component: routeComponent,
context: context
});
return config.start(initialState, function(){
if (!routeInit) {
return res([initialState.deref(), React.renderToString(rootElement)]);
}
return routeInit(initialState, context, function(){
return res([initialState.deref(), React.renderToString(rootElement)]);
});
});
var appState, ref$, routeComponent, context, routeInit, rootElement, transaction;
appState = cursor(config.getInitialState());
ref$ = routes.resolve(path, config.routes()), routeComponent = ref$[0], context = ref$[1], routeInit = ref$[2];
rootElement = appComponent({
initialState: appState,
component: routeComponent,
context: context
});
transaction = appState.startTransaction();
config.start(appState);
if (routeInit) {
routeInit(appState, context);
}
return appState.endTransaction(transaction).then(function(){
return [appState.deref(), React.renderToString(rootElement)];
});
},
processForm: function(path, postData){
return new bluebird(function(res, rej){
var initialState, ref$, routeComponent, context, routeInit, rootElement;
initialState = cursor(config.getInitialState());
ref$ = routes.resolve(path, config.routes()), routeComponent = ref$[0], context = ref$[1], routeInit = ref$[2];
rootElement = appComponent({
initialState: initialState,
component: routeComponent,
context: context
});
return config.start(initialState, function(){
if (!routeInit) {
return res(serverRendering.processForm(rootElement, initialState, postData, path));
}
return routeInit(initialState, context, function(){
return res(serverRendering.processForm(rootElement, initialState, postData, path));
});
});
var appState, ref$, routeComponent, context, routeInit, rootElement, transaction, location;
appState = cursor(config.getInitialState());
ref$ = routes.resolve(path, config.routes()), routeComponent = ref$[0], context = ref$[1], routeInit = ref$[2];
rootElement = appComponent({
initialState: appState,
component: routeComponent,
context: context
});
transaction = appState.startTransaction();
config.start(appState);
if (routeInit) {
routeInit(appState, context);
}
location = serverRendering.processForm(rootElement, appState, postData, path);
return appState.endTransaction(transaction).then(function(){
var body;
body = !location ? React.renderToString(rootElement) : null;
return [appState.deref(), body, location];
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
filename: 'app.js',
contentBase: path.join(paths.app.abs, paths['public']),
hot: true,
quiet: true,
noInfo: true,
quiet: false,
noInfo: false,
watchDelay: 200
});
return server.listen(3001, 'localhost');
Expand Down
44 changes: 38 additions & 6 deletions lib/cursor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
(function(){
var Immutable, ref$, map, take, reverse, each, join, split, isType, empty, arrayCursor, objectCursor, notifyListeners, flushUpdates, performUpdate, Cursor;
var Immutable, bluebird, ref$, map, take, reverse, each, join, split, isType, empty, UpdateTransaction, isPromise, arrayCursor, objectCursor, notifyListeners, flushUpdates, performUpdate, Cursor, toString$ = {}.toString;
Immutable = require('immutable');
bluebird = require('bluebird');
ref$ = require('prelude-ls'), map = ref$.map, take = ref$.take, reverse = ref$.reverse, each = ref$.each, join = ref$.join, split = ref$.split, isType = ref$.isType, empty = ref$.empty;
UpdateTransaction = function(){
this.promises = [];
};
isPromise = function(it){
return it && it.then && toString$.call(it.then).slice(8, -1) === 'Function';
};
arrayCursor = function(root, data, len, path){
var array, ref$;
array = map(function(it){
Expand All @@ -18,14 +25,15 @@
array._root = root || this;
array._data = data ? Immutable.fromJS(data) : null;
array._listeners = {};
array._transactions = [];
array._updates = [];
ref$ = Cursor.prototype, array.get = ref$.get, array.deref = ref$.deref, array.raw = ref$.raw, array.update = ref$.update, array._swap = ref$._swap, array.onChange = ref$.onChange;
return array;
};
objectCursor = function(root, data, path){
return new Cursor(root, data, path);
};
notifyListeners = function(listeners, path, newData){
notifyListeners = function(listeners, transactions, path, newData){
var paths;
paths = each(function(path){
var key;
Expand All @@ -35,12 +43,18 @@
return;
}
return each(function(it){
var payload;
var payload, maybePromise;
payload = newData.getIn(path);
if (payload.toJS) {
payload = payload.toJS();
}
return it(payload);
maybePromise = it(payload);
if (isPromise(maybePromise)) {
return each(function(it){
return it.promises.push(maybePromise);
})(
transactions);
}
})(
listeners[key]);
})(
Expand Down Expand Up @@ -83,13 +97,14 @@
? newVal
: cursor._root._data.setIn(cursor._path, newVal);
cursor._root._swap(newData);
return notifyListeners(cursor._root._listeners, cursor._path, newData);
return notifyListeners(cursor._root._listeners, cursor._root._transactions, cursor._path, newData);
};
Cursor = function(root, data, path){
this._path = path;
this._root = root || this;
this._data = data ? Immutable.fromJS(data) : null;
this._listeners = {};
this._transactions = [];
this._updates = [];
return this;
};
Expand Down Expand Up @@ -125,7 +140,7 @@
if (!(updates.length < 2)) {
return;
}
while (updates.length > 0) {
while (!empty(updates)) {
ref$ = updates[0], cursor = ref$[0], update = ref$[1];
performUpdate(cursor, update);
results$.push(updates.shift());
Expand All @@ -144,6 +159,23 @@
(ref$ = this._root._listeners)[key] || (ref$[key] = []);
return this._root._listeners[key].push(cbk);
};
Cursor.prototype.startTransaction = function(){
var t;
t = new UpdateTransaction();
this._root._transactions.push(t);
return t;
};
Cursor.prototype.endTransaction = function(transaction){
var i;
i = this._root._transactions.indexOf(transaction);
if (i < 0) {
throw new Error("Transaction isn't running");
}
if (empty(transaction.promises)) {
return bluebird.resolve();
}
return bluebird.all(transaction.promises);
};
module.exports = function(data){
if (isType('Array', data)) {
return arrayCursor(null, data, data.length, []);
Expand Down
8 changes: 3 additions & 5 deletions lib/server-rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,17 @@
return ReactUpdates.flushBatchedUpdates();
};
processForm = function(rootElement, initialState, postData, path){
var instance, ref$, form, inputs, that, state, body;
var instance, ref$, form, inputs, that;
configureReact();
resetRedirect();
instance = renderTree(rootElement);
ref$ = extractElements(path, postData, instance), form = ref$[0], inputs = ref$[1];
changeInputs(inputs, postData);
submitForm(form);
if (that = redirectLocation) {
return [null, null, that];
return that;
}
state = initialState.deref();
body = React.renderToString(rootElement);
return [state, body, null];
return null;
};
resetRedirect = function(){
return redirectLocation = null;
Expand Down
4 changes: 2 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
options == null && (options = defaults);
app = options.app || require(options.paths.app.rel);
get = function(req, res){
console.log("GET ", req.originalUrl);
console.log("GET", req.originalUrl);
return reflexGet(app, req.originalUrl, options).then(function(it){
return res.send(it);
});
};
post = function(req, res){
var postData;
postData = req.body;
console.log("POST ", req.originalUrl, postData);
console.log("POST", req.originalUrl, postData);
return reflexPost(app, req.originalUrl, postData, options).spread(function(status, headers, body){
console.log(status + "", headers);
return res.status(status).set(headers).send(body);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reflex",
"version": "0.0.2",
"version": "0.0.3",
"description": "Web application framework for React",
"homepage": "https://github.com/redbadger/reflex",
"repository": {
Expand Down
5 changes: 2 additions & 3 deletions src/bundler.ls
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ exports.bundle = (paths, watch, changed) ->
root: path.join paths.reflex.abs, 'node_modules'
fallback: path.join paths.app.abs, 'node_modules'


plugins: [ new webpack.DefinePlugin 'process.env': browser-env ]

module:
Expand Down Expand Up @@ -83,8 +82,8 @@ exports.bundle = (paths, watch, changed) ->
filename: 'app.js'
content-base: path.join paths.app.abs, paths.public
hot: true # Enable hot loading
quiet: true
no-info: true
quiet: false
no-info: false
watch-delay: 200

server.listen 3001, 'localhost'
Expand Down

0 comments on commit faf6792

Please sign in to comment.