Skip to content

Commit

Permalink
fix cached
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed May 10, 2021
1 parent c5164d8 commit 6aca536
Show file tree
Hide file tree
Showing 4 changed files with 906 additions and 854 deletions.
3 changes: 2 additions & 1 deletion packages/model/addon/-private/record-state.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { assert } from '@ember/debug';
import { dependentKeyCompat } from '@ember/object/compat';
import { cached, tracked } from '@glimmer/tracking';
import { tracked } from '@glimmer/tracking';

import notifyChanges from './notify-changes';
import cached from './tmp-cached-polyfill';

type RecordData = import('@ember-data/record-data/-private').RecordData;
type RequestCache = import('@ember-data/store/-private/system/request-cache').default;
Expand Down
42 changes: 42 additions & 0 deletions packages/model/addon/-private/tmp-cached-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { assert } from '@ember/debug';
import { createCache, getValue } from '@glimmer/tracking/primitives/cache';

/**
* Implements the @cached decorator from Ember. Ported from ember-cached-decorator-polyfill
*
* This can be removed once the polyfill works with ember 3.27+
* See: https://github.com/ember-polyfills/ember-cached-decorator-polyfill/issues/70
*/
export default function cached(...args) {
const [target, key, descriptor] = args; // Error on `@cached()`, `@cached(...args)`, and `@cached propName = value;`

true &&
!(target !== undefined) &&
assert(
'You attempted to use @cached(), which is not necessary nor supported. Remove the parentheses and you will be good to go!',
target !== undefined
);
true &&
!(typeof target === 'object' && typeof key === 'string' && typeof descriptor === 'object' && args.length === 3) &&
assert(
`You attempted to use @cached on with ${args.length > 1 ? 'arguments' : 'an argument'} ( @cached(${args
.map((d) => `'${d}'`)
.join(
', '
)}), which is not supported. Dependencies are automatically tracked, so you can just use ${'`@cached`'}`,
typeof target === 'object' && typeof key === 'string' && typeof descriptor === 'object' && args.length === 3
);
true &&
!(typeof descriptor.get === 'function') &&
assert(
`The @cached decorator must be applied to getters. '${key}' is not a getter.`,
typeof descriptor.get === 'function'
);
const caches = new WeakMap();
const getter = descriptor.get;

descriptor.get = function () {
if (!caches.has(this)) caches.set(this, createCache(getter.bind(this)));
return getValue(caches.get(this));
};
}
2 changes: 1 addition & 1 deletion packages/model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"ember-cli-string-utils": "^1.1.0",
"ember-cli-test-info": "^1.0.0",
"ember-cli-typescript": "^4.1.0",
"ember-cached-decorator-polyfill": "^0.1.1",
"ember-cache-primitive-polyfill": "^1.0.1",
"ember-compatibility-helpers": "^1.2.0",
"inflection": "1.12.0"
},
Expand Down
Loading

0 comments on commit 6aca536

Please sign in to comment.