Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Molving to preprocessify instead of remove lines since it's more vers… #37

Merged
merged 2 commits into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dist/sww.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ SimpleOfflineCache.prototype.onFetch = function soc_onFetch(request, response) {

var clone = request.clone();
var _this = this;
debug('Handing fetch event: ' + clone.url);
return this.ensureCache().then(function(cache) {
return cache.match(clone, _this.options).then(function(res) {
if (res) {
Expand All @@ -239,6 +240,7 @@ SimpleOfflineCache.prototype.ensureCache = function soc_ensureCache() {
return this.cacheRequest;
};


module.exports = SimpleOfflineCache;

},{}],4:[function(require,module,exports){
Expand Down Expand Up @@ -303,6 +305,7 @@ module.exports = StaticCacher;
'use strict';

var debug = 1 ? console.log.bind(console, '[ServiceWorkerWare]') : function(){};

var StaticCacher = require('./staticcacher.js');
var SimpleOfflineCache = require('./simpleofflinecache.js');
var Router = require('./router.js');
Expand Down Expand Up @@ -349,6 +352,8 @@ ServiceWorkerWare.prototype.init = function sww_init() {
* Handle and forward all events related to SW
*/
ServiceWorkerWare.prototype.handleEvent = function sww_handleEvent(evt) {

debug('Event received: ' + evt.type);
switch(evt.type) {
case 'install':
this.onInstall(evt);
Expand All @@ -365,6 +370,7 @@ ServiceWorkerWare.prototype.handleEvent = function sww_handleEvent(evt) {
this.forwardEvent(evt);
break;
default:
debug('Unhandled event ' + evt.type);
}
};

Expand All @@ -382,6 +388,7 @@ ServiceWorkerWare.prototype.onFetch = function sww_onFetch(evt) {
}).bind(this));

evt.respondWith(this.executeMiddleware(steps, evt.request));

};

/**
Expand Down Expand Up @@ -532,6 +539,7 @@ ServiceWorkerWare.normalizeMwAnswer = function (answer, request, response) {
ServiceWorkerWare.prototype.onInstall = function sww_oninstall(evt) {
var installation = this.getFromMiddleware('onInstall');
evt.waitUntil(installation);

};

/**
Expand All @@ -545,6 +553,7 @@ ServiceWorkerWare.prototype.onActivate = function sww_activate(evt) {
activation.then(function claim() { return self.clients.claim(); });
}
evt.waitUntil(activation);

};

/**
Expand Down Expand Up @@ -685,6 +694,7 @@ ServiceWorkerWare.decorators = {
}
};


module.exports = {
ServiceWorkerWare: ServiceWorkerWare,
StaticCacher: StaticCacher,
Expand Down
11 changes: 5 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var webserver = require('gulp-webserver');
var jshint = require('gulp-jshint');
var watch = require('gulp-watch');
var karma = require('karma').server;
var removeLines = require('gulp-remove-lines');
var preprocessify = require('preprocessify');

var getBundleName = function () {
return 'sww';
Expand All @@ -31,14 +31,10 @@ gulp.task('bundle-dist', function() {

var bundle = function() {
return bundler
.transform(preprocessify())
.bundle()
.pipe(source(getBundleName() + '.js'))
.pipe(buffer())
.pipe(removeLines({'filters': [
/performance\.mark/,
/performance\.measure/,
/debug\(/
]}))
.pipe(gulp.dest('./dist/'));
};

Expand All @@ -55,6 +51,9 @@ gulp.task('bundle-debug', function() {

var bundle = function() {
return bundler
.transform(preprocessify(
{ DEBUG: true }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about having PERF and DEBUG so in production we can have both disabled and remove performance marks as well as debug traces? If you like, open a new issue with the change.

Check the comments but you have my r+

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I was thinking on it, creating a debug one for the debug messages and the one for perf.

We can add it in a different patch, specially if your proposition on the preprocessing repo is accepted, will be pretty easy.

))
.bundle()
.pipe(source(getBundleName() + '.js'))
.pipe(buffer())
Expand Down
23 changes: 20 additions & 3 deletions lib/simpleofflinecache.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ function SimpleOfflineCache(cacheName, options, missPolicy) {
SimpleOfflineCache.prototype.onFetch = function soc_onFetch(request, response) {
// If another middleware layer already have a response, the simple cache
// just pass through the response and does nothing.
performance.mark('soc_' + request.url);
// @ifdef DEBUG
performanceMark('soc_' + request.url);
// @endif
if (response) {
return Promise.resolve(response);
}
Expand All @@ -50,9 +52,13 @@ SimpleOfflineCache.prototype.onFetch = function soc_onFetch(request, response) {
debug('Handing fetch event: ' + clone.url);
return this.ensureCache().then(function(cache) {
return cache.match(clone, _this.options).then(function(res) {
performance.mark('soc_cache_' + clone.url);
// @ifdef DEBUG
performanceMark('soc_cache_' + clone.url);
// @endif
if (res) {
performance.mark('soc_cache_hit_' + clone.url);
// @ifdef DEBUG
performanceMark('soc_cache_hit_' + clone.url);
// @endif
return res;
}

Expand All @@ -72,4 +78,15 @@ SimpleOfflineCache.prototype.ensureCache = function soc_ensureCache() {
return this.cacheRequest;
};

// @ifdef DEBUG
// Used in debugging, to save performance marks.
// Remember than in Firefox we have the performance API in workers
// but we don't have it in Chrome.
function performanceMark(name) {
if (performance && performance.mark) {
performance.mark(name);
}
}
// @endif

module.exports = SimpleOfflineCache;
36 changes: 31 additions & 5 deletions lib/sww.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
'use strict';

var debug = 1 ? console.log.bind(console, '[ServiceWorkerWare]') : function(){};
performance.mark('sww_parsed');
// @ifdef DEBUG
performanceMark('sww_parsed');
// @endif

var StaticCacher = require('./staticcacher.js');
var SimpleOfflineCache = require('./simpleofflinecache.js');
var Router = require('./router.js');
Expand Down Expand Up @@ -49,7 +52,10 @@ ServiceWorkerWare.prototype.init = function sww_init() {
* Handle and forward all events related to SW
*/
ServiceWorkerWare.prototype.handleEvent = function sww_handleEvent(evt) {
performance.mark('event_' + evt.type + '_start');
// @ifdef DEBUG
performanceMark('event_' + evt.type + '_start');
// @endif

debug('Event received: ' + evt.type);
switch(evt.type) {
case 'install':
Expand Down Expand Up @@ -85,7 +91,10 @@ ServiceWorkerWare.prototype.onFetch = function sww_onFetch(evt) {
}).bind(this));

evt.respondWith(this.executeMiddleware(steps, evt.request));
performance.mark('event_fetch_end');

// @ifdef DEBUG
performanceMark('event_fetch_end');
// @endif
};

/**
Expand Down Expand Up @@ -236,7 +245,10 @@ ServiceWorkerWare.normalizeMwAnswer = function (answer, request, response) {
ServiceWorkerWare.prototype.onInstall = function sww_oninstall(evt) {
var installation = this.getFromMiddleware('onInstall');
evt.waitUntil(installation);
performance.mark('event_install_end');

// @ifdef DEBUG
performanceMark('event_install_end');
// @endif
};

/**
Expand All @@ -250,7 +262,10 @@ ServiceWorkerWare.prototype.onActivate = function sww_activate(evt) {
activation.then(function claim() { return self.clients.claim(); });
}
evt.waitUntil(activation);
performance.mark('event_activate_end');

// @ifdef DEBUG
performanceMark('event_activate_end');
// @endif
};

/**
Expand Down Expand Up @@ -391,6 +406,17 @@ ServiceWorkerWare.decorators = {
}
};

// @ifdef DEBUG
// Used in debugging, to save performance marks.
// Remember than in Firefox we have the performance API in workers
// but we don't have it in Chrome.
function performanceMark(name) {
if (performance && performance.mark) {
performance.mark(name);
}
}
// @endif

module.exports = {
ServiceWorkerWare: ServiceWorkerWare,
StaticCacher: StaticCacher,
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"chai": "^2.2.0",
"gulp": "^3.8.11",
"gulp-jshint": "^1.9.2",
"gulp-remove-lines": "^0.1.0",
"gulp-sourcemaps": "^1.3.0",
"gulp-watch": "^4.1.1",
"gulp-webserver": "^0.9.0",
Expand All @@ -25,9 +24,9 @@
"karma-sinon": "^1.0.4",
"karma-sw-mocha": "git://github.com/lodr/karma-sw-mocha.git#master",
"mocha": "git://github.com/lodr/mocha.git#sw",
"preprocessify": "0.0.6",
"sinon": "^1.14.1",
"vinyl-buffer": "^1.0.0",
"karma-firefox-launcher": "^0.1.4",
"vinyl-source-stream": "^1.0.0"
}
}