Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/services/core/data-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ export class DataCache<T> {
public items: Observable<Map<string, T>>;
public deleted: Observable<string>;

/**
* Notification when the cache is being cleared
*/
public cleared: Observable<void>;

public queryCache = new QueryCache();
public pollService = new PollService();

private _items = new BehaviorSubject<Map<string, T>>(Map<string, T>({}));
private _deleted = new Subject<string>();
private _cleared = new Subject<void>();

/**
* @param _uniqueField Each record should have a unqiue field. This is used to update the cache.
Expand All @@ -52,11 +58,13 @@ export class DataCache<T> {
this.id = SecureUtils.uuid();
this.items = this._items.asObservable();
this.deleted = this._deleted.asObservable();
this.cleared = this._cleared.asObservable();
DataCacheTracker.registerCache(this);
}

public clear() {
this.queryCache.clearCache();
this._cleared.next();
this._items.next(Map<string, T>({}));
}

Expand Down
1 change: 1 addition & 0 deletions app/services/core/rx-entity-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function getOnceProxy<TEntity>(getProxy: RxEntityProxy<any, TEntity>): Ob
sub.unsubscribe();
obs.next(item);
obs.complete();
getProxy.dispose();
}
},
error: errorCallback,
Expand Down
4 changes: 4 additions & 0 deletions app/services/core/rx-list-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export abstract class RxListProxy<TParams, TEntity> extends RxProxyBase<TParams,
this.deleted.subscribe((deletedKey) => {
this._itemKeys.next(OrderedSet<string>(this._itemKeys.value.filter((key) => key !== deletedKey)));
});

this._cacheCleared.subscribe((deletedKey) => {
this._itemKeys.next(OrderedSet<string>([]));
});
}

public updateParams(params: TParams) {
Expand Down
15 changes: 15 additions & 0 deletions app/services/core/rx-proxy-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ export abstract class RxProxyBase<TParams, TOptions extends ProxyOptions, TEntit
protected _params: TParams;
protected _cache: DataCache<TEntity>;
protected _options: TOptions;
protected _cacheCleared = new Subject<void>();

private _currentQuerySub: Subscription = null;
private _currentObservable: Observable<any>;
private _deletedSub: Subscription;
private _cacheClearedSub: Subscription;
private _deleted = new Subject<string>();
private _logIgnoreError: number[];
private _pollObservable: PollObservable;
Expand Down Expand Up @@ -145,12 +147,25 @@ export abstract class RxProxyBase<TParams, TOptions extends ProxyOptions, TEntit
return this._pollObservable;
}

/**
* This will release any reference used by the RxProxy.
* You NEED to call this in ngOnDestroy
* otherwise internal subscribe will never get cleared and the list porxy will not get GC
*/
public dispose() {
this._clearDeleteSub();
}

protected set cache(cache: DataCache<TEntity>) {
this._cache = cache;
this._clearDeleteSub();
this._deletedSub = cache.deleted.subscribe((x) => {
this._deleted.next(x);
});

this._cacheClearedSub = cache.cleared.subscribe((x) => {
this._cacheCleared.next(x);
});
}

protected get cache() {
Expand Down