Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to disable src cache #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion projects/ngx-avatar/src/lib/avatar-config.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AvatarConfigService } from './avatar-config.service';
import { AvatarSource } from './sources/avatar-source.enum';
import { AvatarConfig } from './avatar-config';
import { defaultSources, defaultColors } from './avatar.service';
import { defaultSources, defaultColors, defaultDisableSrcCache } from './avatar.service';

describe('AvatarConfigService', () => {
describe('AvatarSources', () => {
Expand Down Expand Up @@ -104,4 +104,27 @@ describe('AvatarConfigService', () => {
);
});
});

describe('AvatarDisableCache', () => {
it('should return the user\'s disable custom source cache settings when provided in the avatar configuration', () => {
const userDisableSrcCache = true;
const userConfig: AvatarConfig = {
disableSrcCache: userDisableSrcCache
};

const avatarConfigService = new AvatarConfigService(userConfig);

expect(avatarConfigService.getDisableSrcCache(defaultDisableSrcCache)).toBe(
userDisableSrcCache
);
});

it('should return the default disable custom source cache settings when no settings are provided in the avatar configuration', () => {
const avatarConfigService = new AvatarConfigService({});

expect(avatarConfigService.getDisableSrcCache(defaultDisableSrcCache)).toBe(
defaultDisableSrcCache
);
});
});
});
5 changes: 5 additions & 0 deletions projects/ngx-avatar/src/lib/avatar-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ export class AvatarConfigService {
defaultColors
);
}

public getDisableSrcCache(defaultDisableSrcCache: boolean): boolean {
if (this.userConfig.disableSrcCache === undefined) return defaultDisableSrcCache;
return this.userConfig.disableSrcCache;
}
}
5 changes: 5 additions & 0 deletions projects/ngx-avatar/src/lib/avatar-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ export interface AvatarConfig {
* The order in which the avatar sources will be used.
*/
sourcePriorityOrder?: AvatarSource[];

/**
* Disable custom source (for custom images) cache.
*/
disableSrcCache?: boolean;
}
5 changes: 5 additions & 0 deletions projects/ngx-avatar/src/lib/avatar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export const defaultColors = [
'#7f8c8d'
];

/**
* Default disable custom source cache settings
*/
export const defaultDisableSrcCache = false;

/**
* Provides utilities methods related to Avatar component
*/
Expand Down
18 changes: 18 additions & 0 deletions projects/ngx-avatar/src/lib/sources/custom-no-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Source } from './source';
import { AvatarSource } from './avatar-source.enum';
/**
* Custom source implementation (with no cache).
* return custom image as an avatar
*
*/
export class CustomNoCache implements Source {
readonly sourceType: AvatarSource = AvatarSource.CUSTOM;

constructor(public sourceId: string) {}

public getAvatar(): string {
const urlSuffix = Math.random();
const sourceId = `${this.sourceId}${this.sourceId.indexOf('?') > -1 ? '&' : '?'}_=${urlSuffix}`;
return sourceId;
}
}
9 changes: 7 additions & 2 deletions projects/ngx-avatar/src/lib/sources/source.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { Vkontakte } from './vkontakte';
import { Github } from './github';
import { SourceCreator } from './source.creator';
import { AvatarSource } from './avatar-source.enum';
import { AvatarConfigService } from '../avatar-config.service';
import { defaultDisableSrcCache } from '../avatar.service';
import { CustomNoCache } from './custom-no-cache';

/**
* Factory class that implements factory method pattern.
Expand All @@ -23,14 +26,16 @@ import { AvatarSource } from './avatar-source.enum';
export class SourceFactory {
private sources: { [key: string]: SourceCreator } = {};

constructor() {
constructor(avatarConfigService: AvatarConfigService) {
const disableSrcCache = avatarConfigService.getDisableSrcCache(defaultDisableSrcCache);

this.sources[AvatarSource.FACEBOOK] = Facebook;
this.sources[AvatarSource.TWITTER] = Twitter;
this.sources[AvatarSource.GOOGLE] = Google;
this.sources[AvatarSource.INSTAGRAM] = Instagram;
this.sources[AvatarSource.SKYPE] = Skype;
this.sources[AvatarSource.GRAVATAR] = Gravatar;
this.sources[AvatarSource.CUSTOM] = Custom;
this.sources[AvatarSource.CUSTOM] = disableSrcCache ? CustomNoCache : Custom;
this.sources[AvatarSource.INITIALS] = Initials;
this.sources[AvatarSource.VALUE] = Value;
this.sources[AvatarSource.VKONTAKTE] = Vkontakte;
Expand Down