Skip to content

Commit

Permalink
Fix #357: Reload preview when files are deleted and renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Jun 22, 2015
1 parent 2638fef commit 263d4f2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/bramble/BrambleEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ define(function (require, exports, module) {
exports.triggerSidebarExpanded = function() {
exports.trigger("bramble:sidebarChange", true);
};

// file delete and rename needs to get broadcast to live dev transport
exports.triggerFileRenamed = function(oldFilename, newFilename) {
exports.trigger("fileRenamed", oldFilename, newFilename);
};
exports.triggerFileRemoved = function(filename) {
exports.trigger("fileRemoved", filename);
};
});
18 changes: 17 additions & 1 deletion src/extensions/default/bramble/lib/PostMessageTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ define(function (require, exports, module) {
var EventDispatcher = brackets.getModule("utils/EventDispatcher"),
LiveDevMultiBrowser = brackets.getModule("LiveDevelopment/LiveDevMultiBrowser"),
BlobUtils = brackets.getModule("filesystem/impls/filer/BlobUtils"),
BrambleEvents = brackets.getModule("bramble/BrambleEvents"),
Path = brackets.getModule("filesystem/impls/filer/BracketsFiler").Path;

// The script that will be injected into the previewed HTML to handle the other side of the post message connection.
Expand Down Expand Up @@ -81,6 +82,10 @@ define(function (require, exports, module) {
*/
function start(){
window.addEventListener("message", _listener);

// Reload whenever files are removed or renamed
BrambleEvents.on("fileRemoved", reload);
BrambleEvents.on("fileRenamed", reload);
}

/**
Expand Down Expand Up @@ -163,12 +168,23 @@ define(function (require, exports, module) {
"<script>\n" + XHRShim + "</script>\n";
}

// URL of document being rewritten/launched (if any)
var _pendingReloadUrl;

function reload() {
var launcher = Launcher.getCurrentInstance();
var liveDoc = LiveDevMultiBrowser._getCurrentLiveDoc();
var url = BlobUtils.getUrl(liveDoc.doc.file.fullPath);

launcher.launch(url);
// Don't start rewriting a URL if it's already in process (prevents infinite loop)
if(_pendingReloadUrl === url) {
return;
}

_pendingReloadUrl = url;
launcher.launch(url, function() {
_pendingReloadUrl = null;
});
}

// Exports
Expand Down
6 changes: 5 additions & 1 deletion src/extensions/default/bramble/lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ define(function (require, exports, module) {
_launcherInstance = this;
}

Launcher.prototype.launch = function(url) {
Launcher.prototype.launch = function(url, callback) {
var server = this.server;
var browser = this.browser;

Expand All @@ -35,6 +35,10 @@ define(function (require, exports, module) {
return;
}
browser.update(urlOrHTML);

if(typeof callback === "function") {
callback();
}
});
};

Expand Down
12 changes: 9 additions & 3 deletions src/filesystem/impls/filer/FilerFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ define(function (require, exports, module) {
Filer = require("filesystem/impls/filer/BracketsFiler"),
BlobUtils = require("filesystem/impls/filer/BlobUtils"),
decodePath = require("filesystem/impls/filer/FilerUtils").decodePath,
Handlers = require("filesystem/impls/filer/lib/handlers"),
Content = require("filesystem/impls/filer/lib/content"),
Async = require("utils/Async");
Handlers = require("filesystem/impls/filer/lib/handlers"),
Content = require("filesystem/impls/filer/lib/content"),
Async = require("utils/Async"),
BrambleEvents = require("bramble/BrambleEvents");

var fs = Filer.fs(),
Path = Filer.Path,
Expand Down Expand Up @@ -184,6 +185,7 @@ define(function (require, exports, module) {

if(stat.isFile) {
BlobUtils.rename(oldPath, newPath);
BrambleEvents.triggerFileRenamed(oldPath, newPath);
}

callback();
Expand Down Expand Up @@ -349,6 +351,10 @@ define(function (require, exports, module) {
// TODO: deal with the symlink case (i.e., only remove cache
// item if file is really going away).
BlobUtils.remove(path);

if(!err) {
BrambleEvents.triggerFileRemoved(path);
}
callback(_mapError(err));
});
});
Expand Down

0 comments on commit 263d4f2

Please sign in to comment.