diff --git a/package-lock.json b/package-lock.json index 84067f41e..f5d443347 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20086,13 +20086,10 @@ } }, "src/puter-js/node_modules/@heyputer/kv.js": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@heyputer/kv.js/-/kv.js-0.2.0.tgz", - "integrity": "sha512-qTKOb3DA4jEd9c5NOuWXfBJD/cLQxvqiCSZ85X/nMqkwx0hx/3dkXp1vdNUVexWywte7PrkWCPyPxQVa6RPzyw==", - "license": "MIT", - "dependencies": { - "minimatch": "^9.0.0" - } + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@heyputer/kv.js/-/kv.js-0.2.1.tgz", + "integrity": "sha512-YhVtzz7ZA/HmuaDvzZZhhUyQWBvp3/TXeY4jULssTdLJwT+tEM4BTYHXttORX+V5auvrYinjj8dNFQnby5T82w==", + "license": "MIT" }, "src/puter-wisp": { "name": "@heyputer/puter-wisp", diff --git a/src/puter-js/src/modules/FileSystem/index.js b/src/puter-js/src/modules/FileSystem/index.js index f525b631c..066bc6821 100644 --- a/src/puter-js/src/modules/FileSystem/index.js +++ b/src/puter-js/src/modules/FileSystem/index.js @@ -125,35 +125,37 @@ export class PuterJSFileSystemModule extends AdvancedBase { this.socket.on('item.renamed', (item) => { // check original_client_socket_id and if it matches this.socket.id, don't invalidate cache if (item.original_client_socket_id !== this.socket.id) { - this.invalidateCache(item); + this.invalidateCache({path: item.old_path, is_dir: item.is_dir ?? true}); } }); this.socket.on('item.deleted', (item) => { // check original_client_socket_id and if it matches this.socket.id, don't invalidate cache if (item.original_client_socket_id !== this.socket.id) { - this.invalidateCache(item); + this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true}); } }); this.socket.on('item.added', (item) => { // check original_client_socket_id and if it matches this.socket.id, don't invalidate cache if (item.original_client_socket_id !== this.socket.id) { - this.invalidateCache(item); + this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true}); } }); this.socket.on('item.updated', (item) => { // check original_client_socket_id and if it matches this.socket.id, don't invalidate cache if (item.original_client_socket_id !== this.socket.id) { - this.invalidateCache(item); + this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true}); } }); this.socket.on('item.moved', (item) => { // check original_client_socket_id and if it matches this.socket.id, don't invalidate cache if (item.original_client_socket_id !== this.socket.id) { - this.invalidateCache(item); + this.invalidateCache({path: item.old_path, is_dir: item.is_dir ?? true}); + // invalidate the destination dir + this.invalidateCache({path: path.dirname(item.path), is_dir: true}); } }); @@ -249,25 +251,39 @@ export class PuterJSFileSystemModule extends AdvancedBase { * @memberof PuterJSFileSystemModule * @returns {void} */ - invalidateCache(item) { + invalidateCache(options) { + console.log('invalidating cache for:', options.path); // Action: Update last valid time // Set to 0, which means the cache is not up to date. localStorage.setItem(LAST_VALID_TS, '0'); // Action: Update cache for the item - if(item?.path){ - console.log('invalidated cache for item:', item.path); - // update cache for the item - puter._cache.set('item:' + item.path, item); + if(options?.path){ + // delete cache for the item + puter._cache.del('item:' + options.path, options); - // if item is a folder, invalidate the readdir cache for the folder - if(item.is_dir){ - puter._cache.del('readdir:' + item.path); - console.log('⮑ invalidated its readdir:', item.path); + // if item is a folder, invalidate the readdir cache for the folder and all its descendants + if(options.is_dir){ + puter._cache.del('readdir:' + options.path); + console.log('⮑ invalidated its readdir:', options.path); + // invalidate all descendants readdir and item caches + const descendants_readdir = puter._cache.keys('readdir:' + options.path + '/*'); + console.log(descendants_readdir); + for(const descendant of descendants_readdir){ + puter._cache.del(descendant); + console.log('⮑ invalidated descendant readdir:', descendant); + } + + // invalidate all descendants item caches + const descendants_item = puter._cache.keys('item:' + options.path + '/*'); + for(const descendant of descendants_item){ + puter._cache.del(descendant); + console.log('⮑ invalidated descendant item:', descendant); + } } // invalidate parent folder cache - puter._cache.del('readdir:' + path.dirname(item.path)); - console.log('⮑ invalidated its parent readdir:', path.dirname(item.path)); + puter._cache.del('readdir:' + path.dirname(options.path)); + console.log('⮑ invalidated its parent readdir:', path.dirname(options.path)); }else{ diff --git a/src/puter-js/src/modules/FileSystem/operations/copy.js b/src/puter-js/src/modules/FileSystem/operations/copy.js index 0ba06c2fd..4e33162a0 100644 --- a/src/puter-js/src/modules/FileSystem/operations/copy.js +++ b/src/puter-js/src/modules/FileSystem/operations/copy.js @@ -55,8 +55,6 @@ const copy = function (...args) { // if user is copying an item to where its source is, change the name so there is no conflict dedupe_name: (options.dedupe_name || options.dedupeName), })); - - this.invalidateCache(); }) } diff --git a/src/puter-js/src/modules/FileSystem/operations/mkdir.js b/src/puter-js/src/modules/FileSystem/operations/mkdir.js index 2fabce36f..d5245f049 100644 --- a/src/puter-js/src/modules/FileSystem/operations/mkdir.js +++ b/src/puter-js/src/modules/FileSystem/operations/mkdir.js @@ -53,8 +53,6 @@ const mkdir = function (...args) { original_client_socket_id: this.socket.id, create_missing_parents: (options.recursive || options.createMissingParents) ?? false, })); - - this.invalidateCache(options.path); }) } diff --git a/src/puter-js/src/modules/FileSystem/operations/move.js b/src/puter-js/src/modules/FileSystem/operations/move.js index a691febf2..f4ddb976d 100644 --- a/src/puter-js/src/modules/FileSystem/operations/move.js +++ b/src/puter-js/src/modules/FileSystem/operations/move.js @@ -65,8 +65,6 @@ const move = function (...args) { new_metadata: (options.new_metadata || options.newMetadata), original_client_socket_id: options.excludeSocketID, })); - - this.invalidateCache(); }) } diff --git a/src/puter-js/src/modules/FileSystem/operations/rename.js b/src/puter-js/src/modules/FileSystem/operations/rename.js index adefd2aa3..f43ca4ac0 100644 --- a/src/puter-js/src/modules/FileSystem/operations/rename.js +++ b/src/puter-js/src/modules/FileSystem/operations/rename.js @@ -47,8 +47,6 @@ const rename = function (...args) { // If dirPath is not provided or it's not starting with a slash, it means it's a relative path // in that case, we need to prepend the app's root directory to it dataToSend.path = getAbsolutePathForApp(options.path); - - this.invalidateCache(dataToSend.path); } xhr.send(JSON.stringify(dataToSend)); diff --git a/src/puter-js/src/modules/FileSystem/operations/upload.js b/src/puter-js/src/modules/FileSystem/operations/upload.js index a8979676e..af7cbfddc 100644 --- a/src/puter-js/src/modules/FileSystem/operations/upload.js +++ b/src/puter-js/src/modules/FileSystem/operations/upload.js @@ -431,8 +431,6 @@ const upload = async function(items, dirPath, options = {}){ // send request xhr.send(fd); - - this.invalidateCache(); }) } diff --git a/src/puter-js/src/modules/FileSystem/operations/write.js b/src/puter-js/src/modules/FileSystem/operations/write.js index 74f81e2f9..d152b1329 100644 --- a/src/puter-js/src/modules/FileSystem/operations/write.js +++ b/src/puter-js/src/modules/FileSystem/operations/write.js @@ -55,8 +55,6 @@ const write = async function (targetPath, data, options = {}) { throw new Error({ code: 'field_invalid', message: 'write() data parameter is an invalid type' }); } - this.invalidateCache(); - // perform upload return this.upload(data, parent, options); }