FIX: Recovering a post does not insert it back into the stream correctly.

This commit is contained in:
Guo Xiang Tan 2018-06-29 11:41:44 +08:00
parent 252e5574cc
commit d1c3eb6bfa
3 changed files with 41 additions and 4 deletions

View File

@ -1096,13 +1096,13 @@ export default Ember.Controller.extend(BufferedContent, {
} }
case "deleted": { case "deleted": {
postStream postStream
.triggerDeletedPost(data.id, data.post_number) .triggerDeletedPost(data.id)
.then(() => refresh({ id: data.id })); .then(() => refresh({ id: data.id }));
break; break;
} }
case "recovered": { case "recovered": {
postStream postStream
.triggerRecoveredPost(data.id, data.post_number) .triggerRecoveredPost(data.id)
.then(() => refresh({ id: data.id })); .then(() => refresh({ id: data.id }));
break; break;
} }

View File

@ -565,8 +565,9 @@ export default RestModel.extend({
return this.triggerChangedPost(postId, new Date()); return this.triggerChangedPost(postId, new Date());
} else { } else {
// need to insert into stream // need to insert into stream
const url = "/posts/" + postId; const url = `/posts/${postId}`;
const store = this.store; const store = this.store;
return ajax(url).then(p => { return ajax(url).then(p => {
const post = store.createRecord("post", p); const post = store.createRecord("post", p);
const stream = this.get("stream"); const stream = this.get("stream");
@ -591,7 +592,9 @@ export default RestModel.extend({
}); });
if (index < posts.length) { if (index < posts.length) {
this.get("postsWithPlaceholders").refreshAll(() => {
posts.insertAt(index, post); posts.insertAt(index, post);
});
} else { } else {
if (post.post_number < posts[posts.length - 1].post_number + 5) { if (post.post_number < posts[posts.length - 1].post_number + 5) {
this.appendMore(); this.appendMore();

View File

@ -681,6 +681,40 @@ QUnit.test("loadedAllPosts when the id changes", assert => {
); );
}); });
QUnit.test("triggerRecoveredPost", async assert => {
const postStream = buildStream(4567);
const store = postStream.store;
[1, 2, 3, 5].forEach(id => {
postStream.appendPost(
store.createRecord("post", { id: id, post_number: id })
);
});
const response = object => {
return [200, { "Content-Type": "application/json" }, object];
};
// prettier-ignore
server.get("/posts/4", () => { // eslint-disable-line no-undef
return response({ id: 4, post_number: 4 });
});
assert.equal(
postStream.get("postsWithPlaceholders.length"),
4,
"it should return the right length"
);
await postStream.triggerRecoveredPost(4);
assert.equal(
postStream.get("postsWithPlaceholders.length"),
5,
"it should return the right length"
);
});
QUnit.test("comitting and triggerNewPostInStream race condition", assert => { QUnit.test("comitting and triggerNewPostInStream race condition", assert => {
const postStream = buildStream(4964); const postStream = buildStream(4964);
const store = postStream.store; const store = postStream.store;