-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
7.0.0-alpha.5: graph navigation and events improvements
- Add go() method for graph navigation through contains - Improve value unpacking with proper type handling - Return unsubscribe function from on() - Fix file sync tests cleanup - Enhance event handling in syncJSONFile - Bump version to 7.0.0-alpha.5
- Loading branch information
1 parent
df8dde5
commit bc7b356
Showing
9 changed files
with
356 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Deep } from './deep.js'; | ||
import chokidar from 'chokidar'; | ||
import fs from 'fs/promises'; | ||
|
||
/** | ||
* Synchronizes a JSON file with a Deep selection. | ||
* When file changes, creates a new selection from the file content. | ||
* When input selection changes, saves it to the file. | ||
* | ||
* @param inputSelection - The Deep selection to sync with file | ||
* @param path - Path to the JSON file | ||
* @returns The selection created from file content | ||
*/ | ||
export async function syncJSONFile(inputSelection: Deep, path: string): Promise<Deep> { | ||
const deep = inputSelection.deep; | ||
let SyncJSONFile = deep.contains.SyncJSONFile; | ||
if (!SyncJSONFile) { | ||
SyncJSONFile = deep.contains.SyncJSONFile = deep.new(); | ||
const allJSONFiles = deep.select({ type: SyncJSONFile }); | ||
allJSONFiles.on((event) => { | ||
if (event.name === 'kill') { | ||
|
||
} | ||
}); | ||
} | ||
const syncJSONFile = SyncJSONFile.new(); | ||
|
||
// Create initial selection from file | ||
try { | ||
const fileContent = await fs.readFile(path, 'utf-8'); | ||
const data = JSON.parse(fileContent); | ||
const fileSelection = inputSelection.unpack(data); | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { // File doesn't exist | ||
// Save initial selection to file | ||
const data = await inputSelection.pack; | ||
await fs.writeFile(path, JSON.stringify(data, null, 2)); | ||
} else { | ||
console.error(`Error loading initial data from ${path}:`, error); | ||
throw error; | ||
} | ||
} | ||
|
||
// Watch for file changes | ||
const watcher = chokidar.watch(path, { | ||
persistent: true, | ||
ignoreInitial: true, | ||
}); | ||
syncJSONFile.from = deep.wrap(() => { | ||
console.log('closing watcher'); | ||
watcher.close(); | ||
}); | ||
|
||
let isUpdatingFile = false; // Flag to prevent recursive updates | ||
|
||
watcher.on('change', async () => { | ||
if (isUpdatingFile) return; // Skip if we're currently updating the file | ||
|
||
try { | ||
const fileContent = await fs.readFile(path, 'utf-8'); | ||
const data = JSON.parse(fileContent); | ||
const fileSelection = await inputSelection.unpack(data); | ||
} catch (error) { | ||
console.error(`Error processing file change for ${path}:`, error); | ||
} | ||
}); | ||
|
||
// Watch for input selection changes | ||
const unsubscribe = inputSelection.on(async (event) => { | ||
if (isUpdatingFile) return; // Skip if we're currently updating the file | ||
|
||
try { | ||
isUpdatingFile = true; | ||
const data = await inputSelection.pack; | ||
await fs.writeFile(path, JSON.stringify(data, null, 2)); | ||
} catch (error) { | ||
console.error(`Error saving changes to ${path}:`, error); | ||
} finally { | ||
isUpdatingFile = false; | ||
} | ||
}); | ||
syncJSONFile.to = deep.wrap(unsubscribe); | ||
|
||
// Add cleanup method to the returned selection | ||
syncJSONFile.kill = () => { | ||
syncJSONFile.from.call(); | ||
syncJSONFile.to.call(); | ||
}; | ||
|
||
return syncJSONFile; | ||
} | ||
|
||
export default syncJSONFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import './tests/core.js'; | ||
import './tests/sync.js'; | ||
import './tests/select.js'; | ||
// import './tests/langchain.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.