mirror of
https://github.com/logseq/logseq.git
synced 2026-04-24 22:25:01 +00:00
refactor(libs): enhance transport listener and improve garbage collection handling
This commit is contained in:
@@ -32,6 +32,7 @@ class LSPluginCaller extends EventEmitter {
|
||||
|
||||
private _status?: 'pending' | 'timeout'
|
||||
private _userModel: any = {}
|
||||
private _syncGCTimer: any = null
|
||||
|
||||
private _call?: (
|
||||
type: string,
|
||||
@@ -70,13 +71,12 @@ class LSPluginCaller extends EventEmitter {
|
||||
const caller = this
|
||||
const isShadowMode = this._pluginLocal != null
|
||||
|
||||
let syncGCTimer: any = 0
|
||||
let syncTag = 0
|
||||
const syncActors = new Map<number, DeferredActor>()
|
||||
const readyDeferred = deferred(1000 * 60)
|
||||
|
||||
const model: any = this._extendUserModel({
|
||||
[LSPMSG_READY]: async (baseInfo) => {
|
||||
[LSPMSG_READY]: async (baseInfo: any) => {
|
||||
// dynamically setup common msg handler
|
||||
model[LSPMSGFn(baseInfo?.pid)] = ({
|
||||
type,
|
||||
@@ -177,7 +177,7 @@ class LSPluginCaller extends EventEmitter {
|
||||
}
|
||||
|
||||
// actors GC
|
||||
syncGCTimer = setInterval(() => {
|
||||
this._syncGCTimer = setInterval(() => {
|
||||
if (syncActors.size > 100) {
|
||||
for (const [k, v] of syncActors) {
|
||||
if (v.settled) {
|
||||
@@ -249,7 +249,7 @@ class LSPluginCaller extends EventEmitter {
|
||||
? `${Math.min((left * 100) / vw, 99)}%`
|
||||
: `${left}px`
|
||||
|
||||
// 45 is height of headbar
|
||||
// 45 is height of head bar
|
||||
top = Math.max(top, 45)
|
||||
top =
|
||||
typeof vh === 'number'
|
||||
@@ -420,7 +420,7 @@ class LSPluginCaller extends EventEmitter {
|
||||
let root: HTMLElement = null
|
||||
if (this._parent) {
|
||||
root = this._getSandboxIframeContainer()
|
||||
await this._parent.destroy()
|
||||
this._parent.destroy()
|
||||
}
|
||||
|
||||
if (this._shadow) {
|
||||
@@ -428,7 +428,13 @@ class LSPluginCaller extends EventEmitter {
|
||||
this._shadow.destroy()
|
||||
}
|
||||
|
||||
root?.parentNode.removeChild(root)
|
||||
root?.parentNode?.removeChild(root)
|
||||
|
||||
// clear GC timer
|
||||
if (this._syncGCTimer) {
|
||||
clearInterval(this._syncGCTimer)
|
||||
this._syncGCTimer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -719,7 +719,7 @@ class PluginLocal extends EventEmitter<
|
||||
return layouts || {}
|
||||
}
|
||||
|
||||
async _saveLayoutsData(data) {
|
||||
async _saveLayoutsData(data: any) {
|
||||
const key = this.id + '_layouts'
|
||||
await invokeHostExportedApi('save_plugin_user_settings', key, data)
|
||||
}
|
||||
|
||||
@@ -64,12 +64,14 @@ export class ParentAPI {
|
||||
|
||||
private addTransportListener(handler: (e: any) => void) {
|
||||
if (this.messagePort) {
|
||||
console.debug('[DEBUG] Using MessagePort for communication:', this.frame.src)
|
||||
// MessagePort delivers MessageEvent too, but without origin/source.
|
||||
this.messagePort.addEventListener('message', handler as any)
|
||||
// Some browsers require start() when using addEventListener.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
;(this.messagePort as any).start?.()
|
||||
} else {
|
||||
console.debug('[DEBUG] Using postMessage for communication:', this.frame.src)
|
||||
this.parent.addEventListener('message', handler, false)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user