From 47db8fb28f5c79af0b69dffc1fc80b29818b13c5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 12:32:48 +0000 Subject: [PATCH 1/9] Initial plan From 5b6026698d957e436f1ed8f7bfc4a4b39cb7db38 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 12:42:44 +0000 Subject: [PATCH 2/9] Refactor output method to use iterator pattern Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- main.ts | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/main.ts b/main.ts index 71224c9..10a78cb 100644 --- a/main.ts +++ b/main.ts @@ -57,6 +57,8 @@ interface NodeInfoWithClient { */ class Search extends Search_ { private data: Set; + private pendingResults: string[] = []; + private currentIndex: number = 0; /** * 构造函数 @@ -83,16 +85,32 @@ class Search extends Search_ { } /** - * 执行搜索并输出结果 - * @param {function} callback - 处理搜索结果的回调函数 - * @returns {*} - 搜索执行结果 + * 执行搜索并输出结果(迭代器形式) + * 如果之前的迭代被中断(break),本次调用会继续执行新的execute() + * @returns {Generator} - 生成器,每次yield一个搜索结果 */ - output(callback: (result: string) => boolean): number { - return super.execute((candidate: Rule) => { + *output(): Generator { + // 如果有待处理的结果(从上次break留下的),先yield它们 + while (this.currentIndex < this.pendingResults.length) { + yield this.pendingResults[this.currentIndex++]; + } + + // 清空待处理列表并重置索引 + this.pendingResults = []; + this.currentIndex = 0; + + // 执行新的搜索 + super.execute((candidate: Rule) => { const result = unparse(candidate.toString()); this.data.add(result); // 保存搜索结果到数据集合 - return callback(result); + this.pendingResults.push(result); + return false; // 继续搜索收集所有结果 }); + + // Yield新搜索到的结果 + while (this.currentIndex < this.pendingResults.length) { + yield this.pendingResults[this.currentIndex++]; + } } /** @@ -138,11 +156,10 @@ class ClusterNode { const begin = Date.now(); const data: string[] = []; // 存储本轮搜索发现的新数据 // 执行搜索引擎,处理搜索结果 - this.engine.output((result: string) => { + for (const result of this.engine.output()) { data.push(result); // 添加到待推送列表(结果已由 engine.output 自动保存) console.log(`Found data: ${result}`); - return false; // 继续搜索 - }); + } // 如果发现新数据,推送到所有其他节点 if (data.length > 0) { for (const id of this.nodes.keys()) { From 59576a446580f76f8ccc3b02bbf92af37c130e45 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 12:45:31 +0000 Subject: [PATCH 3/9] Refactor to eliminate code duplication and improve comments Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- main.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main.ts b/main.ts index 10a78cb..03c0bb9 100644 --- a/main.ts +++ b/main.ts @@ -86,14 +86,12 @@ class Search extends Search_ { /** * 执行搜索并输出结果(迭代器形式) - * 如果之前的迭代被中断(break),本次调用会继续执行新的execute() + * 如果之前的迭代被中断(break),本次调用会先yield剩余结果,然后执行新的execute() * @returns {Generator} - 生成器,每次yield一个搜索结果 */ *output(): Generator { // 如果有待处理的结果(从上次break留下的),先yield它们 - while (this.currentIndex < this.pendingResults.length) { - yield this.pendingResults[this.currentIndex++]; - } + yield* this.yieldPendingResults(); // 清空待处理列表并重置索引 this.pendingResults = []; @@ -108,6 +106,13 @@ class Search extends Search_ { }); // Yield新搜索到的结果 + yield* this.yieldPendingResults(); + } + + /** + * 辅助方法:yield待处理的结果 + */ + private *yieldPendingResults(): Generator { while (this.currentIndex < this.pendingResults.length) { yield this.pendingResults[this.currentIndex++]; } From 71219f358cc8a80f5d488b52fe61266bdff3c32c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 12:47:25 +0000 Subject: [PATCH 4/9] Fix state management to handle early break correctly Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- main.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/main.ts b/main.ts index 03c0bb9..d19b00c 100644 --- a/main.ts +++ b/main.ts @@ -93,20 +93,23 @@ class Search extends Search_ { // 如果有待处理的结果(从上次break留下的),先yield它们 yield* this.yieldPendingResults(); - // 清空待处理列表并重置索引 - this.pendingResults = []; - this.currentIndex = 0; + // 只有当所有待处理结果都已yield后,才清空并执行新搜索 + if (this.currentIndex >= this.pendingResults.length) { + // 清空待处理列表并重置索引 + this.pendingResults = []; + this.currentIndex = 0; - // 执行新的搜索 - super.execute((candidate: Rule) => { - const result = unparse(candidate.toString()); - this.data.add(result); // 保存搜索结果到数据集合 - this.pendingResults.push(result); - return false; // 继续搜索收集所有结果 - }); + // 执行新的搜索 + super.execute((candidate: Rule) => { + const result = unparse(candidate.toString()); + this.data.add(result); // 保存搜索结果到数据集合 + this.pendingResults.push(result); + return false; // 继续搜索收集所有结果 + }); - // Yield新搜索到的结果 - yield* this.yieldPendingResults(); + // Yield新搜索到的结果 + yield* this.yieldPendingResults(); + } } /** From 5e9fbef9ffb5b3aa70e75159e0ea0be5030358fd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 12:55:17 +0000 Subject: [PATCH 5/9] Refactor output to yield one result at a time from execute Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- main.ts | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/main.ts b/main.ts index d19b00c..7ad1934 100644 --- a/main.ts +++ b/main.ts @@ -57,8 +57,6 @@ interface NodeInfoWithClient { */ class Search extends Search_ { private data: Set; - private pendingResults: string[] = []; - private currentIndex: number = 0; /** * 构造函数 @@ -86,38 +84,31 @@ class Search extends Search_ { /** * 执行搜索并输出结果(迭代器形式) - * 如果之前的迭代被中断(break),本次调用会先yield剩余结果,然后执行新的execute() + * 每次yield一个结果时,execute会暂停;下次调用output()会继续execute * @returns {Generator} - 生成器,每次yield一个搜索结果 */ *output(): Generator { - // 如果有待处理的结果(从上次break留下的),先yield它们 - yield* this.yieldPendingResults(); - - // 只有当所有待处理结果都已yield后,才清空并执行新搜索 - if (this.currentIndex >= this.pendingResults.length) { - // 清空待处理列表并重置索引 - this.pendingResults = []; - this.currentIndex = 0; - - // 执行新的搜索 + // 执行搜索,每次返回一个结果后立即停止(返回true) + // execute()应该在下次调用时继续之前的状态 + let hasMore = true; + while (hasMore) { + let resultFound = false; + let currentResult = ""; + super.execute((candidate: Rule) => { const result = unparse(candidate.toString()); - this.data.add(result); // 保存搜索结果到数据集合 - this.pendingResults.push(result); - return false; // 继续搜索收集所有结果 + this.data.add(result); + currentResult = result; + resultFound = true; + // 返回true停止execute,这样每次只处理一个结果 + return true; }); - - // Yield新搜索到的结果 - yield* this.yieldPendingResults(); - } - } - - /** - * 辅助方法:yield待处理的结果 - */ - private *yieldPendingResults(): Generator { - while (this.currentIndex < this.pendingResults.length) { - yield this.pendingResults[this.currentIndex++]; + + if (resultFound) { + yield currentResult; + } else { + hasMore = false; + } } } From 55dc23ba154b3ef96f5de97edfba886f476b0eda Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 12:56:26 +0000 Subject: [PATCH 6/9] Apply code formatting Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- main.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index 7ad1934..fc8fe90 100644 --- a/main.ts +++ b/main.ts @@ -94,7 +94,7 @@ class Search extends Search_ { while (hasMore) { let resultFound = false; let currentResult = ""; - + super.execute((candidate: Rule) => { const result = unparse(candidate.toString()); this.data.add(result); @@ -103,7 +103,7 @@ class Search extends Search_ { // 返回true停止execute,这样每次只处理一个结果 return true; }); - + if (resultFound) { yield currentResult; } else { From 655b1883e38c4dfeb1a02e6a5bdb524aae50fd3f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 12:58:49 +0000 Subject: [PATCH 7/9] Improve comments and code clarity Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- main.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.ts b/main.ts index fc8fe90..9fed9f5 100644 --- a/main.ts +++ b/main.ts @@ -84,14 +84,13 @@ class Search extends Search_ { /** * 执行搜索并输出结果(迭代器形式) - * 每次yield一个结果时,execute会暂停;下次调用output()会继续execute + * 每次迭代调用execute()处理一个结果;如果外部break,下次调用output()会继续新的execute() * @returns {Generator} - 生成器,每次yield一个搜索结果 */ *output(): Generator { - // 执行搜索,每次返回一个结果后立即停止(返回true) - // execute()应该在下次调用时继续之前的状态 - let hasMore = true; - while (hasMore) { + // 循环调用execute(),每次只处理一个结果(callback返回true立即停止) + // 当execute()没有更多结果时(callback未被调用),循环结束 + while (true) { let resultFound = false; let currentResult = ""; @@ -107,7 +106,8 @@ class Search extends Search_ { if (resultFound) { yield currentResult; } else { - hasMore = false; + // execute()没有找到新结果,结束迭代 + break; } } } From a9792411633b4b8e75151085831aa31178fb6212 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 13:17:08 +0000 Subject: [PATCH 8/9] Fix: callback must return false to preserve data structure Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- main.ts | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/main.ts b/main.ts index 9fed9f5..4aa1ec1 100644 --- a/main.ts +++ b/main.ts @@ -57,6 +57,8 @@ interface NodeInfoWithClient { */ class Search extends Search_ { private data: Set; + private pendingResults: string[] = []; + private currentIndex: number = 0; /** * 构造函数 @@ -84,31 +86,32 @@ class Search extends Search_ { /** * 执行搜索并输出结果(迭代器形式) - * 每次迭代调用execute()处理一个结果;如果外部break,下次调用output()会继续新的execute() + * 如果有待处理的结果,先yield它们;否则执行新的execute()收集结果 + * callback必须返回false以保持数据结构完整性 * @returns {Generator} - 生成器,每次yield一个搜索结果 */ *output(): Generator { - // 循环调用execute(),每次只处理一个结果(callback返回true立即停止) - // 当execute()没有更多结果时(callback未被调用),循环结束 - while (true) { - let resultFound = false; - let currentResult = ""; + // 如果有待处理的结果,先yield它们 + while (this.currentIndex < this.pendingResults.length) { + yield this.pendingResults[this.currentIndex++]; + } - super.execute((candidate: Rule) => { - const result = unparse(candidate.toString()); - this.data.add(result); - currentResult = result; - resultFound = true; - // 返回true停止execute,这样每次只处理一个结果 - return true; - }); + // 所有待处理结果已yield完,执行新的execute() + this.pendingResults = []; + this.currentIndex = 0; - if (resultFound) { - yield currentResult; - } else { - // execute()没有找到新结果,结束迭代 - break; - } + // 调用execute()收集新结果,callback必须返回false + super.execute((candidate: Rule) => { + const result = unparse(candidate.toString()); + this.data.add(result); + this.pendingResults.push(result); + // 必须返回false以保持数据结构完整性 + return false; + }); + + // yield新收集的结果 + while (this.currentIndex < this.pendingResults.length) { + yield this.pendingResults[this.currentIndex++]; } } From 2a4490039b027dc4c8cda78c1baf6ae147c9f345 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 12 Dec 2025 13:19:45 +0000 Subject: [PATCH 9/9] Refactor: extract yieldPendingResults helper to reduce duplication Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- main.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main.ts b/main.ts index 4aa1ec1..2ec5dc0 100644 --- a/main.ts +++ b/main.ts @@ -92,9 +92,7 @@ class Search extends Search_ { */ *output(): Generator { // 如果有待处理的结果,先yield它们 - while (this.currentIndex < this.pendingResults.length) { - yield this.pendingResults[this.currentIndex++]; - } + yield* this.yieldPendingResults(); // 所有待处理结果已yield完,执行新的execute() this.pendingResults = []; @@ -110,6 +108,13 @@ class Search extends Search_ { }); // yield新收集的结果 + yield* this.yieldPendingResults(); + } + + /** + * 辅助方法:yield待处理的结果 + */ + private *yieldPendingResults(): Generator { while (this.currentIndex < this.pendingResults.length) { yield this.pendingResults[this.currentIndex++]; }