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
7 changes: 5 additions & 2 deletions addon-test-support/index.js → addon-test-support/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { getContext } from '@ember/test-helpers';
import { Source } from '@orbit/data';

export async function waitForSource(sourceOrSourceName) {
export async function waitForSource(
sourceOrSourceName: Source | string
): Promise<void> {
let source;
if (typeof sourceOrSourceName === 'string') {
let { owner } = getContext();
let { owner } = getContext() as any;
source = owner.lookup(`data-source:${sourceOrSourceName}`);
if (!source) {
throw new Error(
Expand Down
14 changes: 13 additions & 1 deletion addon/-private/model-factory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Orbit from '@orbit/core';
import { getOwner } from '@ember/application';
import { Dict } from '@orbit/utils';
import { RecordIdentity, cloneRecordIdentity } from '@orbit/records';
import Store from './store';
import Model, { ModelSettings } from './model';

const { assert } = Orbit;

interface Factory {
create(settings: ModelSettings): Model;
}
Expand Down Expand Up @@ -34,7 +37,16 @@ export default class ModelFactory {
if (!modelFactory) {
let owner = getOwner(this.#store);
let orbitConfig = owner.lookup('ember-orbit:config');
modelFactory = owner.factoryFor(`${orbitConfig.types.model}:${type}`);

modelFactory = owner.factoryFor(
`${orbitConfig.types.model}:${type}`
) as Factory;

assert(
`An ember-orbit model for type ${type} has not been registered.`,
modelFactory !== undefined
);

this.#modelFactoryMap[type] = modelFactory;
}

Expand Down
2 changes: 1 addition & 1 deletion addon/-private/system/modules-of-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function (prefix: string, type: string): string[] {
const matches = regex.exec(moduleName);
if (matches && matches.length === 1) {
const matchedName = moduleName.match(/[^\/]+\/?$/);
if (matchedName) {
if (matchedName?.[0]) {
found.push(matchedName[0]);
}
}
Expand Down
60 changes: 34 additions & 26 deletions addon/-private/utils/normalize-record-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
RecordRelationship,
Record,
ModelDefinition,
RecordIdentity
RecordIdentity,
RelationshipDefinition
} from '@orbit/records';
import { deepSet, Dict } from '@orbit/utils';

Expand All @@ -30,10 +31,12 @@ function assignKeys(
record: Record,
properties: Dict<unknown>
) {
const keys = modelDefinition.keys || {};
for (let key of Object.keys(keys)) {
if (properties[key] !== undefined) {
deepSet(record, ['keys', key], properties[key]);
const keyDefs = modelDefinition.keys;
if (keyDefs) {
for (let key of Object.keys(keyDefs)) {
if (properties[key] !== undefined) {
deepSet(record, ['keys', key], properties[key]);
}
}
}
}
Expand All @@ -43,10 +46,12 @@ function assignAttributes(
record: Record,
properties: Dict<unknown>
) {
const attributes = modelDefinition.attributes || {};
for (let attribute of Object.keys(attributes)) {
if (properties[attribute] !== undefined) {
deepSet(record, ['attributes', attribute], properties[attribute]);
const attributeDefs = modelDefinition.attributes;
if (attributeDefs) {
for (let attribute of Object.keys(attributeDefs)) {
if (properties[attribute] !== undefined) {
deepSet(record, ['attributes', attribute], properties[attribute]);
}
}
}
}
Expand All @@ -56,23 +61,26 @@ function assignRelationships(
record: Record,
properties: Dict<unknown>
) {
const relationships = modelDefinition.relationships || {};
for (let relationship of Object.keys(relationships)) {
if (properties[relationship] !== undefined) {
let relationshipType = relationships[relationship].type as
| string
| string[];
let relationshipProperties = properties[relationship] as
| RecordIdentity
| RecordIdentity[]
| string
| string[]
| null;
deepSet(
record,
['relationships', relationship],
normalizeRelationship(relationshipType, relationshipProperties)
);
const relationshipDefs = modelDefinition.relationships;
if (relationshipDefs) {
for (let relationship of Object.keys(relationshipDefs)) {
if (properties[relationship] !== undefined) {
let relationshipDef = relationshipDefs[
relationship
] as RelationshipDefinition;
let relationshipType = relationshipDef.type as string | string[];
let relationshipProperties = properties[relationship] as
| RecordIdentity
| RecordIdentity[]
| string
| string[]
| null;
deepSet(
record,
['relationships', relationship],
normalizeRelationship(relationshipType, relationshipProperties)
);
}
}
}
}
Expand Down
88 changes: 51 additions & 37 deletions addon/-private/utils/property-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,90 +44,104 @@ export class PropertyCache<T> {
}

export function notifyPropertyChange(record: Model, property: string) {
const cache = caches.get(record);
if (cache && cache[property]) {
cache[property].notifyPropertyChange();
// TODO: there is an issue with glimmer cache and ember CP macros
// https://github.com/ember-polyfills/ember-cache-primitive-polyfill/issues/78
// in order to fix it for now we are calling Ember.notifyPropertyChange();
emberNotifyPropertyChange(record, property);
}
caches.get(record)?.[property]?.notifyPropertyChange();

// TODO: there is an issue with glimmer cache and ember CP macros
// https://github.com/ember-polyfills/ember-cache-primitive-polyfill/issues/78
// in order to fix it for now we are calling Ember.notifyPropertyChange();
emberNotifyPropertyChange(record, property);
}

export function getKeyCache(
record: Model,
property: string
): PropertyCache<unknown> {
let cache = caches.get(record);
let recordCaches = caches.get(record);

if (!cache) {
cache = {};
caches.set(record, cache);
if (recordCaches === undefined) {
recordCaches = {};
caches.set(record, recordCaches);
}
if (!cache[property]) {
cache[property] = new PropertyCache(() => record.getKey(property));

let propertyCache = recordCaches[property];

if (propertyCache === undefined) {
propertyCache = recordCaches[property] = new PropertyCache(() =>
record.getKey(property)
);
}

return cache[property];
return propertyCache;
}

export function getAttributeCache(
record: Model,
property: string
): PropertyCache<unknown> {
let cache = caches.get(record);
let recordCaches = caches.get(record);

if (!cache) {
cache = {};
caches.set(record, cache);
if (recordCaches === undefined) {
recordCaches = {};
caches.set(record, recordCaches);
}
if (!cache[property]) {
cache[property] = new PropertyCache(() => record.getAttribute(property));

let propertyCache = recordCaches[property];

if (propertyCache === undefined) {
propertyCache = recordCaches[property] = new PropertyCache(() =>
record.getAttribute(property)
);
}

return cache[property];
return propertyCache;
}

export function getHasOneCache(
record: Model,
property: string
): PropertyCache<unknown> {
let cache = caches.get(record);
let recordCaches = caches.get(record);

if (!cache) {
cache = {};
caches.set(record, cache);
if (recordCaches === undefined) {
recordCaches = {};
caches.set(record, recordCaches);
}
if (!cache[property]) {
cache[property] = new PropertyCache(() =>

let propertyCache = recordCaches[property];

if (propertyCache === undefined) {
propertyCache = recordCaches[property] = new PropertyCache(() =>
record.getRelatedRecord(property)
);
}

return cache[property];
return propertyCache;
}

export function getHasManyCache(
record: Model,
property: string
): PropertyCache<unknown> {
let cache = caches.get(record);
let recordCaches = caches.get(record);

if (!cache) {
cache = {};
caches.set(record, cache);
if (recordCaches === undefined) {
recordCaches = {};
caches.set(record, recordCaches);
}
if (!cache[property]) {
cache[property] = new PropertyCache(() =>

let propertyCache = recordCaches[property];

if (propertyCache === undefined) {
propertyCache = recordCaches[property] = new PropertyCache(() =>
addLegacyMutationMethods(
record,
property,
record.getRelatedRecords(property) || []
record.getRelatedRecords(property) ?? []
)
);
}

return cache[property];
return propertyCache;
}

function addLegacyMutationMethods(
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@
"@ember/test-helpers": "^2.2.0",
"@glimmer/component": "^1.0.3",
"@types/ember": "^3.16.0",
"@types/ember-qunit": "^3.4.11",
"@types/ember-testing-helpers": "^0.0.4",
"@types/qunit": "^2.9.4",
"@types/ember-qunit": "^3.4.13",
"@types/ember__test": "^3.16.1",
"@types/ember__test-helpers": "^1.7.3",
"@types/qunit": "^2.11.1",
"@types/rsvp": "^4.0.3",
"@typescript-eslint/parser": "^4.2.0",
"babel-eslint": "^10.1.0",
Expand Down Expand Up @@ -90,7 +91,7 @@
"prettier": "^2.2.1",
"qunit": "^2.14.0",
"qunit-dom": "^1.6.0",
"typescript": "^3.9.7"
"typescript": "^4.1.5"
},
"engines": {
"node": "10.* || >= 12"
Expand All @@ -102,7 +103,7 @@
"configPath": "tests/dummy/config"
},
"peerDependencies": {
"ember-source": "~3.16.0"
"ember-source": "^3.16.0"
},
"volta": {
"node": "10.23.0",
Expand Down
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"inlineSourceMap": true,
"inlineSources": true,
"strict": true,
"alwaysStrict": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"baseUrl": ".",
"paths": {
"dummy/tests/*": ["tests/*"],
Expand All @@ -23,7 +29,7 @@
"include": [
"app/**/*",
"addon/**/*",
"tests/**/*",
"tests/**/*.ts",
"types/**/*",
"test-support/**/*",
"addon-test-support/**/*"
Expand Down
Loading