Skip to content

Commit 6c0f94f

Browse files
authored
DEV: Refactor PostStream with tracked properties and modern tools (#161)
Replace legacy Ember constructs in `PostStream` with tracked properties, `@trackedArray`, and helper methods like `addUniqueValuesToArray`. Simplify initializations, getters, and promise handling with async/await. This refactor improves code clarity and aligns it with current best practices in modern Ember.
1 parent 79f5ae0 commit 6c0f94f

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed
Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { tracked } from "@glimmer/tracking";
12
import EmberObject from "@ember/object";
23
import { reads } from "@ember/object/computed";
3-
import { on } from "@ember-decorators/object";
4-
import { Promise } from "rsvp";
54
import { ajax } from "discourse/lib/ajax";
6-
import discourseComputed from "discourse/lib/decorators";
5+
import { addUniqueValuesToArray } from "discourse/lib/array-tools";
6+
import { trackedArray } from "discourse/lib/tracked-tools";
77
import Category from "discourse/models/category";
88
import RestModel from "discourse/models/rest";
99

@@ -12,62 +12,62 @@ import RestModel from "discourse/models/rest";
1212
// component (in core as well) which expects a `UserStream` instance.
1313

1414
export default class PostStream extends RestModel {
15-
loading = false;
16-
itemsLoaded = 0;
17-
canLoadMore = true;
15+
@tracked loading = false;
16+
@tracked itemsLoaded = 0;
17+
@tracked canLoadMore = true;
1818

19-
@reads("content.lastObject.created_at") lastPostCreatedAt;
19+
@trackedArray content = [];
2020

21-
@on("init")
22-
_initialize() {
23-
this.set("content", []);
24-
}
21+
@reads("content.lastObject.created_at") lastPostCreatedAt;
2522

26-
@discourseComputed("loading", "content.length")
27-
noContent(loading, length) {
28-
return !loading && length === 0;
23+
get noContent() {
24+
return !this.loading && this.content.length === 0;
2925
}
3026

31-
findItems() {
27+
async findItems() {
3228
if (!this.canLoadMore || this.loading) {
33-
return Promise.resolve();
29+
return;
3430
}
3531

36-
this.set("loading", true);
32+
this.loading = true;
3733
const data = {};
34+
3835
if (this.lastPostCreatedAt) {
3936
data.created_before = this.lastPostCreatedAt;
4037
}
41-
return ajax(`/follow/posts/${this.user.username}`, { data })
42-
.then((content) => {
43-
const streamItems = content.posts.map((post) => {
44-
return EmberObject.create({
45-
title: post.topic.title,
46-
postUrl: post.url,
47-
created_at: post.created_at,
48-
category: Category.findById(post.category_id),
49-
topic_id: post.topic.id,
50-
post_id: post.id,
51-
post_number: post.post_number,
5238

53-
username: post.user.username,
54-
name: post.user.name,
55-
avatar_template: post.user.avatar_template,
56-
user_id: post.user.id,
39+
try {
40+
const content = await ajax(`/follow/posts/${this.user.username}`, {
41+
data,
42+
});
43+
44+
const streamItems = content.posts.map((post) => {
45+
return EmberObject.create({
46+
title: post.topic.title,
47+
postUrl: post.url,
48+
created_at: post.created_at,
49+
category: Category.findById(post.category_id),
50+
topic_id: post.topic.id,
51+
post_id: post.id,
52+
post_number: post.post_number,
53+
54+
username: post.user.username,
55+
name: post.user.name,
56+
avatar_template: post.user.avatar_template,
57+
user_id: post.user.id,
5758

58-
excerpt: post.excerpt,
59-
truncated: post.truncated,
60-
});
59+
excerpt: post.excerpt,
60+
truncated: post.truncated,
6161
});
62-
return { posts: streamItems, hasMore: content.extras.has_more };
63-
})
64-
.then(({ posts: streamItems, hasMore }) => {
65-
this.content.addObjects(streamItems);
66-
this.set("itemsLoaded", this.content.length);
67-
this.set("canLoadMore", hasMore);
68-
})
69-
.finally(() => {
70-
this.set("loading", false);
7162
});
63+
64+
const { hasMore } = content.extras;
65+
addUniqueValuesToArray(this.content, streamItems);
66+
67+
this.itemsLoaded = this.content.length;
68+
this.canLoadMore = hasMore;
69+
} finally {
70+
this.loading = false;
71+
}
7272
}
7373
}

0 commit comments

Comments
 (0)