Skip to content

Commit

Permalink
feat(recs): add util to clear cache of local cache component (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Oct 22, 2022
1 parent 8df93fb commit 30a5868
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/recs/src/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,21 @@ export function overridableComponent<S extends Schema, M extends Metadata, T = u
return overriddenComponent;
}

function getLocalCacheId(component: Component, uniqueWorldIdentifier?: string): string {
return `localcache-${uniqueWorldIdentifier}-${component.id}`;
}

export function clearLocalCache(component: Component, uniqueWorldIdentifier?: string): void {
localStorage.removeItem(getLocalCacheId(component, uniqueWorldIdentifier));
}

// Note: Only proof of concept for now - use this only for component that do not update frequently
export function createLocalCache<S extends Schema, M extends Metadata, T = undefined>(
component: Component<S, M, T>,
uniqueWorldIdentifier?: string
): Component<S, M, T> {
const { world, update$, values } = component;
const cacheId = `localcache-${uniqueWorldIdentifier}-${component.id}`;
const cacheId = getLocalCacheId(component as Component, uniqueWorldIdentifier);
let numUpdates = 0;
const creation = Date.now();

Expand Down

0 comments on commit 30a5868

Please sign in to comment.