Skip to content

Commit

Permalink
Merge pull request #221 from lona-web-org/client-refactoring
Browse files Browse the repository at this point in the history
Client refactoring
  • Loading branch information
fscherf authored May 22, 2022
2 parents df1ea10 + 506a8a0 commit d5fddd3
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 364 deletions.
138 changes: 73 additions & 65 deletions lona/client/_lona/context.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
Lona.LonaContext = function(settings) {
// settings ---------------------------------------------------------------
this.settings = settings || {};
this.settings.target = this.settings.target || '#lona';
this.settings.title = this.settings.title || '';

if(typeof(this.settings.update_address_bar) == 'undefined') {
this.settings.update_address_bar = true;
};
Lona.LonaContext = class LonaContext {
constructor(settings) {
this.settings = settings || {};
this.settings.target = this.settings.target || '#lona';
this.settings.title = this.settings.title || '';

if(typeof(this.settings.update_address_bar) == 'undefined') {
this.settings.update_address_bar = true;
};

if(typeof(this.settings.update_title) == 'undefined') {
this.settings.update_title = true;
};
if(typeof(this.settings.update_title) == 'undefined') {
this.settings.update_title = true;
};

if(typeof(this.settings.follow_redirects) == 'undefined') {
this.settings.follow_redirects = true;
};
if(typeof(this.settings.follow_redirects) == 'undefined') {
this.settings.follow_redirects = true;
};

if(typeof(this.settings.follow_http_redirects) == 'undefined') {
this.settings.follow_http_redirects = true;
};
if(typeof(this.settings.follow_http_redirects) == 'undefined') {
this.settings.follow_http_redirects = true;
};

if(typeof(this.settings.scroll_to_top_on_view_start) == 'undefined') {
this.settings.scroll_to_top_on_view_start = true;
};
if(typeof(this.settings.scroll_to_top_on_view_start) == 'undefined') {
this.settings.scroll_to_top_on_view_start = true;
};

// state ------------------------------------------------------------------
this._windows = {};
this._connect_hooks = [];
this._disconnect_hooks = [];
this._rendering_hooks = [];
this._view_timeout_hooks = [];
this._input_event_timeout_hooks = [];
this._message_handler = [];
this._windows = {};
this._last_window_id = 0;
this._connect_hooks = [];
this._disconnect_hooks = [];
this._rendering_hooks = [];
this._view_timeout_hooks = [];
this._input_event_timeout_hooks = [];
this._message_handler = [];
};

// window -----------------------------------------------------------------
this.create_window = function(root, url) {
create_window(root, url) {
if(typeof(root) == 'string') {
root = document.querySelector(root);
};

var window_id = Object.keys(this._windows).length + 1;
this._last_window_id += 1;

var window_id = this._last_window_id;

this._windows[window_id] = new Lona.LonaWindow(
this, root, window_id, url);
Expand All @@ -50,9 +53,7 @@ Lona.LonaContext = function(settings) {
};

// input events -----------------------------------------------------------
this.patch_input_events = function(root_node_selector, window_id) {
var _this = this;

patch_input_events(root_node_selector, window_id) {
// find window
if(window_id == undefined) {
window_id = 1;
Expand All @@ -64,53 +65,53 @@ Lona.LonaContext = function(settings) {
var node = document.querySelector(root_node_selector);
var selector = 'a,form,[data-lona-events]';

node.querySelectorAll(selector).forEach(function(node) {
node.querySelectorAll(selector).forEach(node => {
_window._input_event_handler.patch_input_events(node);
});
};

// hooks ------------------------------------------------------------------
this.add_connect_hook = function(hook) {
add_connect_hook(hook) {
this._connect_hooks.push(hook);
};

this.add_disconnect_hook = function(hook) {
add_disconnect_hook(hook) {
this._disconnect_hooks.push(hook);
};

this.add_rendering_hook = function(hook) {
add_rendering_hook(hook) {
this._rendering_hooks.push(hook);
};

this.add_view_timeout_hook = function(hook) {
add_view_timeout_hook(hook) {
this._view_timeout_hooks.push(hook);
};

this.add_input_event_timeout_hook = function(hook) {
add_input_event_timeout_hook(hook) {
this._input_event_timeout_hooks.push(hook);
};

this.add_message_handler = function(handler) {
add_message_handler(handler) {
this._message_handler.push(handler);
};

this._run_connect_hooks = function(event) {
_run_connect_hooks(event) {
for(var i in this._connect_hooks) {
var hook = this._connect_hooks[i];

hook(this, event);
};
};

this._run_disconnect_hooks = function(event) {
_run_disconnect_hooks(event) {
for(var i in this._disconnect_hooks) {
var hook = this._disconnect_hooks[i];

hook(this, event);
};
};

this._run_rendering_hooks = function(lona_window) {
_run_rendering_hooks(lona_window) {
try {
for(var i in this._rendering_hooks) {
var hook = this._rendering_hooks[i];
Expand All @@ -124,7 +125,7 @@ Lona.LonaContext = function(settings) {
};
};

this._run_view_timeout_hooks = function(lona_window) {
_run_view_timeout_hooks(lona_window) {
var lona_window_shim = new Lona.LonaWindowShim(this, lona_window);

try {
Expand All @@ -140,7 +141,7 @@ Lona.LonaContext = function(settings) {
};
};

this._run_input_event_timeout_hooks = function(lona_window) {
_run_input_event_timeout_hooks(lona_window) {
var lona_window_shim = new Lona.LonaWindowShim(this, lona_window);

try {
Expand All @@ -156,7 +157,7 @@ Lona.LonaContext = function(settings) {
};
};

this._run_message_handler = function(message) {
_run_message_handler(message) {
for(var i in this._message_handler) {
var message_handler = this._message_handler[i];

Expand All @@ -169,7 +170,7 @@ Lona.LonaContext = function(settings) {
};

// websocket messages -----------------------------------------------------
this.send = function(message) {
send(message) {
if(typeof(message) != 'string') {
message = JSON.stringify(message);
};
Expand All @@ -179,7 +180,7 @@ Lona.LonaContext = function(settings) {
this._ws.send(message);
};

this._handle_raw_websocket_message = function(event) {
_handle_raw_websocket_message(event) {
var raw_message = event.data;
var json_data = undefined;

Expand Down Expand Up @@ -226,7 +227,7 @@ Lona.LonaContext = function(settings) {
};

// pings ------------------------------------------------------------------
this.send_ping = function() {
send_ping() {
var raw_message = [
undefined, // window_id
undefined, // view_runtime_id
Expand All @@ -242,24 +243,22 @@ Lona.LonaContext = function(settings) {
this.send(message);
};

this._send_pings = function() {
var _this = this;

_send_pings() {
setTimeout(
function() {
if(_this._ws.readyState != _this._ws.OPEN) {
() => {
if(this._ws.readyState != this._ws.OPEN) {
return;
};

_this.send_ping();
_this._send_pings();
this.send_ping();
this._send_pings();
},
Lona.settings.PING_INTERVAL * 1000,
);
};

// setup ------------------------------------------------------------------
this.reconnect = function() {
reconnect() {
// state
this._windows = {};

Expand Down Expand Up @@ -310,17 +309,26 @@ Lona.LonaContext = function(settings) {
};
};

this.setup = function() {
var _this = this;

_this.reconnect();
setup() {
this.reconnect();

// unset websocket.onclose handler when page gets unloaded to
// prevent the browser from showing "Server disconnected" when the
// user changes the browser URL
window.addEventListener('beforeunload', function(event) {
_this._ws.onclose = function(event) {};
_this._ws.close();
window.addEventListener('beforeunload', event => {
this._ws.onclose = event => {};
this._ws.close();
});
};

// shortcuts --------------------------------------------------------------
get_default_window() {
return this._windows[1];
};

run_view(url, post_data) {
var window = this.get_default_window();

return window.run_view(url, post_data);
};
};
Loading

0 comments on commit d5fddd3

Please sign in to comment.