-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui/list): separate the functions of list
- Loading branch information
Showing
43 changed files
with
835 additions
and
738 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import type { NotifyData, PageData, CommentData, DataManagerApi, ListFetchParams, ListLastFetchData } from '~/types/artalk-data' | ||
import type { EventPayloadMap } from '~/types/event' | ||
import EventManager from './lib/event-manager' | ||
|
||
export class DataManager implements DataManagerApi { | ||
private loading: boolean = false | ||
private listLastFetch?: ListLastFetchData | ||
private comments: CommentData[] = [] // Note: 无层级结构 + 无须排列 | ||
private unreads: NotifyData[] = [] | ||
private page?: PageData | ||
|
||
constructor( | ||
protected events: EventManager<EventPayloadMap> | ||
) {} | ||
|
||
getLoading() { | ||
return this.loading | ||
} | ||
|
||
setLoading(val: boolean) { | ||
this.loading = val | ||
} | ||
|
||
getListLastFetch() { | ||
return this.listLastFetch | ||
} | ||
|
||
setListLastFetch(val: ListLastFetchData) { | ||
this.listLastFetch = val | ||
} | ||
|
||
// ------------------------------------------------------------------- | ||
// Comments | ||
// ------------------------------------------------------------------- | ||
getComments() { | ||
return this.comments | ||
} | ||
|
||
fetchComments(params: Partial<ListFetchParams>) { | ||
this.events.trigger('list-fetch', params) | ||
} | ||
|
||
findComment(id: number) { | ||
return this.comments.find(c => c.id === id) | ||
} | ||
|
||
clearComments() { | ||
this.comments = [] | ||
this.events.trigger('list-loaded', this.comments) | ||
} | ||
|
||
loadComments(comments: CommentData[]) { | ||
this.events.trigger('list-load', comments) | ||
|
||
this.comments = comments | ||
|
||
this.events.trigger('list-loaded', comments) | ||
} | ||
|
||
insertComment(comment: CommentData) { | ||
this.comments.push(comment) | ||
|
||
this.events.trigger('comment-inserted', comment) | ||
this.events.trigger('list-loaded', this.comments) // list-loaded should always keep the last | ||
} | ||
|
||
updateComment(comment: CommentData) { | ||
this.comments = this.comments.map(c => { | ||
if (c.id === comment.id) return comment | ||
return c | ||
}) | ||
|
||
this.events.trigger('comment-updated', comment) | ||
this.events.trigger('list-loaded', this.comments) | ||
} | ||
|
||
deleteComment(id: number) { | ||
const comment = this.comments.find(c => c.id === id) | ||
if (!comment) throw new Error(`Comment ${id} not found`) | ||
this.comments = this.comments.filter(c => c.id !== id) | ||
|
||
this.events.trigger('comment-deleted', comment) | ||
this.events.trigger('list-loaded', this.comments) | ||
} | ||
|
||
// ------------------------------------------------------------------- | ||
// Unreads | ||
// ------------------------------------------------------------------- | ||
getUnreads() { | ||
return this.unreads | ||
} | ||
|
||
updateUnreads(unread: NotifyData[]) { | ||
this.unreads = unread | ||
|
||
this.events.trigger('unreads-updated', this.unreads) | ||
} | ||
|
||
// ------------------------------------------------------------------- | ||
// Page | ||
// ------------------------------------------------------------------- | ||
getPage() { | ||
return this.page | ||
} | ||
|
||
updatePage(pageData: PageData) { | ||
this.page = pageData | ||
|
||
this.events.trigger('page-loaded', pageData) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.