szurubooru/client/js/main.js
rr- 2a4241641c client/events: improve event dispatching
This commit introduces timer-less retry system:

1. Any change to URL is going to stop listening to any messages.
2. If a message is sent and there's no handler that could pick it up,
   the message gets enqueued.
3. The message is sent again to the first handler that attaches itself
   to given event type.

While in theory this is full of holes (no control over the first
handler), in practice, it works quite well.

Additionally, views.listenToMessages was attaching to completely wrong
DOM node; this commit fixes this as well.
2016-05-11 23:47:50 +02:00

57 lines
1.7 KiB
JavaScript

'use strict';
require('./util/polyfill.js');
const page = require('page');
const origPushState = page.Context.prototype.pushState;
page.Context.prototype.pushState = function() {
window.scrollTo(0, 0);
origPushState.call(this);
};
const mousetrap = require('mousetrap');
page(/.*/, (ctx, next) => {
mousetrap.reset();
next();
});
let controllers = [];
controllers.push(require('./controllers/auth_controller.js'));
controllers.push(require('./controllers/posts_controller.js'));
controllers.push(require('./controllers/users_controller.js'));
controllers.push(require('./controllers/help_controller.js'));
controllers.push(require('./controllers/comments_controller.js'));
controllers.push(require('./controllers/history_controller.js'));
controllers.push(require('./controllers/tags_controller.js'));
controllers.push(require('./controllers/settings_controller.js'));
controllers.push(require('./controllers/home_controller.js'));
const tags = require('./tags.js');
const events = require('./events.js');
const views = require('./util/views.js');
for (let controller of controllers) {
controller.registerRoutes();
}
page.exit((ctx, next) => {
views.unlistenToMessages();
next();
});
const api = require('./api.js');
Promise.all([tags.refreshExport(), api.loginFromCookies()])
.then(() => {
page();
}).catch(errorMessage => {
if (window.location.href.indexOf('login') !== -1) {
api.forget();
page();
} else {
page('/');
events.notify(
events.Error,
'An error happened while trying to log you in: ' +
errorMessage);
}
});