Provide a way to prevent closing Webamp

Fixes #653
This commit is contained in:
Jordan Eldredge 2018-09-11 07:24:34 -07:00
parent ff910ba97e
commit 61caa4f6f9
7 changed files with 60 additions and 4 deletions

View file

@ -216,6 +216,21 @@ const unsubscribe = webamp.onTrackDidChange((track => {
unsubscribe();
```
### `onWillClose(callback)`
A callback which will be called when Webamp is _about to_ closed. Returns an "unsubscribe" function. The callback will be passed a `cancel` function which you can use to conditionally prevent Webamp from being closed.
```JavaScript
const unsubscribe = webamp.onClose((cancel) => {
if (!window.confirm("Are you sure you want to close Webamp?")) {
cancel();
}
});
// If at some point in the future you want to stop listening to these events:
unsubscribe();
```
### `onClose(callback)`
A callback which will be called when Webamp is closed. Returns an "unsubscribe" function.

9
index.d.ts vendored
View file

@ -160,6 +160,15 @@ export default class Webamp {
*/
public onTrackDidChange(callback: (track: Track) => any): () => void;
/**
* A callback which will be called when Webamp is _about to_ closed. Returns an
* "unsubscribe" function. The callback will be passed a `cancel` function
* which you can use to conditionally prevent Webamp from being closed.
*
* @returns An "unsubscribe" function. Useful if at some point in the future you want to stop listening to these events.
*/
public onWillClose(callback: (cancel: () => void) => any): () => void;
/**
* A callback which will be called when Webamp is closed.
*

View file

@ -1,4 +1,9 @@
import { CLOSE_WINAMP, STOP, TOGGLE_VISUALIZER_STYLE } from "../actionTypes";
import {
CLOSE_WINAMP,
STOP,
TOGGLE_VISUALIZER_STYLE,
CLOSE_REQUESTED
} from "../actionTypes";
import { Dispatchable } from "../types";
export {
@ -71,8 +76,17 @@ export {
export function close(): Dispatchable {
return dispatch => {
dispatch({ type: STOP });
dispatch({ type: CLOSE_WINAMP });
// TODO: This could probably be improved by adding a "PREVENT_CLOSE" action
// or something, but this works okay for now.
let defaultPrevented = false;
const cancel = () => {
defaultPrevented = true;
};
dispatch({ type: CLOSE_REQUESTED, cancel });
if (!defaultPrevented) {
dispatch({ type: STOP });
dispatch({ type: CLOSE_WINAMP });
}
};
}

View file

@ -70,3 +70,4 @@ export const DISABLE_MARQUEE = "DISABLE_MARQUEE";
export const SET_DUMMY_VIZ_DATA = "SET_DUMMY_VIZ_DATA";
export const SET_WINDOW_VISIBILITY = "SET_WINDOW_VISIBILITY";
export const LOADING = "LOADING";
export const CLOSE_REQUESTED = "CLOSE_REQUESTED";

View file

@ -262,6 +262,12 @@ Raven.context(() => {
});
}
webamp.onWillClose(cancel => {
if (!window.confirm("Are you sure you want to close Webamp?")) {
cancel();
}
});
webamp.renderWhenReady(document.getElementById("app"));
// Expose webamp instance for debugging and integration tests.

View file

@ -327,6 +327,10 @@ export type Action =
}
| {
type: "MINIMIZE_WINAMP";
}
| {
type: "CLOSE_REQUESTED";
cancel: () => void;
};
export interface SettingsState {

View file

@ -27,7 +27,8 @@ import {
LOADED,
REGISTER_VISUALIZER,
SET_Z_INDEX,
SET_MEDIA
SET_MEDIA,
CLOSE_REQUESTED
} from "./actionTypes";
import Emitter from "./emitter";
@ -182,6 +183,12 @@ class Winamp {
this.store.dispatch(loadMediaFiles(tracks, LOAD_STYLE.PLAY));
}
onWillClose(cb) {
return this._actionEmitter.on(CLOSE_REQUESTED, action => {
cb(action.cancel);
});
}
onClose(cb) {
return this._actionEmitter.on(CLOSE_WINAMP, cb);
}