Skip to content

Commit

Permalink
address feedback other than feature-flags
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Jul 19, 2019
1 parent 4cbd9ed commit 5a761a0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
39 changes: 27 additions & 12 deletions packages/store/addon/-private/identifiers/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ import uuidv4 from './utils/uuid-v4';
import normalizeModelName from '../system/normalize-model-name';
import isStableIdentifier from './is-stable-identifier';
import isNonEmptyString from '../utils/is-non-empty-string';

type IdentifierMap = Dict<string, StableRecordIdentifier>;
type TypeMap = Dict<string, KeyOptions>;
import Store from '../system/store';

interface KeyOptions {
lid: IdentifierMap;
id: IdentifierMap;
_allIdentifiers: StableRecordIdentifier[];
}

type IdentifierMap = Dict<string, StableRecordIdentifier>;
type TypeMap = Dict<string, KeyOptions>;

let configuredForgetMethod: ForgetMethod;
let configuredGenerationMethod: GenerationMethod;
let configuredResetMethod: ResetMethod;
Expand Down Expand Up @@ -59,9 +60,9 @@ function defaultGenerationMethod(data: ResourceIdentifierObject, bucket: string)
return uuidv4();
}

const IdentifierCaches = new WeakMap<object, IdentifierCache>();
const IdentifierCaches = new WeakMap<Store, IdentifierCache>();

export function identifierCacheFor(store: object): IdentifierCache {
export function identifierCacheFor(store: Store): IdentifierCache {
let cache = IdentifierCaches.get(store);

if (cache === undefined) {
Expand Down Expand Up @@ -101,12 +102,15 @@ export class IdentifierCache {
this._reset = configuredResetMethod || defaultEmptyCallback;
}

// allows us to peek without generating when needed
// useful for the "create" case when we need to see if
// we are accidentally overwritting something
peekRecordIdentifier(resource: ResourceIdentifierObject, shouldGenerate: true): StableRecordIdentifier;
peekRecordIdentifier(resource: ResourceIdentifierObject, shouldGenerate: false): StableRecordIdentifier | undefined;
peekRecordIdentifier(
/**
* @internal
*/
private _getRecordIdentifier(resource: ResourceIdentifierObject, shouldGenerate: true): StableRecordIdentifier;
private _getRecordIdentifier(
resource: ResourceIdentifierObject,
shouldGenerate: false
): StableRecordIdentifier | undefined;
private _getRecordIdentifier(
resource: ResourceIdentifierObject,
shouldGenerate: boolean = false
): StableRecordIdentifier | undefined {
Expand Down Expand Up @@ -204,6 +208,17 @@ export class IdentifierCache {
return identifier;
}

/**
* allows us to peek without generating when needed
* useful for the "create" case when we need to see if
* we are accidentally overwritting something
*
* @internal
*/
peekRecordIdentifier(resource: ResourceIdentifierObject): StableRecordIdentifier | undefined {
return this._getRecordIdentifier(resource, false);
}

/*
Returns the Identifier for the given Resource, creates one if it does not yet exist.
Expand All @@ -215,7 +230,7 @@ export class IdentifierCache {
- this referential stability of the object itself is guaranteed
*/
getOrCreateRecordIdentifier(resource: ResourceIdentifierObject): StableRecordIdentifier {
return this.peekRecordIdentifier(resource, true);
return this._getRecordIdentifier(resource, true);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { StableRecordIdentifier, IS_IDENTIFIER } from '../ts-interfaces/identifier';

export default function isStableIdentifier(
identifier: StableRecordIdentifier | any
): identifier is StableRecordIdentifier {
export default function isStableIdentifier(identifier: any): identifier is StableRecordIdentifier {
return identifier[IS_IDENTIFIER] === true;
}
2 changes: 1 addition & 1 deletion packages/store/addon/-private/utils/is-non-empty-string.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function isNonEmptyString(str?: any): str is string {
export default function isNonEmptyString(str: any): str is string {
return typeof str === 'string' && str.length > 0;
}

0 comments on commit 5a761a0

Please sign in to comment.