Skip to content

Commit

Permalink
Refactor SmartBlock and Collection classes to remove deprecated data …
Browse files Browse the repository at this point in the history
…path methods and move relevant logic to adapters

- Removed deprecated `data_path` and `data_file` getters from SmartBlock and CollectionItem classes to streamline the codebase.
- Updated the Collection class to handle invalid items without logging warnings, enhancing code clarity.
- Introduced new methods in data adapters for loading item data if updated externally, improving data integrity and responsiveness.
- Adjusted SmartSource to utilize the new loading mechanism, ensuring it checks for updates before importing data.
- Enhanced the FileItemDataAdapter to implement the loading logic based on file updates, improving data synchronization.
  • Loading branch information
Brian Joseph Petro committed Dec 19, 2024
1 parent b03d200 commit 447ca1c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 37 deletions.
2 changes: 0 additions & 2 deletions smart-blocks/smart_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,6 @@ export class SmartBlock extends SmartEntity {
}

// source dependent
get data_path() { return this.source.data_path; }
get data_file() { return this.source.data_file; }
get excluded_lines() { return this.source.excluded_lines; }
get file() { return this.source.file; }
get is_canvas() { return this.source.is_canvas; }
Expand Down
18 changes: 18 additions & 0 deletions smart-collections/adapters/_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ export class CollectionDataAdapter {
* @returns {Promise<void>}
*/
async process_save_queue() { throw new Error('Not implemented'); }

/**
* Load the item's data from storage if it has been updated externally.
* @async
* @param {string} key - The key of the item to load.
* @returns {Promise<void>} Resolves when the item is loaded.
*/
async load_item_if_updated(item) {
const adapter = this.create_item_adapter(item);
await adapter.load_if_updated();
}
}


Expand Down Expand Up @@ -151,6 +162,13 @@ export class ItemDataAdapter {
get collection_adapter() {
return this.item.collection.data_adapter;
}

/**
* Load the item's data from storage if it has been updated externally.
* @async
* @returns {Promise<void>} Resolves when the item is loaded.
*/
async load_if_updated() { throw new Error('Not implemented'); }
}

export default {
Expand Down
13 changes: 13 additions & 0 deletions smart-collections/adapters/_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ export class FileItemDataAdapter extends ItemDataAdapter {
get fs() {
return this.item.collection.data_fs || this.item.collection.env.data_fs;
}
get_data_path() { throw new Error('Not implemented'); }

async load_if_updated() {
const data_path = this.get_data_path();
if(await this.fs.exists(data_path)) {
const loaded_at = this.item.loaded_at || 0;
const data_file_stat = await this.fs.stat(data_path);
if(data_file_stat.mtime > (loaded_at + 1 * 60 * 1000)) {
console.log(`Smart Collections: Re-loading item ${this.item.key} because it has been updated on disk`);
await this.load();
}
}
}
}

export default {
Expand Down
5 changes: 5 additions & 0 deletions smart-collections/adapters/ajson_multi_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ export class AjsonMultiFileCollectionDataAdapter extends FileCollectionDataAdapt
console.log(`Saved ${this.collection.collection_key} in ${Date.now() - time_start}ms`);
this.collection.notices?.remove('saving');
}

get_item_data_path(item) {
const item_adapter = this.create_item_adapter(item);
return item_adapter.get_data_path();
}

}

Expand Down
8 changes: 1 addition & 7 deletions smart-collections/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class Collection {

// If this is a new item, validate it before adding to the collection
if (!existing_item && !item.validate_save()) {
console.warn("Invalid item, skipping adding to collection:", item);
// console.warn("Invalid item, skipping adding to collection:", item);
return item;
}

Expand Down Expand Up @@ -328,12 +328,6 @@ export class Collection {
*/
get adapter() { return this.data_adapter; }

/**
* @deprecated Use env.env_data_dir
* @returns {string}
*/
get data_path() { return this.env.data_path; }

/**
* Saves the current state of the collection.
* @returns {Promise<void>}
Expand Down
8 changes: 0 additions & 8 deletions smart-collections/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,6 @@ export class CollectionItem {
return this.collection.data_fs;
}

/**
* The path at which this item's data is stored.
* @returns {string}
*/
get data_path() {
return this.collection.data_dir + (this.data_fs?.sep || "/") + this.multi_ajson_file_name + '.ajson';
}

/**
* Access to collection-level settings.
* @returns {Object}
Expand Down
20 changes: 1 addition & 19 deletions smart-sources/smart_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class SmartSource extends SmartEntity {
async import(){
this._queue_import = false;
try{
await this.check_if_data_updated_on_disk();
await this.data_adapter.load_if_updated(this);
await this.source_adapter.import();
}catch(err){
if(err.code === "ENOENT"){
Expand All @@ -65,17 +65,6 @@ export class SmartSource extends SmartEntity {
}
}

async check_if_data_updated_on_disk() {
// Must check exists using async because not always reflects by file.stat (e.g., Obsidian)
if ((await this.data_fs.exists(this.data_path))) {
// Check if file has been updated on disk
if (this.loaded_at && (this.env.fs.files[this.data_path] && this.env.fs.files[this.data_path].mtime > (this.loaded_at + 1 * 60 * 1000))) {
console.log(`Smart Connections: Re-loading data source for ${this.path} because it has been updated on disk`);
await this.load();
}
}
}

/**
* Finds connections relevant to this SmartSource based on provided parameters.
* @async
Expand Down Expand Up @@ -407,13 +396,6 @@ export class SmartSource extends SmartEntity {
*/
get data_path() { return this.collection.data_dir + "/" + this.multi_ajson_file_name + '.ajson'; }

/**
* Retrieves the data file associated with the SmartSource.
* @readonly
* @returns {Object} The data file object.
*/
get data_file() { return this.data_fs.files[this.data_path]; }

/**
* Retrieves the embed input, either from cache or by generating it.
* @readonly
Expand Down
3 changes: 2 additions & 1 deletion smart-sources/smart_sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export class SmartSources extends SmartEntities {
// Remove identified sources
for(let i = 0; i < remove_sources.length; i++){
const source = remove_sources[i];
await this.data_fs.remove(source.data_path);
const source_data_path = this.data_adapter.get_item_data_path(source);
await this.data_fs.remove(source_data_path);
source.delete();
}

Expand Down

0 comments on commit 447ca1c

Please sign in to comment.