-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventConsumer.controller.js
104 lines (84 loc) · 2.7 KB
/
eventConsumer.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
var angular = require('angular');
var Rx = require('rx');
var customStream = require('../../services/customStream/customStream.service');
var arraySource = require('../../services/arraySource/arraySource.service');
exports.moduleName = 'exp-ang.controllers.eventConsumer';
exports.controllerName = 'EventConsumerController';
EventConsumerController.$inject = ['$scope', customStream.serviceName, arraySource.serviceName];
function EventConsumerController($scope, customStream, arraySource) {
var self = this;
// --- public interface ---
// Event
self.hitCount = 0;
self.xPos = null;
self.finished = false;
self.eventError = null;
// Custom
self.customCurrentValue = null;
self.customFinished = false;
self.customError = null;
self.finishCustom = customStream.end;
self.generateCustomError = customStream.error;
// Array
self.arrayCurrentValue = null;
self.arrayFinished = false;
self.arrayError = null;
// --- initialize ---
consumeEventStream();
consumeCustomStream();
consumeArrayStream();
function consumeEventStream() {
var oldSubscription;
self.hitCount = 0;
$scope.$watch('eventStream', function(newValue) {
if (oldSubscription) {
oldSubscription.dispose();
}
var xCoordStream = newValue.map(function(event) {
return {
position: event.offsetX,
};
}).sample(1000 /*ms*/);
oldSubscription = xCoordStream.subscribe(function(data) {
$scope.$apply(function() { self.hitCount++; self.xPos = data.position; });
}, function(error) {
$scope.$apply(function() { self.eventError = error; });
}, function() {
$scope.$apply(function() { self.finished = true; });
});
});
}
function consumeCustomStream() {
var customSourceStream = customStream.get();
customSourceStream.subscribe(function(value) {
$scope.$apply(function() {
self.currentCustomValue = value;
});
}, function(error) {
self.customError = error;
}, function() {
self.customFinished = true;
});
}
function consumeArrayStream() {
// Use controlled() to cause stream to only supply values on request
var controlledArraySource = arraySource.get().controlled();
var pullData;
// Display each value from the stream
controlledArraySource.subscribe(function(value) {
self.arrayCurrentValue = value;
}, function(error) {
self.arrayError = error;
}, function() {
self.arrayFinished = true;
pullData.dispose();
});
// Request values at regular intervals
pullData = Rx.Observable.interval(1000).subscribe(function() {
$scope.$apply(function() { controlledArraySource.request(1); });
});
}
}
angular.module(exports.moduleName, [customStream.moduleName, arraySource.moduleName])
.controller(exports.controllerName, EventConsumerController);