From f8d9f4ceda8be011f3e8d803d374aa5cd6bba715 Mon Sep 17 00:00:00 2001 From: hizzgdev Date: Fri, 19 Sep 2025 02:59:49 +0800 Subject: [PATCH] clean code in add_nodes --- package-lock.json | 33 +++--------------------- src/jsmind.js | 65 ++++++++++++++--------------------------------- 2 files changed, 22 insertions(+), 76 deletions(-) diff --git a/package-lock.json b/package-lock.json index 273f4a73..31444a48 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "license": "BSD-3-Clause", "devDependencies": { "@rollup/plugin-terser": "^0.4.4", - "cross-env": "^10.0.0", "http-server": "^14.1.1", "jest": "^28.1.0", "jest-environment-jsdom": "^28.1.0", @@ -576,13 +575,6 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, - "node_modules/@epic-web/invariant": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@epic-web/invariant/-/invariant-1.0.0.tgz", - "integrity": "sha512-lrTPqgvfFQtR/eY/qkIzp98OGdNJu0m5ji3q/nJI8v3SXkRKEnWiOxMmbvcSoAIzv/cGiuvRy57k4suKQSAdwA==", - "dev": true, - "license": "MIT" - }, "node_modules/@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmmirror.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -1712,30 +1704,11 @@ "node": ">= 0.4.0" } }, - "node_modules/cross-env": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-10.0.0.tgz", - "integrity": "sha512-aU8qlEK/nHYtVuN4p7UQgAwVljzMg8hB4YK5ThRqD2l/ziSnryncPNn7bMLt5cFYsKVKBh8HqLqyCoTupEUu7Q==", - "dev": true, - "license": "MIT", - "dependencies": { - "@epic-web/invariant": "^1.0.0", - "cross-spawn": "^7.0.6" - }, - "bin": { - "cross-env": "dist/bin/cross-env.js", - "cross-env-shell": "dist/bin/cross-env-shell.js" - }, - "engines": { - "node": ">=20" - } - }, "node_modules/cross-spawn": { - "version": "7.0.6", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", - "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", + "version": "7.0.3", + "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz", + "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", "dev": true, - "license": "MIT", "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", diff --git a/src/jsmind.js b/src/jsmind.js index 9420cc11..3d8310ac 100644 --- a/src/jsmind.js +++ b/src/jsmind.js @@ -431,20 +431,11 @@ export default class jsMind { } var node = this.mind.add_node(parent_node, node_id, topic, data, dir); - if (!node) { - return null; // Mind-level failure - } - - try { + if (!!node) { this.view.add_node(node); this.view.reset_node_custom_style(node); - return node; // Success - } catch (e) { - // View operations failed, rollback mind changes - logger.error('View operation failed, rolling back node:', e); - this.mind.remove_node(node); - return null; } + return node; } /** @@ -515,23 +506,13 @@ export default class jsMind { return []; } - let all_created_nodes = []; const expected_count = this._count_expected_nodes(nodes_data); + let created_nodes = nodes_data + .map(node_data => this._add_nodes_recursive(the_parent_node, node_data)) + .flat() + .filter(n => n !== null); - try { - // Process nodes and flatten results - for (const node_data of nodes_data) { - const created_nodes = this._add_nodes_recursive(the_parent_node, node_data); - all_created_nodes = all_created_nodes.concat(created_nodes); - } - - // Filter null values - all_created_nodes = all_created_nodes.filter(node => node !== null); - } catch (e) { - logger.error('Failed to add nodes:', e); - } - - const actual_count = all_created_nodes.length; + const actual_count = created_nodes.length; // Atomic operation: either all nodes succeed or cleanup all partial nodes if (actual_count === expected_count) { @@ -540,17 +521,15 @@ export default class jsMind { this.invoke_event_handle(EventType.edit, { evt: 'add_nodes', data: [the_parent_node.id, nodes_data], - nodes: all_created_nodes.map(node => node.id), + nodes: created_nodes.map(node => node.id), }); - return all_created_nodes; + return created_nodes; } else { // Cleanup partially created nodes to ensure atomicity - if (all_created_nodes.length > 0) { - logger.warn( - `Expected ${expected_count} nodes, but only created ${actual_count}. Cleaning up...` - ); - this._cleanup_partial_nodes(all_created_nodes); - } + logger.warn( + `Expected ${expected_count} nodes, but only created ${actual_count}. Cleaning up...` + ); + this._cleanup_partial_nodes(created_nodes); return []; } } @@ -563,7 +542,7 @@ export default class jsMind { * @returns {Array} */ _add_nodes_recursive(parent_node, node_data) { - var all_nodes = []; + var created_nodes = []; if (!node_data.id || !node_data.topic) { logger.warn('invalid node data:', node_data); @@ -580,22 +559,16 @@ export default class jsMind { ); if (new_node) { - // Add the result only if node creation was successful - all_nodes.push(new_node); - - // Process children recursively only if parent node was created successfully - const children = - node_data && Array.isArray(node_data.children) ? node_data.children : null; - if (children && children.length > 0) { - // Use lambda to process children and flatten results - const child_nodes = children + created_nodes.push(new_node); + if (Array.isArray(node_data.children)) { + const sub_nodes = node_data.children .map(child => this._add_nodes_recursive(new_node, child)) .flat(); - all_nodes = all_nodes.concat(child_nodes); + created_nodes = created_nodes.concat(sub_nodes); } } - return all_nodes; + return created_nodes; } /**