Skip to content

Commit

Permalink
v0.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
2234839 committed Jun 13, 2024
1 parent ec9305f commit d086521
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expr",
"version": "0.0.8",
"version": "0.0.9",
"type": "module",
"description": "",
"repository": "https://github.com/2234839/siyuan_expr",
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "expr",
"author": "崮生_子虚",
"url": "https://github.com/2234839/siyuan_expr",
"version": "0.0.8",
"version": "0.0.9",
"minAppVersion": "2.10.3",
"backends": [
"all"
Expand Down
27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ export default class Expr extends Plugin {
IntervalId = 0;
/** 主循环的间隔毫秒数 */
intervalMs = 1_000;
/** 控制sql相关 */
/** 控制sql相关 TODO 添加选项配置 */
intervalUpdateSql = siyuan.bindData({
initValue: true,
that: this,
storageName: "intervalUpdateSql.json",
});
/** 只更新这个时间戳以后的表达式 */
updated = 0;
updated = siyuan.bindData({
initValue: 0,
that: this,
storageName: "updated.json",
});
/** 为 true 代表正在进行求值运算中 */
evalState = false;

Expand Down Expand Up @@ -94,7 +98,7 @@ export default class Expr extends Plugin {

try {
this.evalState = true;
const exprAttr: MergedBlock[] = await sql(
const exprBlock = (await sql(
`SELECT b.*,a.id AS a_id,a."name" AS a_name,a."value" as a_value,a."type" AS a_type,a.block_id AS a_block_id,a.root_id AS a_root_id,a.box AS a_box,a."path" AS a_path
FROM blocks AS b
INNER JOIN attributes AS a
Expand All @@ -105,27 +109,28 @@ export default class Expr extends Plugin {
(
( b.id IN (${exprIDsStr}) )
OR
(CAST(b.updated AS INTEGER) > ${this.updated})
(CAST(b.updated AS INTEGER) > ${this.updated.value()})
)
ORDER BY b.updated DESC;`,
);
if (exprAttr.length > 0) {
await Promise.all(exprAttr.map(this.exprEval.bind(this)));
)) as MergedBlock[] | null;

if (exprBlock && exprBlock.length > 0) {
await Promise.all(exprBlock.map(this.exprEval.bind(this)));
}
} catch (error) {
dev("求值错误", error);
} finally {
this.evalState = false;
this.evalExprIDs.push(...exprIDs);
}
}
/** 记录计算完成的 id ,不再计算 */
evalExprIDs: string[] = [];
async exprEval(block: MergedBlock) {
const evalValue = await eval(block.a_value);

const updated = generateTimestamp();
if (Number(updated) > this.updated) {
this.updated = Number(updated);
if (Number(updated) > this.updated.value()) {
this.updated.set(Number(updated));
}
/** TODO,这里应该要考虑ial中不存在相关字段的情况,需要进行添加而非替换 更新块的update时间戳
* ial = `{: updated="20240604233920" custom-expr="10-11+Math.random()+"2"" custom-expr-value="-0.95897021536132312" id="20240514180539-3zvaoab" style="background-color: var(--b3-font-background4);"} `
Expand All @@ -151,6 +156,8 @@ export default class Expr extends Plugin {
/** 将求值结果更新到块文本 */
await updateBlock("markdown", String(evalValue + "\n" + newKramdownAttr), block.id);
dev("expr:", block.id, block.a_value, evalValue);

this.evalExprIDs.push(block.id);
}
}

Expand Down

0 comments on commit d086521

Please sign in to comment.