Skip to content

Commit

Permalink
Removing caching
Browse files Browse the repository at this point in the history
  • Loading branch information
tokenshift committed May 6, 2023
1 parent 1af634c commit f13266f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 136 deletions.
36 changes: 12 additions & 24 deletions src/ExpressionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import type { Component } from 'obsidian'
import type { DataviewApi } from 'obsidian-dataview'

import type { Page } from './PageService'
import NestedCache from './NestedCache'

const _evaluateCache = new NestedCache<any>()
const _renderExpressionCache = new NestedCache<Promise<string | null>>()

export default class ExpressionCache {
component: Component
api: DataviewApi
Expand Down Expand Up @@ -38,33 +36,23 @@ export default class ExpressionCache {
}

evaluate<TResult> (expression: string, page: Page) {
return _evaluateCache
.nested(this.parentPage)
.nested(page)
.fetch(expression, () => {
const result = this.api.evaluate(expression, {
...page,
this: this.parentPage
})
const result = this.api.evaluate(expression, {
...page,
this: this.parentPage
})

const value = result.successful
? result.value as TResult
: null
const value = result.successful
? result.value as TResult
: null

return value
})
return value
}

async renderExpression (expression: string, page: Page): Promise<string | null> {
return _renderExpressionCache
.nested(this.parentPage)
.nested(page)
.fetch(expression, async () => {
const value = this.evaluate(expression, page)
if (!value) { return null }
const value = this.evaluate(expression, page)
if (!value) { return null }

return await this.renderFieldValue(value, page)
})
return await this.renderFieldValue(value, page)
}

async renderFieldValue (value: any, page: Page | null = null): Promise<string | null> {
Expand Down
49 changes: 0 additions & 49 deletions src/NestedCache.ts

This file was deleted.

114 changes: 51 additions & 63 deletions src/PageContentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Component, MarkdownPreviewView, TFile } from 'obsidian'
import * as path from './path'
import type PageGalleryPlugin from './PageGalleryPlugin'
import type { Page } from './PageService'
import NestedCache from './NestedCache'

export const IMG_MIME_TYPES = [
'image/jpeg',
Expand All @@ -14,9 +13,6 @@ export const IMG_MIME_TYPES = [
'image/webp'
]

const _contentCache = new NestedCache<Promise<string | null>>()
const _elementCache = new NestedCache<Promise<HTMLElement | null>>()

export default class PageContentService {
plugin: PageGalleryPlugin
component: Component
Expand All @@ -30,35 +26,31 @@ export default class PageContentService {
}

async getContent (page: Page): Promise<string | null> {
return _contentCache.nested(page).fetch('getContent', async () => {
const file = this.plugin.app.vault.getAbstractFileByPath(page.file.path)
const file = this.plugin.app.vault.getAbstractFileByPath(page.file.path)

if (!(file instanceof TFile)) {
return null
}
if (!(file instanceof TFile)) {
return null
}

const content = await this.plugin.app.vault.cachedRead(file as TFile)
const content = await this.plugin.app.vault.cachedRead(file as TFile)

return content
})
return content
}

async getRenderedContent (page: Page): Promise<HTMLElement | null> {
return _elementCache.nested(page).fetch('getRenderedContent', async () => {
const source = await this.getContent(page)
if (!source) { return null }
const source = await this.getContent(page)
if (!source) { return null }

const rendered = document.createElement('div')
const rendered = document.createElement('div')

// The .render-bypass class is included so that we can detect elsewhere
// if we're inside another page gallery render call, to short-circuit
// recursive rendering.
rendered.classList.add('render-bypass')
// The .render-bypass class is included so that we can detect elsewhere
// if we're inside another page gallery render call, to short-circuit
// recursive rendering.
rendered.classList.add('render-bypass')

MarkdownPreviewView.renderMarkdown(source, rendered, page.file.path, this.component)
MarkdownPreviewView.renderMarkdown(source, rendered, page.file.path, this.component)

return rendered
})
return rendered
}

/**
Expand All @@ -68,34 +60,32 @@ export default class PageContentService {
* all .internal-embed[src] elements with img[src] elements.
*/
async getRenderedContentWithImages (page: Page): Promise<HTMLElement | null> {
return _elementCache.nested(page).fetch('getRenderedContentWithImages', async () => {
const rendered = await this.getRenderedContent(page)
if (!rendered) { return null }
const rendered = await this.getRenderedContent(page)
if (!rendered) { return null }

for (const el of rendered.findAll('.internal-embed[src]')) {
const src = el.getAttribute('src')
if (!src) { continue }
for (const el of rendered.findAll('.internal-embed[src]')) {
const src = el.getAttribute('src')
if (!src) { continue }

const ext = src.split('.').pop()
if (!ext) { continue }
const ext = src.split('.').pop()
if (!ext) { continue }

const mimeType = mime.getType(ext)
if (!mimeType || !IMG_MIME_TYPES.contains(mimeType)) { continue }
const mimeType = mime.getType(ext)
if (!mimeType || !IMG_MIME_TYPES.contains(mimeType)) { continue }

const file = this.getClosestMatchingImageSrc(src, page)
if (!file) { continue }
const file = this.getClosestMatchingImageSrc(src, page)
if (!file) { continue }

const imageSrc = this.plugin.app.vault.getResourcePath(file)
const imageSrc = this.plugin.app.vault.getResourcePath(file)

el.innerHTML = ''
const img = document.createElement('img')
img.setAttribute('alt', el.getAttribute('alt') || '')
img.setAttribute('src', imageSrc)
el.appendChild(img)
}
el.innerHTML = ''
const img = document.createElement('img')
img.setAttribute('alt', el.getAttribute('alt') || '')
img.setAttribute('src', imageSrc)
el.appendChild(img)
}

return rendered
})
return rendered
}

/** Attempts to find the *right* matching image, in the event that there's
Expand Down Expand Up @@ -139,31 +129,29 @@ export default class PageContentService {
}

async getFirstImageSrc (page: Page): Promise<string | null> {
return _contentCache.nested(page).fetch('getFirstImageSrc', async () => {
const rendered = await this.getRenderedContent(page)
if (!rendered) { return null }
const rendered = await this.getRenderedContent(page)
if (!rendered) { return null }

for (const el of rendered.findAll('.internal-embed[src], img[src]')) {
const src = el.getAttribute('src')
if (!src) { continue }
for (const el of rendered.findAll('.internal-embed[src], img[src]')) {
const src = el.getAttribute('src')
if (!src) { continue }

if (el.tagName === 'IMG' && src.match(/^https?:\/\//)) {
return src
}
if (el.tagName === 'IMG' && src.match(/^https?:\/\//)) {
return src
}

const ext = src.split('.').pop()
if (!ext) { continue }
const ext = src.split('.').pop()
if (!ext) { continue }

const mimeType = mime.getType(ext)
if (!mimeType || !IMG_MIME_TYPES.contains(mimeType)) { continue }
const mimeType = mime.getType(ext)
if (!mimeType || !IMG_MIME_TYPES.contains(mimeType)) { continue }

const file = this.getClosestMatchingImageSrc(src, page)
if (!file) { continue }
const file = this.getClosestMatchingImageSrc(src, page)
if (!file) { continue }

return this.plugin.app.vault.getResourcePath(file)
}
return this.plugin.app.vault.getResourcePath(file)
}

return null
})
return null
}
}

0 comments on commit f13266f

Please sign in to comment.