-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
150 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,57 @@ | ||
/* | ||
* Copyright (c) 2024 by frostime. All Rights Reserved. | ||
* @Author : frostime | ||
* @Date : 2024-12-01 11:21:44 | ||
* @FilePath : /src/core/gc.ts | ||
* @LastEditTime : 2024-12-03 15:18:18 | ||
* @Description : | ||
*/ | ||
import { DataView } from "./data-view"; | ||
|
||
const dataviews = new WeakMap<object, WeakRef<DataView>[]>(); | ||
const dataviews = new Map<DocumentId, WeakRef<DataView>[]>(); | ||
|
||
const registry = new FinalizationRegistry((docId: string) => { | ||
const views = dataviews.get(docId); | ||
if (views) { | ||
views.forEach(view => { | ||
const dataView = view.deref(); | ||
if (dataView) { | ||
console.debug('FinalizationRegistry dispose dataView', dataView); | ||
dataView.dispose(); | ||
} | ||
}); | ||
dataviews.delete(docId); | ||
} | ||
}); | ||
|
||
/** | ||
* Register DataView for Garbage Collection on Document Closed | ||
* @param docId | ||
* @param dataView | ||
*/ | ||
export const registerProtyleGC = (docId: DocumentId, dataView: DataView) => { | ||
const key = { id: docId }; | ||
if (!dataviews.has(key)) { | ||
dataviews.set(key, []); | ||
if (!dataviews.has(docId)) { | ||
dataviews.set(docId, []); | ||
} | ||
const views = dataviews.get(key); | ||
const views = dataviews.get(docId); | ||
views.push(new WeakRef(dataView)); | ||
|
||
// 注册 dataView 到 FinalizationRegistry | ||
registry.register(dataView, docId); | ||
} | ||
|
||
export const onProtyleDestroyed = ({ detail }) => { | ||
console.log('closed protyle'); | ||
console.log(detail); | ||
const rootID = detail.protyle.block.rootID; | ||
if (!(rootID in dataviews)) return; | ||
dataviews[rootID].forEach(view => { | ||
|
||
if (!dataviews.has(rootID)) return; | ||
|
||
const views = dataviews.get(rootID); | ||
views.forEach(view => { | ||
const dataView = view.deref(); | ||
if (dataView) { | ||
console.debug('onProtyleDestroyed dispose dataView', dataView); | ||
dataView.dispose(); | ||
} | ||
}); | ||
delete dataviews[rootID]; | ||
dataviews.delete(rootID); | ||
} |
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