FEATURE: Add on_preload for TopicView (#13122)

This allows plugins to load data on fetched posts before each post is
individually serialized.
This commit is contained in:
Daniel Waterworth
2021-05-24 11:46:57 -05:00
committed by GitHub
parent fc6e7dd3f1
commit ca809d2c40
2 changed files with 38 additions and 0 deletions

View File

@@ -4,6 +4,25 @@ class TopicView
MEGA_TOPIC_POSTS_COUNT = 10000
MIN_POST_READ_TIME = 4.0
def self.on_preload(&blk)
(@preload ||= Set.new) << blk
end
def self.cancel_preload(&blk)
if @preload
@preload.delete blk
if @preload.length == 0
@preload = nil
end
end
end
def self.preload(topic_view)
if @preload
@preload.each { |preload| preload.call(topic_view) }
end
end
attr_reader(
:topic,
:posts,
@@ -106,6 +125,8 @@ class TopicView
end
end
TopicView.preload(self)
@draft_key = @topic.draft_key
@draft_sequence = DraftSequence.current(@user, @draft_key)