js/eventsource.js   A
last analyzed

Complexity

Total Complexity 18
Complexity/F 2.57

Size

Lines of Code 85
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 18
mnd 11
bc 11
fnc 7
bpm 1.5713
cpm 2.5714
noi 0
1
/**
2
 * Nextcloud - Gallery
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Robin Appelman <[email protected]>
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Robin Appelman 2017
11
 * @copyright Olivier Paroz 2017
12
 */
13
14
/**
15
 * Wrapper for server side events
16
 * (http://en.wikipedia.org/wiki/Server-sent_events)
17
 */
18
19
/* global EventSource, oc_requesttoken, Gallery */
20
(function (oc_requesttoken) {
21
	"use strict";
22
	/**
23
	 * Create a new event source
24
	 *
25
	 * Comes from core and is thus not modified too much in order to be able to easily backport
26
	 * changes in core
27
	 *
28
	 * @param {string} src
29
	 * @param {object} [data] to be send as GET
30
	 * @constructor
31
	 */
32
	var CustomEventSource = function (src, data) {
33
		var dataStr = '';
34
		var joinChar;
35
		this.typelessListeners = [];
36
		if (data) {
37
			for (var i = 0, keys = Object.keys(data); i < keys.length; i++) {
38
				dataStr += keys[i] + '=' + encodeURIComponent(data[keys[i]]) + '&';
39
			}
40
		}
41
		/* jshint camelcase: false */
42
		dataStr += 'requesttoken=' + encodeURIComponent(oc_requesttoken);
43
		if (typeof EventSource !== 'undefined') {
44
			joinChar = '&';
45
			if (src.indexOf('?') === -1) {
46
				joinChar = '?';
47
			}
48
			var options = {};
49
			if (EventSource.isPolyfill !== undefined) {
50
				// 10 thumbnails * 200k per thumbnail
51
				options.bufferSizeLimit = 10 * 200 * 1024;
52
				//options.loggingEnabled = true;
53
			}
54
			this.source = new EventSource(src + joinChar + dataStr, options);
55
			this.source.onmessage = function (e) {
56
				for (var i = 0; i < this.typelessListeners.length; i++) {
57
					this.typelessListeners[i](JSON.parse(e.data));
58
				}
59
			}.bind(this);
60
			//add close listener
61
			this.listen('__internal__', function (data) {
62
				if (data === 'close') {
63
					this.close();
64
				}
65
			}.bind(this));
66
		}
67
	};
68
69
	CustomEventSource.prototype = {
70
		typelessListeners: [],
71
72
		/**
73
		 * Listen to a given type of events.
74
		 *
75
		 * @param {String} type event type
76
		 * @param {Function} callback event callback
77
		 */
78
		listen: function (type, callback) {
79
			if (callback && callback.call) {
80
				if (type) {
81
					this.source.addEventListener(type, function (e) {
82
						if (typeof e.data !== 'undefined') {
83
							callback(JSON.parse(e.data));
84
						} else {
85
							callback('');
86
						}
87
					}, false);
88
				} else {
89
					this.typelessListeners.push(callback);
90
				}
91
			}
92
		},
93
		/**
94
		 * Closes this event source.
95
		 */
96
		close: function () {
97
			if (typeof this.source !== 'undefined') {
98
				this.source.close();
99
			}
100
		}
101
	};
102
103
	Gallery.EventSource = CustomEventSource;
104
})(oc_requesttoken);
105