@x-otto/view-state 0.0.0-stage → 0.4.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build-revision.json +4 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +77 -0
- package/package.json +24 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FoldViewStatePort } from "@x-otto/kernel";
|
|
2
|
+
import * as _$_x_otto_universal0 from "@x-otto/universal";
|
|
3
|
+
|
|
4
|
+
//#region src/component.d.ts
|
|
5
|
+
interface ViewStateComponentOptions {
|
|
6
|
+
home: string;
|
|
7
|
+
store?: FoldViewStatePort;
|
|
8
|
+
}
|
|
9
|
+
declare const ViewState: _$_x_otto_universal0.ComponentDefinition<ViewStateComponentOptions>;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/store.d.ts
|
|
12
|
+
declare const FOLD_VIEW_STATE_VERSION = 1;
|
|
13
|
+
interface FoldViewStateStoreOptions {
|
|
14
|
+
directory: string;
|
|
15
|
+
clock?: {
|
|
16
|
+
now(): number;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
declare function createFoldViewStateStore(options: FoldViewStateStoreOptions): FoldViewStatePort;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { FOLD_VIEW_STATE_VERSION, type FoldViewStateStoreOptions, ViewState, type ViewStateComponentOptions, createFoldViewStateStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { SLOTS } from "@x-otto/kernel";
|
|
3
|
+
import { defineComponent } from "@x-otto/universal";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
6
|
+
import { ensurePrivateDir, withStoreMutex, writeJsonAtomic } from "@x-otto/atomic-fs";
|
|
7
|
+
|
|
8
|
+
//#region src/store.ts
|
|
9
|
+
const FOLD_VIEW_STATE_VERSION = 1;
|
|
10
|
+
function fileNameOf(sessionId) {
|
|
11
|
+
return `${createHash("sha256").update(sessionId).digest("hex")}.json`;
|
|
12
|
+
}
|
|
13
|
+
function stringListOf(raw) {
|
|
14
|
+
if (!Array.isArray(raw)) return void 0;
|
|
15
|
+
if (!raw.every((entry) => typeof entry === "string")) return void 0;
|
|
16
|
+
return [...raw];
|
|
17
|
+
}
|
|
18
|
+
function parseFoldViewState(raw, sessionId) {
|
|
19
|
+
const parsed = JSON.parse(raw);
|
|
20
|
+
if (typeof parsed !== "object" || parsed === null) return void 0;
|
|
21
|
+
const record = parsed;
|
|
22
|
+
if (record["version"] !== 1) return void 0;
|
|
23
|
+
if (record["sessionId"] !== sessionId) return void 0;
|
|
24
|
+
const folded = stringListOf(record["folded"]);
|
|
25
|
+
const expanded = stringListOf(record["expanded"]);
|
|
26
|
+
if (folded === void 0 || expanded === void 0) return void 0;
|
|
27
|
+
const updatedAt = record["updatedAt"];
|
|
28
|
+
return {
|
|
29
|
+
version: 1,
|
|
30
|
+
sessionId,
|
|
31
|
+
folded,
|
|
32
|
+
expanded,
|
|
33
|
+
updatedAt: typeof updatedAt === "number" ? updatedAt : 0
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function createFoldViewStateStore(options) {
|
|
37
|
+
const clock = options.clock ?? Date;
|
|
38
|
+
const pathOf = (sessionId) => join(options.directory, fileNameOf(sessionId));
|
|
39
|
+
return {
|
|
40
|
+
async load(sessionId) {
|
|
41
|
+
let raw;
|
|
42
|
+
try {
|
|
43
|
+
raw = await readFile(pathOf(sessionId), "utf8");
|
|
44
|
+
} catch {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return parseFoldViewState(raw, sessionId);
|
|
49
|
+
} catch {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
async save(sessionId, state) {
|
|
54
|
+
const path = pathOf(sessionId);
|
|
55
|
+
await ensurePrivateDir(options.directory);
|
|
56
|
+
await withStoreMutex(options.directory, async () => {
|
|
57
|
+
await writeJsonAtomic(path, {
|
|
58
|
+
version: 1,
|
|
59
|
+
sessionId,
|
|
60
|
+
folded: [...state.folded],
|
|
61
|
+
expanded: [...state.expanded],
|
|
62
|
+
updatedAt: clock.now()
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/component.ts
|
|
71
|
+
const ViewState = defineComponent("ViewState", (ctx, options) => {
|
|
72
|
+
const store = ctx.resource("fold-view-state", () => options.store ?? createFoldViewStateStore({ directory: join(options.home, "view-state", "fold") }), () => {}, { hmr: "recreate" });
|
|
73
|
+
ctx.provide(SLOTS.foldViewState, () => store);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
export { FOLD_VIEW_STATE_VERSION, ViewState, createFoldViewStateStore };
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-otto/view-state",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0-beta.0",
|
|
4
|
+
"description": "折叠查看态:按会话持久化的显式折叠/展开意图(load / save,双向集合)",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@x-otto/atomic-fs": "^0.4.0-beta.0",
|
|
19
|
+
"@x-otto/kernel": "^0.4.0-beta.0",
|
|
20
|
+
"@x-otto/universal": "^0.4.0-beta.0"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"tag": "beta"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/README.md
DELETED