forked from reTHINK-project/dev-hyperty-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloWorldObserver.hy.js
92 lines (59 loc) · 2.37 KB
/
HelloWorldObserver.hy.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
/* jshint undef: true */
import {Syncher} from 'service-framework/dist/Syncher';
import {divideURL} from '../utils/utils';
import EventEmitter from '../utils/EventEmitter';
/**
* Hello World Observer
* @author Paulo Chainho [[email protected]]
* @version 0.1.0
*/
class HelloWorldObserver extends EventEmitter {
/**
* Create a new HelloWorldObserver
* @param {Syncher} syncher - Syncher provided from the runtime core
*/
constructor(hypertyURL, bus, configuration) {
if (!hypertyURL) throw new Error('The hypertyURL is a needed parameter');
if (!bus) throw new Error('The MiniBus is a needed parameter');
if (!configuration) throw new Error('The configuration is a needed parameter');
super();
let _this = this;
_this._domain = divideURL(hypertyURL).domain;
_this._objectDescURL = 'hyperty-catalogue://' + _this._domain + '/.well-known/dataschemas/HelloWorldDataSchema';
let domain = divideURL(hypertyURL).domain;
let syncher = new Syncher(hypertyURL, bus, configuration);
syncher.onNotification(function(event) {
_this._onNotification(event);
});
_this._syncher = syncher;
}
_onNotification(event) {
let _this = this;
console.info( 'Event Received: ', event);
_this.trigger('invitation', event.identity);
// Acknowledge reporter about the Invitation was received
event.ack();
// Subscribe Hello World Object
_this._syncher.subscribe(_this._objectDescURL, event.url).then(function(helloObjtObserver) {
// Hello World Object was subscribed
console.info( helloObjtObserver);
// lets notify the App the subscription was accepted with the mnost updated version of Hello World Object
_this.trigger('hello', helloObjtObserver.data);
// lets now observe any changes done in Hello World Object
helloObjtObserver.onChange('*', function(event) {
// Hello World Object was changed
console.info('message received:',event);
// lets notify the App about the change
_this.trigger('hello', helloObjtObserver.data);
});
}).catch(function(reason) {
console.error(reason);
});
}
}
export default function activate(hypertyURL, bus, configuration) {
return {
name: 'HelloWorldObserver',
instance: new HelloWorldObserver(hypertyURL, bus, configuration)
};
}