Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: calculate plaintext from HTML content in advance in rIC #1748

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/routes/_actions/createMakeProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { database } from '../_database/database'
import { decode as decodeBlurhash, init as initBlurhash } from '../_utils/blurhash'
import { mark, stop } from '../_utils/marks'
import { get } from '../_utils/lodash-lite'
import { statusHtmlToPlainText } from '../_utils/statusHtmlToPlainText'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'

async function getNotification (instanceName, timelineType, timelineValue, itemId) {
return {
Expand Down Expand Up @@ -45,6 +47,24 @@ async function decodeAllBlurhashes (statusOrNotification) {
}
}

async function calculatePlainTextContent (statusOrNotification) {
const status = statusOrNotification.status || statusOrNotification.notification.status
if (!status) {
return
}
const originalStatus = status.reblog ? status.reblog : status
const content = originalStatus.content || ''
const mentions = originalStatus.mentions || []
// Calculating the plaintext from the HTML is a non-trivial operation, so we might
// as well do it in advance, while blurhash is being decoded on the worker thread.
await new Promise(resolve => {
scheduleIdleTask(() => {
originalStatus.plainTextContent = statusHtmlToPlainText(content, mentions)
resolve()
})
})
}

export function createMakeProps (instanceName, timelineType, timelineValue) {
let promiseChain = Promise.resolve()

Expand All @@ -64,7 +84,10 @@ export function createMakeProps (instanceName, timelineType, timelineValue) {

async function getStatusOrNotification (itemId) {
const statusOrNotification = await fetchFromIndexedDB(itemId)
await decodeAllBlurhashes(statusOrNotification)
await Promise.all([
decodeAllBlurhashes(statusOrNotification),
calculatePlainTextContent(statusOrNotification)
])
return statusOrNotification
}

Expand Down
4 changes: 1 addition & 3 deletions src/routes/_components/status/Status.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
import { absoluteDateFormatter } from '../../_utils/formatters'
import { composeNewStatusMentioning } from '../../_actions/mention'
import { createStatusOrNotificationUuid } from '../../_utils/createStatusOrNotificationUuid'
import { statusHtmlToPlainText } from '../../_utils/statusHtmlToPlainText'

const INPUT_TAGS = new Set(['a', 'button', 'input', 'textarea', 'label'])
const isUserInputElement = node => INPUT_TAGS.has(node.localName)
Expand Down Expand Up @@ -224,8 +223,7 @@
originalAccountId: ({ originalAccount }) => originalAccount.id,
accountForShortcut: ({ originalAccount, notification }) => notification ? notification.account : originalAccount,
visibility: ({ originalStatus }) => originalStatus.visibility,
mentions: ({ originalStatus }) => originalStatus.mentions || [],
plainTextContent: ({ content, mentions }) => statusHtmlToPlainText(content, mentions),
plainTextContent: ({ originalStatus }) => originalStatus.plainTextContent || '',
plainTextContentOverLength: ({ plainTextContent }) => (
// measureText() is expensive, so avoid doing it when possible.
// Also measureText() typically only makes text shorter, not longer, so we can measure the raw length
Expand Down