Skip to content

Commit

Permalink
Use Map for instances type
Browse files Browse the repository at this point in the history
  • Loading branch information
MurhafSousli committed Oct 4, 2018
1 parent 7c82516 commit ac08077
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions projects/core/src/lib/services/gallery.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { GalleryConfig } from '../models';
export class Gallery {

/** Stores GalleryRef instances */
private readonly _instances = {};
private readonly _instances = new Map<string, GalleryRef>();

/** Global config */
config: GalleryConfig;
Expand All @@ -18,45 +18,38 @@ export class Gallery {
this.config = {...defaultConfig, ...config};
}

/** Checks if gallery exists */
hasRef(id = 'root'): boolean {
return this._instances[id] instanceof GalleryRef;
}

/** Returns Gallery by ID */
ref(id = 'root', config?: GalleryConfig): GalleryRef {
if (this.hasRef(id)) {
if (this._instances.has(id)) {
const galleryRef = this._instances.get(id);
if (config) {
this._instances[id].setConfig({...this.config, ...config});
galleryRef.setConfig({...this.config, ...config});
}
return this._instances[id];
return galleryRef;
} else {
return this._instances[id] = new GalleryRef({...this.config, ...config});
return this._instances.set(id, new GalleryRef({...this.config, ...config})).get(id);
}
}

/** Destroy a gallery instance */
destroy(id = 'root') {
if (this.hasRef(id)) {
this._instances[id].destroy();
this._instances[id] = null;
if (this._instances.has(id)) {
this._instances.get(id).destroy();
this._instances.delete(id);
}
}

/** Destroy all gallery instances */
destroyAll() {
Object.keys(this._instances)
.map((key) => {
this._instances[key].destory();
this._instances[key] = null;
});
this._instances.forEach((ref: GalleryRef, id: string) => {
ref.destroy();
this._instances.delete(id);
});
}

/** Reset all gallery instances */
resetAll() {
Object.keys(this._instances)
.map((id = 'root') => this._instances[id].gallery)
.map((gallery: GalleryRef) => gallery.reset());
this._instances.forEach((ref: GalleryRef) => ref.reset());
}

}

0 comments on commit ac08077

Please sign in to comment.