-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3067bc9
commit 54906a5
Showing
22 changed files
with
1,155 additions
and
270 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,92 @@ | ||
import fs from 'fs'; | ||
import { SmartCollectionItemDataAdapter } from './_adapter.js'; | ||
export class TestSmartCollectionDataAdapter extends SmartCollectionItemDataAdapter{ | ||
import { SmartCollectionDataAdapter } from './_adapter.js'; | ||
import { ajson_merge } from '../utils/ajson_merge.js'; | ||
|
||
const class_to_collection_key = { | ||
'SmartSource': 'smart_sources', | ||
'SmartNote': 'smart_sources', // DEPRECATED: added for backward compatibility | ||
'SmartBlock': 'smart_blocks', | ||
'SmartDirectory': 'smart_directories', | ||
}; | ||
|
||
export class SmartCollectionTestDataAdapter extends SmartCollectionDataAdapter { | ||
constructor(collection) { | ||
super(collection); | ||
this.test_data = JSON.parse(fs.readFileSync(this.data_path, 'utf8')); | ||
} | ||
|
||
get data_path() { return "./test/_data.json"; } | ||
async load() { | ||
// console.log("Loading test collection..."); | ||
Object.entries(this.test_data).forEach(([ajson_key, value]) => { | ||
const [class_name, key] = ajson_key.split(":"); | ||
const entity = new (this.env.item_types[class_name])(this.env, value); | ||
// if value has content, this.collection.fs.write() | ||
if(value.content) this.collection.fs.write(key, value.content); | ||
this.add_to_collection(entity); | ||
}); | ||
// console.log("Loaded test collection"); | ||
|
||
async load(item) { | ||
try { | ||
const ajson_key = `${item.constructor.name}:${item.key}`; | ||
const data = this.test_data[ajson_key]; | ||
|
||
if (!data) { | ||
console.log(`Data not found for: ${ajson_key}`); | ||
return item.queue_import(); | ||
} | ||
|
||
const parsed_data = {}; | ||
data.split('\n').forEach(line => { | ||
try { | ||
const parsed = JSON.parse(`{${line}}`); | ||
if (Object.values(parsed)[0] === null) { | ||
if (parsed_data[Object.keys(parsed)[0]]) delete parsed_data[Object.keys(parsed)[0]]; | ||
} else { | ||
Object.assign(parsed_data, parsed); | ||
} | ||
} catch (err) { | ||
console.warn("Error parsing line: ", line); | ||
console.warn(err.stack); | ||
} | ||
}); | ||
|
||
Object.entries(parsed_data).forEach(([parsed_ajson_key, value]) => { | ||
if (!value) return; // handle null values (deleted) | ||
const [class_name, ...key_parts] = parsed_ajson_key.split(":"); | ||
const entity_key = key_parts.join(":"); | ||
if (entity_key === item.key) { | ||
item.data = value; | ||
} else { | ||
if (!this.env[class_to_collection_key[class_name]]) { | ||
return console.warn(`Collection class not found: ${class_name}`); | ||
} | ||
this.env[class_to_collection_key[class_name]].items[entity_key] = new this.env.item_types[class_name](this.env, value); | ||
} | ||
}); | ||
|
||
item._queue_load = false; | ||
item.loaded_at = Date.now(); | ||
} catch (err) { | ||
console.log(`Error loading collection item: ${item.key}`); | ||
console.warn(err.stack); | ||
item.queue_load(); | ||
} | ||
} | ||
async save_item(key) { | ||
delete this._save_queue[key]; | ||
const item = this.collection.get(key); | ||
if(!item) return console.warn("Item not found: " + key); | ||
if(!item.deleted) this.test_data[key] = item.ajson; | ||
else { | ||
delete this.test_data[key]; | ||
this.collection.delete_item(key); | ||
|
||
async save(item, ajson = null) { | ||
if (!ajson) ajson = item.ajson; | ||
const ajson_key = `${item.constructor.name}:${item.key}`; | ||
|
||
try { | ||
if (item.deleted) { | ||
this.collection.delete_item(item.key); | ||
delete this.test_data[ajson_key]; | ||
} else { | ||
this.test_data[ajson_key] += '\n' + ajson; | ||
} | ||
|
||
// Simulate writing to file | ||
fs.writeFileSync(this.data_path, JSON.stringify(this.test_data, null, 2)); | ||
|
||
item._queue_save = false; | ||
return true; | ||
} catch (err) { | ||
console.warn(`Error saving collection item: ${item.key}`); | ||
console.warn(err.stack); | ||
item.queue_save(); | ||
return false; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.