Skip to content
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
3 changes: 2 additions & 1 deletion dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"useTabs": false,
"newLineKind": "lf",
"includes": [
"**/*.{ts,tsx,js,jsx,mjs,cjs}",
"**/*.{ts,tsx,js,jsx,mjs,cjs,mts,cts}",
"**/*.{json,jsonc}",
"**/*.{css,scss}",
"**/*.{md,markdown}",
Expand All @@ -28,6 +28,7 @@
"nextControlFlowPosition": "sameLine",
"spaceSurroundingProperties": false,
"importDeclaration.forceMultiLine": "whenMultiple",
"importDeclaration.spaceSurroundingNamedImports": false,
"importDeclaration.sortNamedImports": "maintain"
},
"json": {
Expand Down
30 changes: 22 additions & 8 deletions src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ class KernelPlugin {
);

// ── siyuan.storage 示例

// 监听插件存储目录的文件系统事件。
// Watch filesystem events in the plugin storage directory.
await storage.watcher.add("./");

// put:将 UTF-8 字符串写入相对于插件数据目录的路径。
// put: write a UTF-8 string to a path relative to the plugin data dir.
await storage.put("demo.txt", JSON.stringify(new Date().toISOString()));
Expand Down Expand Up @@ -391,7 +396,11 @@ class KernelPlugin {
* After broadcasting, close any open client-side connections to avoid resource leaks in the kernel process.
*/
private async onunload(): Promise<void> {
const {rpc, logger} = this.siyuan;
const {rpc, logger, storage} = this.siyuan;

// 解除对插件存储目录的文件系统事件的监听。
// Unwatch filesystem events in the plugin storage directory.
await storage.watcher.remove("./");

// 解绑在 onload 中注册的 RPC 方法。
// Unbind the RPC method registered in onload.
Expand Down Expand Up @@ -591,9 +600,8 @@ class KernelPlugin {
* Demonstrates the SSE server handler at `GET /plugin/private/<name>/*path` (Server-Sent Events).
*
* `request.port.onopen` fires once the SSE stream is ready.
* Call `port.send(eventType, data)` inside `onopen` or later to push events.
* `eventType` maps to the SSE `event:` field.
* `data` maps to `data:`.
* Call `port.send(event)` inside `onopen` or later to push events.
*
* `send` is synchronous.
*
* The connection stays open until `port.close()` is called or the client disconnects.
Expand All @@ -607,11 +615,17 @@ class KernelPlugin {
request.port.onopen = async (event) => {
await logger.debug("sse server: port open", event);
// send 是同步的。
// eventType 会成为 SSE 的 `event:` 字段。
// send is synchronous.
// eventType becomes the SSE `event:` field.
request.port.send("message", "Connected to plugin SSE!");
request.port.send("update", JSON.stringify({ts: Date.now()}));
const now = Date.now();
request.port.send({
event: "update",
data: JSON.stringify({ts: now}),
id: now.toString(),
retry: 5000,
});
request.port.send({
data: "Connected to plugin SSE!",
});
};
request.port.onclose = async (event) => {
await logger.debug("sse server: port close", event);
Expand Down