Skip to content

Commit fe022cf

Browse files
committed
Back to native purge to build from first principles?!
1 parent 1195e31 commit fe022cf

File tree

1 file changed

+14
-57
lines changed
  • src/puter-js/src/modules/FileSystem

1 file changed

+14
-57
lines changed

src/puter-js/src/modules/FileSystem/index.js

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import { AdvancedBase } from '../../../../putility/index.js';
2525
import FSItem from '../FSItem.js';
2626
import deleteFSEntry from './operations/deleteFSEntry.js';
2727
import getReadURL from './operations/getReadUrl.js';
28-
import path from "../../lib/path.js";
2928

3029

3130
export class PuterJSFileSystemModule extends AdvancedBase {
@@ -124,43 +123,34 @@ export class PuterJSFileSystemModule extends AdvancedBase {
124123

125124
this.socket.on('item.renamed', (item) => {
126125
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
127-
if (item.original_client_socket_id !== this.socket.id) {
128-
this.invalidateCache({path: item.old_path, is_dir: item.is_dir ?? true});
129-
}
126+
puter._cache.flushall();
127+
console.log('Flushed cache for item.renamed');
130128
});
131129

132-
this.socket.on('item.deleted', (item) => {
130+
this.socket.on('item.removed', (item) => {
133131
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
134-
if (item.original_client_socket_id !== this.socket.id) {
135-
this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true});
136-
}
132+
puter._cache.flushall();
133+
console.log('Flushed cache for item.deleted');
137134
});
138135

139136
this.socket.on('item.added', (item) => {
140137
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
141-
if (item.original_client_socket_id !== this.socket.id) {
142-
this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true});
143-
}
138+
puter._cache.flushall();
139+
console.log('Flushed cache for item.added');
144140
});
145141

146142
this.socket.on('item.updated', (item) => {
147143
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
148-
if (item.original_client_socket_id !== this.socket.id) {
149-
this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true});
150-
}
144+
puter._cache.flushall();
145+
console.log('Flushed cache for item.updated');
151146
});
152147

153148
this.socket.on('item.moved', (item) => {
154149
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
155-
if (item.original_client_socket_id !== this.socket.id) {
156-
this.invalidateCache({path: item.old_path, is_dir: item.is_dir ?? true});
157-
// invalidate the destination dir
158-
this.invalidateCache({path: path.dirname(item.path), is_dir: true});
159-
}
150+
puter._cache.flushall();
151+
console.log('Flushed cache for item.moved');
160152
});
161153

162-
163-
164154
this.socket.on('connect', () => {
165155
if ( puter.debugMode )
166156
{
@@ -251,44 +241,11 @@ export class PuterJSFileSystemModule extends AdvancedBase {
251241
* @memberof PuterJSFileSystemModule
252242
* @returns {void}
253243
*/
254-
invalidateCache(options) {
255-
console.log('invalidating cache for:', options.path);
244+
invalidateCache() {
256245
// Action: Update last valid time
257246
// Set to 0, which means the cache is not up to date.
258247
localStorage.setItem(LAST_VALID_TS, '0');
259-
260-
// Action: Update cache for the item
261-
if(options?.path){
262-
// delete cache for the item
263-
puter._cache.del('item:' + options.path, options);
264-
265-
// if item is a folder
266-
if(options.is_dir){
267-
puter._cache.del('readdir:' + options.path);
268-
console.log('⮑ delete its readdir:', options.path);
269-
// delete all descendants readdirs
270-
const descendants_readdir = puter._cache.keys('readdir:' + options.path + '/*');
271-
for(const descendant of descendants_readdir){
272-
puter._cache.del(descendant);
273-
console.log('⮑ delete descendant readdir:', descendant);
274-
}
275-
276-
// delete all descendants items
277-
const descendants_item = puter._cache.keys('item:' + options.path + '/*');
278-
for(const descendant of descendants_item){
279-
puter._cache.del(descendant);
280-
console.log('⮑ delete descendant item:', descendant);
281-
}
282-
}
283-
// invalidate parent folder cache
284-
puter._cache.del('readdir:' + path.dirname(options.path));
285-
console.log('⮑ delete its parent readdir:', path.dirname(options.path));
286-
287-
288-
}else{
289-
puter._cache.flushall();
290-
console.log('invalidated cache for all items');
291-
}
248+
puter._cache.flushall();
292249
}
293250

294251
/**
@@ -328,7 +285,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
328285
const localValidTs = parseInt(localStorage.getItem(LAST_VALID_TS)) || 0;
329286

330287
if (serverTimestamp - localValidTs > 2000) {
331-
console.log('PURGING CACHE');
288+
console.log('Cache is not up to date, purging cache');
332289
// Server has newer data, purge local cache
333290
puter._cache.flushall();
334291
localStorage.setItem(LAST_VALID_TS, '0');

0 commit comments

Comments
 (0)