Skip to content

Commit c7f5f1a

Browse files
authored
DEV: Replace deprecated array methods in post-stream.js (#162)
Refactor `post-stream.js` to eliminate deprecated array methods, enhancing code stability and maintainability. The changes include replacing `set` with tracked properties and utilizing `addUniqueValuesToArray` for managing the content array.
1 parent 6c0f94f commit c7f5f1a

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

assets/javascripts/discourse/models/post-stream.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { tracked } from "@glimmer/tracking";
22
import EmberObject from "@ember/object";
3-
import { reads } from "@ember/object/computed";
3+
import { dependentKeyCompat } from "@ember/object/compat";
44
import { ajax } from "discourse/lib/ajax";
55
import { addUniqueValuesToArray } from "discourse/lib/array-tools";
66
import { trackedArray } from "discourse/lib/tracked-tools";
@@ -12,14 +12,17 @@ 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-
@tracked loading = false;
16-
@tracked itemsLoaded = 0;
1715
@tracked canLoadMore = true;
18-
16+
@tracked itemsLoaded = 0;
17+
@tracked loading = false;
1918
@trackedArray content = [];
2019

21-
@reads("content.lastObject.created_at") lastPostCreatedAt;
20+
@dependentKeyCompat
21+
get lastPostCreatedAt() {
22+
return this.content.at(-1)?.created_at;
23+
}
2224

25+
@dependentKeyCompat
2326
get noContent() {
2427
return !this.loading && this.content.length === 0;
2528
}
@@ -31,16 +34,13 @@ export default class PostStream extends RestModel {
3134

3235
this.loading = true;
3336
const data = {};
34-
3537
if (this.lastPostCreatedAt) {
3638
data.created_before = this.lastPostCreatedAt;
3739
}
38-
3940
try {
4041
const content = await ajax(`/follow/posts/${this.user.username}`, {
4142
data,
4243
});
43-
4444
const streamItems = content.posts.map((post) => {
4545
return EmberObject.create({
4646
title: post.topic.title,
@@ -60,12 +60,9 @@ export default class PostStream extends RestModel {
6060
truncated: post.truncated,
6161
});
6262
});
63-
64-
const { hasMore } = content.extras;
6563
addUniqueValuesToArray(this.content, streamItems);
66-
6764
this.itemsLoaded = this.content.length;
68-
this.canLoadMore = hasMore;
65+
this.canLoadMore = content.extras.has_more;
6966
} finally {
7067
this.loading = false;
7168
}

0 commit comments

Comments
 (0)