Skip to content

Commit

Permalink
feat: rename LifetimesService => CachePolicy for clarity (#9444)
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired authored May 16, 2024
1 parent fdd783b commit de51331
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 82 deletions.
4 changes: 2 additions & 2 deletions packages/active-record/src/-private/builders/find-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export type FindRecordResultDocument<T> = Omit<SingleResourceDataDocument<T>, 'd
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing and underscoring the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down
4 changes: 2 additions & 2 deletions packages/active-record/src/-private/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ import { copyForwardUrlOptions, extractCacheOptions } from './-utils';
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing and underscoring the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down
12 changes: 6 additions & 6 deletions packages/active-record/src/-private/builders/save-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ function isExisting(identifier: StableRecordIdentifier): identifier is StableExi
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down Expand Up @@ -128,9 +128,9 @@ export function deleteRecord(record: unknown, options: ConstrainedRequestOptions
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down Expand Up @@ -202,9 +202,9 @@ export function createRecord(record: unknown, options: ConstrainedRequestOptions
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down
2 changes: 1 addition & 1 deletion packages/core-types/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type CacheOptions<T = unknown> = {
/**
* Useful for metadata around when to invalidate the cache. Typically used
* by strategies that invalidate requests by resource type when a new resource
* of that type has been created. See the LifetimesService implementation
* of that type has been created. See the CachePolicy implementation
* provided by `@ember-data/request-utils` for an example.
*
* It is recommended to only use this for query/queryRecord requests where
Expand Down
4 changes: 2 additions & 2 deletions packages/json-api/src/-private/builders/find-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import { ACCEPT_HEADER_VALUE, copyForwardUrlOptions, extractCacheOptions } from
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down
8 changes: 4 additions & 4 deletions packages/json-api/src/-private/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ import { ACCEPT_HEADER_VALUE, copyForwardUrlOptions, extractCacheOptions } from
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down Expand Up @@ -137,9 +137,9 @@ export function query(
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down
12 changes: 6 additions & 6 deletions packages/json-api/src/-private/builders/save-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ function isExisting(identifier: StableRecordIdentifier): identifier is StableExi
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down Expand Up @@ -126,9 +126,9 @@ export function deleteRecord(record: unknown, options: ConstrainedRequestOptions
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down Expand Up @@ -200,9 +200,9 @@ export function createRecord(record: unknown, options: ConstrainedRequestOptions
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function saveRecord<T>(context: StoreRequestContext): Promise<T> {

// blatantly lie if we were a createRecord request
// to give some semblance of cache-control to the
// lifetimes service while legacy is still around
// CachePolicy while legacy is still around
if (store.lifetimes?.didRequest && operation === 'createRecord') {
store.lifetimes.didRequest(context.request, { status: 201 } as Response, null, store);
}
Expand Down
55 changes: 34 additions & 21 deletions packages/request-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,10 @@ function isStale(headers: Headers, expirationTime: number): boolean {
return result;
}

export type LifetimesConfig = { apiCacheSoftExpires: number; apiCacheHardExpires: number };
export type PolicyConfig = { apiCacheSoftExpires: number; apiCacheHardExpires: number };

/**
* A basic LifetimesService that can be added to the Store service.
* A basic CachePolicy that can be added to the Store service.
*
* Determines staleness based on time since the request was last received from the API
* using the `date` header.
Expand Down Expand Up @@ -678,25 +678,25 @@ export type LifetimesConfig = { apiCacheSoftExpires: number; apiCacheHardExpires
* Usage:
*
* ```ts
* import { LifetimesService } from '@ember-data/request-utils';
* import { CachePolicy } from '@ember-data/request-utils';
* import DataStore from '@ember-data/store';
*
* // ...
*
* export class Store extends DataStore {
* constructor(args) {
* super(args);
* this.lifetimes = new LifetimesService({ apiCacheSoftExpires: 30_000, apiCacheHardExpires: 60_000 });
* this.lifetimes = new CachePolicy({ apiCacheSoftExpires: 30_000, apiCacheHardExpires: 60_000 });
* }
* }
* ```
*
* @class LifetimesService
* @class CachePolicy
* @public
* @module @ember-data/request-utils
*/
export class LifetimesService {
declare config: LifetimesConfig;
export class CachePolicy {
declare config: PolicyConfig;
declare _stores: WeakMap<Store, { invalidated: Set<string>; types: Map<string, Set<string>> }>;

_getStore(store: Store): { invalidated: Set<string>; types: Map<string, Set<string>> } {
Expand All @@ -708,12 +708,12 @@ export class LifetimesService {
return set;
}

constructor(config: LifetimesConfig) {
constructor(config: PolicyConfig) {
this._stores = new WeakMap();

const _config = arguments.length === 1 ? config : (arguments[1] as unknown as LifetimesConfig);
const _config = arguments.length === 1 ? config : (arguments[1] as unknown as PolicyConfig);
deprecate(
`Passing a Store to the LifetimesService is deprecated, please pass only a config instead.`,
`Passing a Store to the CachePolicy is deprecated, please pass only a config instead.`,
arguments.length === 1,
{
id: 'ember-data:request-utils:lifetimes-service-store-arg',
Expand All @@ -725,22 +725,16 @@ export class LifetimesService {
until: '6.0',
}
);
assert(`You must pass a config to the LifetimesService`, _config);
assert(
`You must pass a apiCacheSoftExpires to the LifetimesService`,
typeof _config.apiCacheSoftExpires === 'number'
);
assert(
`You must pass a apiCacheHardExpires to the LifetimesService`,
typeof _config.apiCacheHardExpires === 'number'
);
assert(`You must pass a config to the CachePolicy`, _config);
assert(`You must pass a apiCacheSoftExpires to the CachePolicy`, typeof _config.apiCacheSoftExpires === 'number');
assert(`You must pass a apiCacheHardExpires to the CachePolicy`, typeof _config.apiCacheHardExpires === 'number');
this.config = _config;
}

/**
* Invalidate a request by its identifier for a given store instance.
*
* While the store argument may seem redundant, the lifetimes service
* While the store argument may seem redundant, the CachePolicy
* is designed to be shared across multiple stores / forks
* of the store.
*
Expand All @@ -761,7 +755,7 @@ export class LifetimesService {
* Invalidate all requests associated to a specific type
* for a given store instance.
*
* While the store argument may seem redundant, the lifetimes service
* While the store argument may seem redundant, the CachePolicy
* is designed to be shared across multiple stores / forks
* of the store.
*
Expand Down Expand Up @@ -892,3 +886,22 @@ export class LifetimesService {
return !cached || !cached.response || isStale(cached.response.headers, this.config.apiCacheSoftExpires);
}
}

export class LifetimesService extends CachePolicy {
constructor(config: PolicyConfig) {
deprecate(
`\`import { LifetimesService } from '@ember-data/request-utils';\` is deprecated, please use \`import { CachePolicy } from '@ember-data/request-utils';\` instead.`,
false,
{
id: 'ember-data:deprecate-lifetimes-service-import',
since: {
enabled: '5.4',
available: '5.4',
},
for: 'ember-data',
until: '6.0',
}
);
super(config);
}
}
4 changes: 2 additions & 2 deletions packages/rest/src/-private/builders/find-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ import { copyForwardUrlOptions, extractCacheOptions } from './-utils';
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing and camelCasing the supplied type
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
* option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
* promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
* defaulting to `false` if none is configured.
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
*
Expand Down
Loading

0 comments on commit de51331

Please sign in to comment.