Skip to content

Commit

Permalink
Rehydrate based on resolution keys
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdustan committed Apr 22, 2015
1 parent 5f0efec commit 8164960
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
32 changes: 14 additions & 18 deletions src/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export default class Resolver {
throw new ReferenceError(`${container.constructor.displayName} should have an ID`);
}

const state = this.states.hasOwnProperty(id) ? this.states[id] : {
fulfilled: false,
rejected: false,
values: {},
};
const state = this.states.hasOwnProperty(id) ? this.states[id] : this.rehydrate(id);

if (!this.states.hasOwnProperty(id)) {
this.states[id] = state;
Expand All @@ -72,6 +68,17 @@ export default class Resolver {
throw new Error(`${this.constructor.displayName} was rejected: ${error}`);
}

rehydrate(id) {
if (typeof __resolver__ === 'undefined') {
return {
fulfilled: false,
rejected: false,
values: {},
};
}
return __resolver__[id];
}

resolve(container, callback) {
const asyncProps = container.props.resolve || {};
const state = this.getContainerState(container);
Expand All @@ -87,17 +94,6 @@ export default class Resolver {

return true;
})
// Assign rehydration values
.filter((asyncProp) => {
if (typeof __resolver__ === 'object') {
if (__resolver__.hasOwnProperty(asyncProp)) {
state.values[asyncProp] = __resolver__[asyncProp];
return false;
}
}

return true;
})
// Filter out pre-loaded values
.filter((asyncProp) => {
return !state.values.hasOwnProperty(asyncProp);
Expand Down Expand Up @@ -182,7 +178,7 @@ export default class Resolver {

var html = React.renderToString(context);
return {
data: data,
data: resolver.states,
toString() { return html; }
};
});
Expand All @@ -199,7 +195,7 @@ export default class Resolver {

var html = React.renderToStaticMarkup(context);
return {
data: data,
data: resolver.states,
toString() { return html; }
};
});
Expand Down
12 changes: 10 additions & 2 deletions test/Container.element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ describe("<Container />", function() {

context("when keys are rehydrating", function() {
before(function() {
global.__resolver__ = { user: "Exists" };
global.__resolver__ = {
'.0': {
values: {
fulfilled: false,
rejected: false,
user: "Exists"
}
}
};
});

after(function() {
Expand Down Expand Up @@ -130,7 +138,7 @@ describe("<Container />", function() {
/>
);

assert.equal(actual, `<code>${JSON.stringify(global.__resolver__)}</code>`);
assert.equal(actual, `<code>${JSON.stringify(global.__resolver__['.0'].values)}</code>`);
});
});
});
Expand Down

0 comments on commit 8164960

Please sign in to comment.