1 | <!DOCTYPE html>
|
2 | <html lang="en">
|
3 | <head>
|
4 | <meta charset="utf-8">
|
5 | <title>JSDoc: Source: lib/model/Action.js</title>
|
6 |
|
7 | <script src="scripts/prettify/prettify.js"> </script>
|
8 | <script src="scripts/prettify/lang-css.js"> </script>
|
9 | |
10 |
|
11 |
|
12 | <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
13 | <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
14 | </head>
|
15 |
|
16 | <body>
|
17 |
|
18 | <div id="main">
|
19 |
|
20 | <h1 class="page-title">Source: lib/model/Action.js</h1>
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | <section>
|
28 | <article>
|
29 | <pre class="prettyprint source linenums"><code>var client = require('../client');
|
30 | var util = require('../util');
|
31 |
|
32 | /**
|
33 | * An action to be control execution on a remote object
|
34 | *
|
35 | * @constructor
|
36 | * @augments Container
|
37 | * @param {Object} data Action data
|
38 | * @param {ServiceObject} object reference to owner object
|
39 | */
|
40 | var Action = function (data, serviceObject) {
|
41 |
|
42 | if(!serviceObject) {
|
43 | throw new Error("serviceObject parameter is required");
|
44 | }
|
45 |
|
46 | this.setServiceObject(serviceObject);
|
47 | this.client = this.getContainer().client;
|
48 |
|
49 | this.data = {
|
50 | name: null,
|
51 | description: null,
|
52 | status: {}
|
53 | };
|
54 |
|
55 | this.exportProperties();
|
56 | this.parseJSON(data);
|
57 | };
|
58 | util.extends(Action, 'Container');
|
59 |
|
60 | /**
|
61 | * @inheritdoc
|
62 | */
|
63 | Action.prototype.validate = function () {
|
64 |
|
65 | if(!this.data.name) {
|
66 | throw new Error("Action name is required");
|
67 | }
|
68 |
|
69 | };
|
70 |
|
71 | /**
|
72 | * @inheritdoc
|
73 | */
|
74 | Action.prototype.toJSON = function () {
|
75 | var json = this.__super__.toJSON.call(this);
|
76 | if(!this.description && !Object.keys(this.status).length) {
|
77 | return this.name;
|
78 | }
|
79 | return json;
|
80 | };
|
81 |
|
82 | /**
|
83 | * @inheritdoc
|
84 | */
|
85 | Action.prototype.parseJSON = function (data) {
|
86 |
|
87 | if(typeof data === 'string') {
|
88 | data = {
|
89 | name: data
|
90 | };
|
91 | }
|
92 |
|
93 | this.__super__.parseJSON.call(this, data);
|
94 | this.validate();
|
95 | };
|
96 |
|
97 | /**
|
98 | * Invoke the ServiceObject action
|
99 | *
|
100 | * @param {String} body The body of the request as STRING
|
101 | * @return {Promise} Promise callback with result
|
102 | */
|
103 | Action.prototype.invoke = function (body) {
|
104 |
|
105 | var me = this;
|
106 | body = body || "";
|
107 |
|
108 | var url = this.serviceObject.id + '/actions/' + this.data.name;
|
109 |
|
110 | return client.request({
|
111 | method: 'POST',
|
112 | path: url,
|
113 | headers: {
|
114 | 'Content-Type': 'text/plain'
|
115 | },
|
116 | body: body.toString(),
|
117 | }).then(function (res) {
|
118 |
|
119 | if(res.id)
|
120 | this.status.id = res.id;
|
121 |
|
122 | if(res.createdAt)
|
123 | this.status.createdAt = res.createdAt;
|
124 |
|
125 | return Promise.resolve(me);
|
126 | }).bind(this);
|
127 | };
|
128 |
|
129 | /**
|
130 | * Reset the status of an action
|
131 | * */
|
132 | Action.prototype.reset = function () {
|
133 | this.status.id = null;
|
134 | this.status.createdAt = null;
|
135 | };
|
136 |
|
137 | /**
|
138 | * Subscribe to receive updates for an action
|
139 | * */
|
140 | Action.prototype.subscribe = function (fn) {
|
141 | var topic = this.getServiceObject().id + "/actions/" + this.name;
|
142 | return this.client.subscribe(topic, fn).bind(this);
|
143 | };
|
144 |
|
145 | /**
|
146 | * Unsubscribe from updates for an action
|
147 | * */
|
148 | Action.prototype.subscribe = function (fn) {
|
149 | var topic = this.getServiceObject().id + "/actions/" + this.name;
|
150 | return this.client.subscribe(topic, fn).bind(this);
|
151 | };
|
152 |
|
153 | /**
|
154 | * Get the status of an action
|
155 | *
|
156 | * @param {mixed} newStatus optional, set a new status of the action
|
157 | * @return {Promise} Promise callback with result
|
158 | */
|
159 | Action.prototype.status = function (newStatus) {
|
160 |
|
161 | var url = this.serviceObject.id + '/actions/' + this.data.id;
|
162 | var isSet = (newStatus !== undefined);
|
163 |
|
164 | return client.request({
|
165 | method: isSet ? 'PUT' : 'GET',
|
166 | path: url,
|
167 | headers: {
|
168 | 'Content-Type': isSet ? 'text/plain' : 'application/json'
|
169 | },
|
170 | body: isSet ? newStatus : null,
|
171 | });
|
172 | };
|
173 |
|
174 | /**
|
175 | * Cancel a launched action
|
176 | * @return {Promise} Promise callback with result
|
177 | */
|
178 | Action.prototype.cancel = function () {
|
179 | var url = this.serviceObject.id + '/actions/' + this.data.id;
|
180 | return client.delete(url).then(function () {
|
181 | this.status = {};
|
182 | return Promise.resolve();
|
183 | }).bind(this);
|
184 | };
|
185 |
|
186 | module.exports = Action;
|
187 | </code></pre>
|
188 | </article>
|
189 | </section>
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | </div>
|
195 |
|
196 | <nav>
|
197 | <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Action.html">Action</a></li><li><a href="Channel.html">Channel</a></li><li><a href="Client.html">Client</a></li><li><a href="Raptor.html">Raptor</a></li><li><a href="RecordSet.html">RecordSet</a></li><li><a href="ResultSet.html">ResultSet</a></li><li><a href="ServiceObject.html">ServiceObject</a></li><li><a href="Stream.html">Stream</a></li><li><a href="User.html">User</a></li></ul><h3>Global</h3><ul><li><a href="global.html#Container">Container</a></li></ul>
|
198 | </nav>
|
199 |
|
200 | <br class="clear">
|
201 |
|
202 | <footer>
|
203 | Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Wed Nov 02 2016 11:15:06 GMT+0100 (CET)
|
204 | </footer>
|
205 |
|
206 | <script> prettyPrint(); </script>
|
207 | <script src="scripts/linenumber.js"> </script>
|
208 | </body>
|
209 | </html>
|