Skip to content

Commit

Permalink
eliminate hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Dec 3, 2019
1 parent fa052c5 commit 9c10069
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/record-data/addon/-private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as Relationship } from './relationships/state/relationship';
export { default as BelongsToRelationship } from './relationships/state/belongs-to';
export { default as ManyRelationship } from './relationships/state/has-many';
export { relationshipStateFor, implicitRelationshipsFor, relationshipsFor } from './record-data-for';
export { peekGraph } from './relationships/state/graph';
12 changes: 6 additions & 6 deletions packages/record-data/addon/-private/relationships/state/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import { JsonApiRelationship } from '@ember-data/store/-private/ts-interfaces/re

const Graphs = new WeakMap<RecordDataStoreWrapper, Graph>();

export function peekGraph(store: RecordDataStoreWrapper): Graph | undefined {
return Graphs.get(store);
}

export function graphFor(store: RecordDataStoreWrapper): Graph {
let graph = Graphs.get(store);
if (graph === undefined) {
graph = new Graph(store);
const originalDestroy = store._store.destroy;
store._store.destroy = function() {
graph!.destroy();
Graphs.delete(store!);
return originalDestroy.call(this, ...arguments);
};
Graphs.set(store, graph);
}
return graph;
Expand Down Expand Up @@ -99,6 +97,8 @@ export class Graph {
destroy() {
this.identifiers.clear();
this.implicitMap.clear();
Graphs.delete(this.storeWrapper);
this.storeWrapper = (null as unknown) as RecordDataStoreWrapper;
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3465,6 +3465,7 @@ abstract class CoreStore extends Service {
identifierCacheFor(this).destroy();

this.unloadAll();
this._storeWrapper.destroy();

if (DEBUG) {
unregisterWaiter(this.__asyncWaiter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import { identifierCacheFor, IdentifierCache } from '../../identifiers/cache';
import CoreStore from '../core-store';
import constructResource from '../../utils/construct-resource';
import { StableRecordIdentifier } from '../../ts-interfaces/identifier';
import { HAS_RECORD_DATA_PACKAGE } from '@ember-data/private-build-infra';
import require from 'require';

let peekGraph;
if (HAS_RECORD_DATA_PACKAGE) {
let _peekGraph;
peekGraph = wrapper => {
_peekGraph = _peekGraph || require('@ember-data/record-data/-private').peekGraph;
return _peekGraph(wrapper);
};
}

/**
@module @ember-data/store
Expand Down Expand Up @@ -221,9 +232,24 @@ export default class RecordDataStoreWrapper implements IRecordDataStoreWrapper {
disconnectRecord(type: string, id: string | null, lid?: string | null): void {
const resource = constructResource(type, id, lid);
const identifier = identifierCacheFor(this._store).getOrCreateRecordIdentifier(resource);
if (HAS_RECORD_DATA_PACKAGE) {
let graph = peekGraph(this);
if (graph) {
graph.unload(identifier);
}
}
let internalModel = internalModelFactoryFor(this._store).peek(identifier);
if (internalModel) {
internalModel.destroyFromRecordData();
}
}

destroy() {
if (HAS_RECORD_DATA_PACKAGE) {
let graph = peekGraph(this);
if (graph) {
graph.destroy();
}
}
}
}

0 comments on commit 9c10069

Please sign in to comment.