2014-08-15 11:20:14 +02:00
|
|
|
/*
|
|
|
|
Copyright 2014 matrix.org
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-08-15 12:31:13 +02:00
|
|
|
/*
|
|
|
|
This service manages where in the event stream the web client currently is and
|
|
|
|
provides methods to resume/pause/stop the event stream. This service is not
|
|
|
|
responsible for parsing event data. For that, see the eventDataHandler.
|
|
|
|
*/
|
2014-08-15 11:20:14 +02:00
|
|
|
angular.module('eventStreamService', [])
|
|
|
|
.factory('eventStreamService', ['matrixService', function(matrixService) {
|
2014-08-15 12:31:13 +02:00
|
|
|
var END = "END";
|
|
|
|
var START = "START";
|
|
|
|
var TIMEOUT_MS = 5000;
|
|
|
|
|
2014-08-15 11:20:14 +02:00
|
|
|
var settings = {
|
|
|
|
from: "END",
|
|
|
|
to: undefined,
|
|
|
|
limit: undefined,
|
|
|
|
shouldPoll: true
|
|
|
|
};
|
|
|
|
|
|
|
|
// interrupts the stream. Only valid if there is a stream conneciton
|
|
|
|
// open.
|
|
|
|
var interrupt = function(shouldPoll) {
|
2014-08-15 12:31:13 +02:00
|
|
|
console.log("p[EventStream] interrupt("+shouldPoll+") "+
|
2014-08-15 11:20:14 +02:00
|
|
|
JSON.stringify(settings));
|
|
|
|
};
|
|
|
|
|
|
|
|
var saveStreamSettings = function() {
|
|
|
|
localStorage.setItem("streamSettings", JSON.stringify(settings));
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
// resume the stream from whereever it last got up to. Typically used
|
|
|
|
// when the page is opened.
|
|
|
|
resume: function() {
|
|
|
|
console.log("[EventStream] resume "+JSON.stringify(settings));
|
|
|
|
// run the stream from the latest token
|
2014-08-15 12:31:13 +02:00
|
|
|
return matrixService.getEventStream(settings.from, TIMEOUT_MS);
|
2014-08-15 11:20:14 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// pause the stream. Resuming it will continue from the current position
|
|
|
|
pause: function() {
|
|
|
|
console.log("[EventStream] pause "+JSON.stringify(settings));
|
|
|
|
// kill any running stream
|
|
|
|
interrupt(false);
|
|
|
|
// save the latest token
|
|
|
|
saveStreamSettings();
|
|
|
|
},
|
|
|
|
|
|
|
|
// stop the stream and wipe the position in the stream. Typically used
|
2014-08-15 12:31:13 +02:00
|
|
|
// when logging out / logged out.
|
2014-08-15 11:20:14 +02:00
|
|
|
stop: function() {
|
|
|
|
console.log("[EventStream] stop "+JSON.stringify(settings));
|
|
|
|
// kill any running stream
|
|
|
|
interrupt(false);
|
|
|
|
// clear the latest token
|
2014-08-15 12:31:13 +02:00
|
|
|
settings.from = END;
|
2014-08-15 11:20:14 +02:00
|
|
|
saveStreamSettings();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}]);
|