Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rebrand RecordData types to Cache #8359

Merged
merged 1 commit into from
Dec 9, 2022
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ module.exports = {
'packages/graph/src/-private/graph/-operations.ts',
'packages/graph/src/-private/graph/-edge-definition.ts',
'packages/graph/src/-private/coerce-id.ts',
'packages/json-api/src/-private/record-data.ts',
'packages/json-api/src/-private/cache.ts',
'packages/private-build-infra/addon/index.ts',
'packages/private-build-infra/addon/deprecations.ts',
'packages/private-build-infra/addon/current-deprecations.ts',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IdentifierCache } from '@ember-data/store/-private/caches/identifier-cache';
import { NotificationType } from '@ember-data/store/-private/managers/record-notification-manager';

import type { Cache } from './cache';
import { StableRecordIdentifier } from './identifier';
import type { RecordData } from './record-data';
import type { AttributesSchema, RelationshipsSchema } from './record-data-schemas';
import { SchemaDefinitionService } from './schema-definition-service';

Expand All @@ -11,19 +11,19 @@ import { SchemaDefinitionService } from './schema-definition-service';
*/

/**
* RecordDataStoreWrapper provides encapsulated API access to the minimal
* subset of the Store's functionality that cache (RecordData) implementations
* should interact with. It is provided to the Store's `createRecordDataFor`
* CacheStoreWrapper provides encapsulated API access to the minimal
* subset of the Store's functionality that Cache implementations
* should interact with. It is provided to the Store's `createCache`
* hook.
*
* Cache implementations should not need more than this API provides.
*
* This class cannot be directly instantiated.
*
* @class RecordDataStoreWrapper
* @class CacheStoreWrapper
* @public
*/
export interface LegacyRecordDataStoreWrapper {
export interface LegacyCacheStoreWrapper {
/**
* Provides access to the IdentifierCache instance
* for this Store instance.
Expand Down Expand Up @@ -171,14 +171,14 @@ export interface LegacyRecordDataStoreWrapper {
*
* @method recordDataFor
* @param {StableRecordIdentifier} identifier
* @return {RecordData} the RecordData cache instance associated with the identifier
* @return {Cache} the RecordData cache instance associated with the identifier
* @public
*/
recordDataFor(type: string, id: string, lid?: string | null): RecordData;
recordDataFor(type: string, id: string | null, lid: string): RecordData;
recordDataFor(type: string): RecordData;
recordDataFor(type: string, id?: string | null, lid?: string | null): RecordData;
recordDataFor(identifier: StableRecordIdentifier): RecordData;
recordDataFor(type: string, id: string, lid?: string | null): Cache;
recordDataFor(type: string, id: string | null, lid: string): Cache;
recordDataFor(type: string): Cache;
recordDataFor(type: string, id?: string | null, lid?: string | null): Cache;
recordDataFor(identifier: StableRecordIdentifier): Cache;

/**
* Use notifyChange
Expand Down Expand Up @@ -237,7 +237,7 @@ export interface LegacyRecordDataStoreWrapper {
notifyStateChange(modelName: string, id: string | null, clientId: string | null, key?: string): void;
}

export interface V2RecordDataStoreWrapper {
export interface V2CacheStoreWrapper {
identifierCache: IdentifierCache;
getSchemaDefinitionService(): SchemaDefinitionService;

Expand All @@ -247,9 +247,9 @@ export interface V2RecordDataStoreWrapper {

hasRecord(identifier: StableRecordIdentifier): boolean;

recordDataFor(identifier: StableRecordIdentifier): RecordData;
recordDataFor(identifier: StableRecordIdentifier): Cache;

notifyChange(identifier: StableRecordIdentifier, namespace: NotificationType, key?: string): void;
}

export type RecordDataStoreWrapper = LegacyRecordDataStoreWrapper | V2RecordDataStoreWrapper;
export type CacheStoreWrapper = LegacyCacheStoreWrapper | V2CacheStoreWrapper;
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface MergeOperation {
value: StableRecordIdentifier; // new
}

export interface RecordDataV1 {
export interface CacheV1 {
version?: '1';

// Cache
Expand Down Expand Up @@ -51,10 +51,10 @@ export interface RecordDataV1 {
getBelongsTo(key: string): SingleResourceRelationship;
getHasMany(key: string): CollectionResourceRelationship;

setDirtyBelongsTo(name: string, recordData: RecordData | null): void;
setDirtyHasMany(key: string, recordDatas: RecordData[]): void;
addToHasMany(key: string, recordDatas: RecordData[], idx?: number): void;
removeFromHasMany(key: string, recordDatas: RecordData[]): void;
setDirtyBelongsTo(name: string, recordData: Cache | null): void;
setDirtyHasMany(key: string, recordDatas: Cache[]): void;
addToHasMany(key: string, recordDatas: Cache[], idx?: number): void;
removeFromHasMany(key: string, recordDatas: Cache[]): void;

// State
// =============
Expand All @@ -66,7 +66,7 @@ export interface RecordDataV1 {
isDeletionCommitted(identifier: StableRecordIdentifier): boolean;
}

export interface RecordData {
export interface Cache {
version: '2';

// Cache
Expand Down
1 change: 0 additions & 1 deletion packages/-ember-data/addon/-private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export { Snapshot } from '@ember-data/store/-private';
// `ember-data-model-fragments' and `ember-data-change-tracker` rely on `normalizeModelName`
export { RecordArrayManager, SnapshotRecordArray, normalizeModelName, coerceId } from '@ember-data/store/-private';
export { ManyArray, PromiseManyArray } from '@ember-data/model/-private';
export { RecordData } from '@ember-data/json-api/-private';

export const PromiseArray = ArrayProxy.extend(PromiseProxyMixin);
export const PromiseObject = ObjectProxy.extend(PromiseProxyMixin);
2 changes: 1 addition & 1 deletion packages/graph/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ember-data/graph",
"version": "4.10.0-alpha.4",
"description": "Provides the default resource cache (RecordData) implementation for ember-data",
"description": "Provides a normalized graph for managing relationships between resources",
"keywords": [
"ember-addon"
],
Expand Down
4 changes: 2 additions & 2 deletions packages/graph/src/-private/graph/-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { assert, inspect, warn } from '@ember/debug';
import { LOG_GRAPH } from '@ember-data/private-build-infra/debugging';
import type { Store } from '@ember-data/store/-private';
import { recordDataFor as peekRecordData } from '@ember-data/store/-private';
import type { CacheStoreWrapper } from '@ember-data/types/q/cache-store-wrapper';
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
import type { RecordDataStoreWrapper } from '@ember-data/types/q/record-data-store-wrapper';
import type { Dict } from '@ember-data/types/q/utils';

import { coerceId } from '../coerce-id';
Expand All @@ -13,7 +13,7 @@ import type ManyRelationship from '../relationships/state/has-many';
import type { UpdateRelationshipOperation } from './-operations';
import type { Graph, ImplicitRelationship } from './graph';

export function getStore(wrapper: RecordDataStoreWrapper | { _store: Store }): Store {
export function getStore(wrapper: CacheStoreWrapper | { _store: Store }): Store {
assert(`expected a private _store property`, '_store' in wrapper);
return wrapper._store;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/graph/src/-private/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

import { LOG_GRAPH } from '@ember-data/private-build-infra/debugging';
import { MergeOperation } from '@ember-data/types/q/cache';
import type { CacheStoreWrapper } from '@ember-data/types/q/cache-store-wrapper';
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
import { MergeOperation } from '@ember-data/types/q/record-data';
import type { RecordDataStoreWrapper } from '@ember-data/types/q/record-data-store-wrapper';
import type { Dict } from '@ember-data/types/q/utils';

import BelongsToRelationship from '../relationships/state/belongs-to';
Expand Down Expand Up @@ -44,7 +44,7 @@ export interface ImplicitRelationship {

export type RelationshipEdge = ImplicitRelationship | ManyRelationship | BelongsToRelationship;

export const Graphs = new Map<RecordDataStoreWrapper, Graph>();
export const Graphs = new Map<CacheStoreWrapper, Graph>();

/*
* Graph acts as the cache for relationship data. It allows for
Expand All @@ -69,7 +69,7 @@ export class Graph {
declare _definitionCache: EdgeCache;
declare _potentialPolymorphicTypes: Dict<Dict<boolean>>;
declare identifiers: Map<StableRecordIdentifier, Dict<RelationshipEdge>>;
declare store: RecordDataStoreWrapper;
declare store: CacheStoreWrapper;
declare isDestroyed: boolean;
declare _willSyncRemote: boolean;
declare _willSyncLocal: boolean;
Expand All @@ -82,7 +82,7 @@ export class Graph {
declare _transaction: Set<ManyRelationship | BelongsToRelationship> | null;
declare _removing: StableRecordIdentifier | null;

constructor(store: RecordDataStoreWrapper) {
constructor(store: CacheStoreWrapper) {
this._definitionCache = Object.create(null) as EdgeCache;
this._potentialPolymorphicTypes = Object.create(null) as Dict<Dict<boolean>>;
this.identifiers = new Map();
Expand Down Expand Up @@ -409,11 +409,11 @@ export class Graph {
Graphs.delete(this.store);

if (DEBUG) {
Graphs.delete(getStore(this.store) as unknown as RecordDataStoreWrapper);
Graphs.delete(getStore(this.store) as unknown as CacheStoreWrapper);
}

this.identifiers.clear();
this.store = null as unknown as RecordDataStoreWrapper;
this.store = null as unknown as CacheStoreWrapper;
this.isDestroyed = true;
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/graph/src/-private/graph/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DEBUG } from '@glimmer/env';

import type Store from '@ember-data/store';
import type { CacheStoreWrapper } from '@ember-data/types/q/cache-store-wrapper';
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
import type { RecordDataStoreWrapper } from '@ember-data/types/q/record-data-store-wrapper';

import type { UpgradedMeta } from './-edge-definition';
import { getStore } from './-utils';
Expand All @@ -19,16 +19,16 @@ function isStore(maybeStore: unknown): maybeStore is Store {
return (maybeStore as Store)._instanceCache !== undefined;
}

function getWrapper(store: RecordDataStoreWrapper | Store): RecordDataStoreWrapper {
function getWrapper(store: CacheStoreWrapper | Store): CacheStoreWrapper {
return isStore(store) ? store._instanceCache._storeWrapper : store;
}

export function peekGraph(store: RecordDataStoreWrapper | Store): Graph | undefined {
export function peekGraph(store: CacheStoreWrapper | Store): Graph | undefined {
return Graphs.get(getWrapper(store));
}
export type peekGraph = typeof peekGraph;

export function graphFor(store: RecordDataStoreWrapper | Store): Graph {
export function graphFor(store: CacheStoreWrapper | Store): Graph {
const wrapper = getWrapper(store);
let graph = Graphs.get(wrapper);

Expand All @@ -38,7 +38,7 @@ export function graphFor(store: RecordDataStoreWrapper | Store): Graph {

// in DEBUG we attach the graph to the main store for improved debuggability
if (DEBUG) {
Graphs.set(getStore(wrapper) as unknown as RecordDataStoreWrapper, graph);
Graphs.set(getStore(wrapper) as unknown as CacheStoreWrapper, graph);
}
}
return graph;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MergeOperation } from '@ember-data/types/q/record-data';
import type { MergeOperation } from '@ember-data/types/q/cache';
import type { Dict } from '@ember-data/types/q/utils';

import type BelongsToRelationship from '../../relationships/state/belongs-to';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,14 @@ export function removeFromInverse(

export function syncRemoteToLocal(graph: Graph, rel: ManyRelationship) {
let toSet = rel.remoteState;
let newRecordDatas = rel.localState.filter((recordData) => isNew(recordData) && toSet.indexOf(recordData) === -1);
let newIdentifiers = rel.localState.filter((recordData) => isNew(recordData) && toSet.indexOf(recordData) === -1);
let existingState = rel.localState;
rel.localState = toSet.concat(newRecordDatas);
rel.localState = toSet.concat(newIdentifiers);

let localMembers = (rel.localMembers = new Set<StableRecordIdentifier>());
rel.remoteMembers.forEach((v) => localMembers.add(v));
for (let i = 0; i < newRecordDatas.length; i++) {
localMembers.add(newRecordDatas[i]);
for (let i = 0; i < newIdentifiers.length; i++) {
localMembers.add(newIdentifiers[i]);
}

// TODO always notifying fails only one test and we should probably do away with it
Expand Down
2 changes: 1 addition & 1 deletion packages/json-api/src/-private.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as RecordData } from './-private/record-data';
export { default as Cache } from './-private/cache';

/**
This package provides the default cache implementation used
Expand Down
Loading