Skip to content

Commit eb92a27

Browse files
authored
Merge pull request #328 from orbitjs/typings
Improve typings and fix ember-source peer dependency
2 parents dfbf614 + 6666a70 commit eb92a27

File tree

10 files changed

+134
-88
lines changed

10 files changed

+134
-88
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { getContext } from '@ember/test-helpers';
2+
import { Source } from '@orbit/data';
23

3-
export async function waitForSource(sourceOrSourceName) {
4+
export async function waitForSource(
5+
sourceOrSourceName: Source | string
6+
): Promise<void> {
47
let source;
58
if (typeof sourceOrSourceName === 'string') {
6-
let { owner } = getContext();
9+
let { owner } = getContext() as any;
710
source = owner.lookup(`data-source:${sourceOrSourceName}`);
811
if (!source) {
912
throw new Error(

addon/-private/model-factory.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import Orbit from '@orbit/core';
12
import { getOwner } from '@ember/application';
23
import { Dict } from '@orbit/utils';
34
import { RecordIdentity, cloneRecordIdentity } from '@orbit/records';
45
import Store from './store';
56
import Model, { ModelSettings } from './model';
67

8+
const { assert } = Orbit;
9+
710
interface Factory {
811
create(settings: ModelSettings): Model;
912
}
@@ -34,7 +37,16 @@ export default class ModelFactory {
3437
if (!modelFactory) {
3538
let owner = getOwner(this.#store);
3639
let orbitConfig = owner.lookup('ember-orbit:config');
37-
modelFactory = owner.factoryFor(`${orbitConfig.types.model}:${type}`);
40+
41+
modelFactory = owner.factoryFor(
42+
`${orbitConfig.types.model}:${type}`
43+
) as Factory;
44+
45+
assert(
46+
`An ember-orbit model for type ${type} has not been registered.`,
47+
modelFactory !== undefined
48+
);
49+
3850
this.#modelFactoryMap[type] = modelFactory;
3951
}
4052

addon/-private/system/modules-of-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function (prefix: string, type: string): string[] {
1515
const matches = regex.exec(moduleName);
1616
if (matches && matches.length === 1) {
1717
const matchedName = moduleName.match(/[^\/]+\/?$/);
18-
if (matchedName) {
18+
if (matchedName?.[0]) {
1919
found.push(matchedName[0]);
2020
}
2121
}

addon/-private/utils/normalize-record-properties.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
RecordRelationship,
55
Record,
66
ModelDefinition,
7-
RecordIdentity
7+
RecordIdentity,
8+
RelationshipDefinition
89
} from '@orbit/records';
910
import { deepSet, Dict } from '@orbit/utils';
1011

@@ -30,10 +31,12 @@ function assignKeys(
3031
record: Record,
3132
properties: Dict<unknown>
3233
) {
33-
const keys = modelDefinition.keys || {};
34-
for (let key of Object.keys(keys)) {
35-
if (properties[key] !== undefined) {
36-
deepSet(record, ['keys', key], properties[key]);
34+
const keyDefs = modelDefinition.keys;
35+
if (keyDefs) {
36+
for (let key of Object.keys(keyDefs)) {
37+
if (properties[key] !== undefined) {
38+
deepSet(record, ['keys', key], properties[key]);
39+
}
3740
}
3841
}
3942
}
@@ -43,10 +46,12 @@ function assignAttributes(
4346
record: Record,
4447
properties: Dict<unknown>
4548
) {
46-
const attributes = modelDefinition.attributes || {};
47-
for (let attribute of Object.keys(attributes)) {
48-
if (properties[attribute] !== undefined) {
49-
deepSet(record, ['attributes', attribute], properties[attribute]);
49+
const attributeDefs = modelDefinition.attributes;
50+
if (attributeDefs) {
51+
for (let attribute of Object.keys(attributeDefs)) {
52+
if (properties[attribute] !== undefined) {
53+
deepSet(record, ['attributes', attribute], properties[attribute]);
54+
}
5055
}
5156
}
5257
}
@@ -56,23 +61,26 @@ function assignRelationships(
5661
record: Record,
5762
properties: Dict<unknown>
5863
) {
59-
const relationships = modelDefinition.relationships || {};
60-
for (let relationship of Object.keys(relationships)) {
61-
if (properties[relationship] !== undefined) {
62-
let relationshipType = relationships[relationship].type as
63-
| string
64-
| string[];
65-
let relationshipProperties = properties[relationship] as
66-
| RecordIdentity
67-
| RecordIdentity[]
68-
| string
69-
| string[]
70-
| null;
71-
deepSet(
72-
record,
73-
['relationships', relationship],
74-
normalizeRelationship(relationshipType, relationshipProperties)
75-
);
64+
const relationshipDefs = modelDefinition.relationships;
65+
if (relationshipDefs) {
66+
for (let relationship of Object.keys(relationshipDefs)) {
67+
if (properties[relationship] !== undefined) {
68+
let relationshipDef = relationshipDefs[
69+
relationship
70+
] as RelationshipDefinition;
71+
let relationshipType = relationshipDef.type as string | string[];
72+
let relationshipProperties = properties[relationship] as
73+
| RecordIdentity
74+
| RecordIdentity[]
75+
| string
76+
| string[]
77+
| null;
78+
deepSet(
79+
record,
80+
['relationships', relationship],
81+
normalizeRelationship(relationshipType, relationshipProperties)
82+
);
83+
}
7684
}
7785
}
7886
}

addon/-private/utils/property-cache.ts

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,90 +44,104 @@ export class PropertyCache<T> {
4444
}
4545

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

5755
export function getKeyCache(
5856
record: Model,
5957
property: string
6058
): PropertyCache<unknown> {
61-
let cache = caches.get(record);
59+
let recordCaches = caches.get(record);
6260

63-
if (!cache) {
64-
cache = {};
65-
caches.set(record, cache);
61+
if (recordCaches === undefined) {
62+
recordCaches = {};
63+
caches.set(record, recordCaches);
6664
}
67-
if (!cache[property]) {
68-
cache[property] = new PropertyCache(() => record.getKey(property));
65+
66+
let propertyCache = recordCaches[property];
67+
68+
if (propertyCache === undefined) {
69+
propertyCache = recordCaches[property] = new PropertyCache(() =>
70+
record.getKey(property)
71+
);
6972
}
7073

71-
return cache[property];
74+
return propertyCache;
7275
}
7376

7477
export function getAttributeCache(
7578
record: Model,
7679
property: string
7780
): PropertyCache<unknown> {
78-
let cache = caches.get(record);
81+
let recordCaches = caches.get(record);
7982

80-
if (!cache) {
81-
cache = {};
82-
caches.set(record, cache);
83+
if (recordCaches === undefined) {
84+
recordCaches = {};
85+
caches.set(record, recordCaches);
8386
}
84-
if (!cache[property]) {
85-
cache[property] = new PropertyCache(() => record.getAttribute(property));
87+
88+
let propertyCache = recordCaches[property];
89+
90+
if (propertyCache === undefined) {
91+
propertyCache = recordCaches[property] = new PropertyCache(() =>
92+
record.getAttribute(property)
93+
);
8694
}
8795

88-
return cache[property];
96+
return propertyCache;
8997
}
9098

9199
export function getHasOneCache(
92100
record: Model,
93101
property: string
94102
): PropertyCache<unknown> {
95-
let cache = caches.get(record);
103+
let recordCaches = caches.get(record);
96104

97-
if (!cache) {
98-
cache = {};
99-
caches.set(record, cache);
105+
if (recordCaches === undefined) {
106+
recordCaches = {};
107+
caches.set(record, recordCaches);
100108
}
101-
if (!cache[property]) {
102-
cache[property] = new PropertyCache(() =>
109+
110+
let propertyCache = recordCaches[property];
111+
112+
if (propertyCache === undefined) {
113+
propertyCache = recordCaches[property] = new PropertyCache(() =>
103114
record.getRelatedRecord(property)
104115
);
105116
}
106117

107-
return cache[property];
118+
return propertyCache;
108119
}
109120

110121
export function getHasManyCache(
111122
record: Model,
112123
property: string
113124
): PropertyCache<unknown> {
114-
let cache = caches.get(record);
125+
let recordCaches = caches.get(record);
115126

116-
if (!cache) {
117-
cache = {};
118-
caches.set(record, cache);
127+
if (recordCaches === undefined) {
128+
recordCaches = {};
129+
caches.set(record, recordCaches);
119130
}
120-
if (!cache[property]) {
121-
cache[property] = new PropertyCache(() =>
131+
132+
let propertyCache = recordCaches[property];
133+
134+
if (propertyCache === undefined) {
135+
propertyCache = recordCaches[property] = new PropertyCache(() =>
122136
addLegacyMutationMethods(
123137
record,
124138
property,
125-
record.getRelatedRecords(property) || []
139+
record.getRelatedRecords(property) ?? []
126140
)
127141
);
128142
}
129143

130-
return cache[property];
144+
return propertyCache;
131145
}
132146

133147
function addLegacyMutationMethods(

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@
5353
"@ember/test-helpers": "^2.2.0",
5454
"@glimmer/component": "^1.0.3",
5555
"@types/ember": "^3.16.0",
56-
"@types/ember-qunit": "^3.4.11",
57-
"@types/ember-testing-helpers": "^0.0.4",
58-
"@types/qunit": "^2.9.4",
56+
"@types/ember-qunit": "^3.4.13",
57+
"@types/ember__test": "^3.16.1",
58+
"@types/ember__test-helpers": "^1.7.3",
59+
"@types/qunit": "^2.11.1",
5960
"@types/rsvp": "^4.0.3",
6061
"@typescript-eslint/parser": "^4.2.0",
6162
"babel-eslint": "^10.1.0",
@@ -90,7 +91,7 @@
9091
"prettier": "^2.2.1",
9192
"qunit": "^2.14.0",
9293
"qunit-dom": "^1.6.0",
93-
"typescript": "^3.9.7"
94+
"typescript": "^4.1.5"
9495
},
9596
"engines": {
9697
"node": "10.* || >= 12"
@@ -102,7 +103,7 @@
102103
"configPath": "tests/dummy/config"
103104
},
104105
"peerDependencies": {
105-
"ember-source": "~3.16.0"
106+
"ember-source": "^3.16.0"
106107
},
107108
"volta": {
108109
"node": "10.23.0",

tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"inlineSourceMap": true,
99
"inlineSources": true,
1010
"strict": true,
11+
"alwaysStrict": true,
12+
"noFallthroughCasesInSwitch": true,
13+
"noUnusedLocals": true,
14+
"noUnusedParameters": true,
15+
"noImplicitReturns": true,
16+
"noUncheckedIndexedAccess": true,
1117
"baseUrl": ".",
1218
"paths": {
1319
"dummy/tests/*": ["tests/*"],
@@ -23,7 +29,7 @@
2329
"include": [
2430
"app/**/*",
2531
"addon/**/*",
26-
"tests/**/*",
32+
"tests/**/*.ts",
2733
"types/**/*",
2834
"test-support/**/*",
2935
"addon-test-support/**/*"

0 commit comments

Comments
 (0)