1+ import { tracked } from "@glimmer/tracking" ;
12import EmberObject from "@ember/object" ;
23import { reads } from "@ember/object/computed" ;
3- import { on } from "@ember-decorators/object" ;
4- import { Promise } from "rsvp" ;
54import { 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" ;
77import Category from "discourse/models/category" ;
88import 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
1414export 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