Skip to content

Commit

Permalink
Improve gallery destroyer
Browse files Browse the repository at this point in the history
  • Loading branch information
MurhafSousli committed Oct 4, 2018
1 parent ac08077 commit 65f3358
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion projects/core/src/lib/components/gallery.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class GalleryComponent implements OnInit, OnChanges, OnDestroy {
this._playingChange$.unsubscribe();
this._playerListener$.unsubscribe();
if (this.destroyRef) {
this.galleryRef.reset();
this.galleryRef.destroy();
}
}

Expand Down
9 changes: 6 additions & 3 deletions projects/core/src/lib/services/gallery-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class GalleryRef {

/** Stream that emits gallery state */
private readonly _state$: BehaviorSubject<GalleryState>;
state: GalleryState = defaultState;

/** Stream that emits gallery config */
private readonly _config$: BehaviorSubject<GalleryConfig>;
Expand Down Expand Up @@ -62,8 +63,8 @@ export class GalleryRef {
return this.state$.pipe(filterActions([GalleryAction.PLAY, GalleryAction.STOP, GalleryAction.INDEX_CHANGED]));
}

constructor(public config: GalleryConfig = defaultConfig, public state: GalleryState = defaultState) {
this._state$ = new BehaviorSubject<GalleryState>(state);
constructor(public config: GalleryConfig = defaultConfig, private deleteInstance: Function) {
this._state$ = new BehaviorSubject<GalleryState>(this.state);
this._config$ = new BehaviorSubject<GalleryConfig>(defaultConfig);
this.setConfig(config);
}
Expand Down Expand Up @@ -221,6 +222,7 @@ export class GalleryRef {

/**
* Start gallery player
* @param interval
*/
play(interval?: number) {
if (interval) {
Expand All @@ -244,13 +246,14 @@ export class GalleryRef {
}

/**
* Destroy GalleryRef (for internal use only)
* Destroy gallery
*/
destroy() {
this._state$.complete();
this._config$.complete();
this.itemClick.complete();
this.thumbClick.complete();
this.deleteInstance();
}

}
35 changes: 22 additions & 13 deletions projects/core/src/lib/services/gallery.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GalleryConfig } from '../models';
@Injectable()
export class Gallery {

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

/** Global config */
Expand All @@ -18,7 +18,11 @@ export class Gallery {
this.config = {...defaultConfig, ...config};
}

/** Returns Gallery by ID */
/**
* Get or create gallery by ID
* @param id
* @param config
*/
ref(id = 'root', config?: GalleryConfig): GalleryRef {
if (this._instances.has(id)) {
const galleryRef = this._instances.get(id);
Expand All @@ -27,29 +31,34 @@ export class Gallery {
}
return galleryRef;
} else {
return this._instances.set(id, new GalleryRef({...this.config, ...config})).get(id);
return this._instances.set(id, new GalleryRef({...this.config, ...config}, this.deleteInstance(id))).get(id);
}
}

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

/** Destroy all gallery instances */
/**
* Destroy all gallery instances
*/
destroyAll() {
this._instances.forEach((ref: GalleryRef, id: string) => {
ref.destroy();
this._instances.delete(id);
});
}

/** Reset all gallery instances */
/**
* Reset all gallery instances
*/
resetAll() {
this._instances.forEach((ref: GalleryRef) => ref.reset());
}

/**
* A destroyer function for each gallery instance
*/
private deleteInstance(id: string) {
return () => {
this._instances.delete(id);
};
}

}

0 comments on commit 65f3358

Please sign in to comment.