Switch to using location-history package

https://npmjs.com/package/location-history
This commit is contained in:
Feross Aboukhadijeh
2016-07-28 20:07:12 -07:00
parent 43a81f725f
commit 536f04985f
6 changed files with 15 additions and 139 deletions

View File

@@ -27,6 +27,7 @@
"hat": "0.0.3",
"iso-639-1": "^1.2.1",
"languagedetect": "^1.1.1",
"location-history": "^1.0.0",
"musicmetadata": "^2.0.2",
"network-address": "^1.1.0",
"parse-torrent": "^5.7.3",

View File

@@ -28,11 +28,11 @@ module.exports = class PlaybackController {
playFile (infoHash, index /* optional */) {
this.state.location.go({
url: 'player',
onbeforeload: (cb) => {
setup: (cb) => {
this.play()
this.openPlayer(infoHash, index, cb)
},
onbeforeunload: (cb) => this.closePlayer(cb)
destroy: () => this.closePlayer()
}, (err) => {
if (err) dispatch('error', err)
})
@@ -251,7 +251,7 @@ module.exports = class PlaybackController {
})
}
closePlayer (cb) {
closePlayer () {
console.log('closePlayer')
// Quit any external players, like Chromecast/Airplay/etc or VLC
@@ -290,7 +290,6 @@ module.exports = class PlaybackController {
ipcRenderer.send('onPlayerClose')
this.update()
cb()
}
}

View File

@@ -13,24 +13,19 @@ module.exports = class PrefsController {
var state = this.state
state.location.go({
url: 'preferences',
onbeforeload: function (cb) {
setup: function (cb) {
// initialize preferences
dispatch('setTitle', 'Preferences')
state.unsaved = Object.assign(state.unsaved || {}, {prefs: state.saved.prefs || {}})
cb()
},
onbeforeunload: (cb) => {
// save state after preferences
this.save()
dispatch('resetTitle')
cb()
}
destroy: () => this.save()
})
}
// Updates a single property in the UNSAVED prefs
// For example: updatePreferences('foo.bar', 'baz')
// Call savePreferences to save to config.json
// Call save() to save to config.json
update (property, value) {
var path = property.split('.')
var key = this.state.unsaved.prefs

View File

@@ -1,125 +0,0 @@
module.exports = LocationHistory
function LocationHistory () {
this._history = []
this._forward = []
this._pending = false
}
LocationHistory.prototype.url = function () {
return this.current() && this.current().url
}
LocationHistory.prototype.current = function () {
return this._history[this._history.length - 1]
}
LocationHistory.prototype.go = function (page, cb) {
if (!cb) cb = noop
if (this._pending) return cb(null)
console.log('go', page)
this.clearForward()
this._go(page, cb)
}
LocationHistory.prototype.back = function (cb) {
var self = this
if (!cb) cb = noop
if (self._history.length <= 1 || self._pending) return cb(null)
var page = self._history.pop()
self._unload(page, done)
function done (err) {
if (err) return cb(err)
self._forward.push(page)
self._load(self.current(), cb)
}
}
LocationHistory.prototype.hasBack = function () {
return this._history.length > 1
}
LocationHistory.prototype.forward = function (cb) {
if (!cb) cb = noop
if (this._forward.length === 0 || this._pending) return cb(null)
var page = this._forward.pop()
this._go(page, cb)
}
LocationHistory.prototype.hasForward = function () {
return this._forward.length > 0
}
LocationHistory.prototype.clearForward = function (url) {
if (url == null) {
this._forward = []
} else {
console.log(this._forward)
console.log(url)
this._forward = this._forward.filter(function (page) {
return page.url !== url
})
}
}
LocationHistory.prototype.backToFirst = function (cb) {
var self = this
if (!cb) cb = noop
if (self._history.length <= 1) return cb(null)
self.back(function (err) {
if (err) return cb(err)
self.backToFirst(cb)
})
}
LocationHistory.prototype._go = function (page, cb) {
var self = this
if (!cb) cb = noop
self._unload(self.current(), done1)
function done1 (err) {
if (err) return cb(err)
self._load(page, done2)
}
function done2 (err) {
if (err) return cb(err)
self._history.push(page)
cb(null)
}
}
LocationHistory.prototype._load = function (page, cb) {
var self = this
self._pending = true
if (page && page.onbeforeload) page.onbeforeload(done)
else done(null)
function done (err) {
self._pending = false
cb(err)
}
}
LocationHistory.prototype._unload = function (page, cb) {
var self = this
self._pending = true
if (page && page.onbeforeunload) page.onbeforeunload(done)
else done(null)
function done (err) {
self._pending = false
cb(err)
}
}
function noop () {}

View File

@@ -15,7 +15,7 @@ var State = module.exports = Object.assign(new EventEmitter(), {
appConfig.filePath = path.join(config.CONFIG_PATH, 'config.json')
function getDefaultState () {
var LocationHistory = require('./location-history')
var LocationHistory = require('location-history')
return {
/*

View File

@@ -66,7 +66,13 @@ function onState (err, _state) {
}
// Add first page to location history
state.location.go({ url: 'home' })
state.location.go({
url: 'home',
setup: (cb) => {
dispatch('resetTitle')
cb(null)
}
})
// Restart everything we were torrenting last time the app ran
resumeTorrents()