Skip to content

Commit 39709ce

Browse files
committed
Updated provider with do method
1 parent 8b0ec6a commit 39709ce

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

js/provider.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ const EVENT_DELETED = `${EVENT_ROOT}.deleted`;
2222
* @class
2323
*/
2424
export default class Provider extends Emitter {
25+
/**
26+
* Create a provider, with a model constructor and optionally
27+
* a base URL for requests. If no model is provided then
28+
* Object types will be emitted. Add event listeners for
29+
* mvc.provider.{started,completed,added,changed,deleted} to
30+
* tap into the request lifecycle and mvc.provider.error to
31+
* deal with request errors.
32+
* @param {Model} constructor - The model used to create objects from data.
33+
* @param {string} origin - The base URL used for making requests.
34+
*/
2535
constructor(constructor, origin) {
2636
super();
2737
this.$origin = origin || '';
@@ -30,6 +40,14 @@ export default class Provider extends Emitter {
3040
this.$timer = null;
3141
}
3242

43+
/**
44+
* Request data from a remote source, either once or by interval.
45+
* Subsequent calls to this function will cancel any existing
46+
* timers.
47+
* @param {string} url - The endpoint of the data provider.
48+
* @param {Object} req - Request data. See the documentaton for fetch.
49+
* @param {number} interval - If provided, the number of milliseconds between each request.
50+
*/
3351
request(url, req, interval) {
3452
this.cancel();
3553
if (!this.$timer) {
@@ -40,6 +58,18 @@ export default class Provider extends Emitter {
4058
}
4159
}
4260

61+
/**
62+
* Perform a request without interrupting any existing request interval timer.
63+
* @param {string} url - The endpoint of the data provider.
64+
* @param {Object} req - Request data. See the documentaton for fetch.
65+
*/
66+
do(url, req) {
67+
this.$fetch(url, req);
68+
}
69+
70+
/**
71+
* Cancel any existing request interval timer.
72+
*/
4373
cancel() {
4474
if (this.$timer) {
4575
clearTimeout(this.$timer);
@@ -158,22 +188,30 @@ export default class Provider extends Emitter {
158188
return changed;
159189
}
160190

161-
// objects property returns all objects loaded by provider
191+
/**
192+
* Return all objects which are registered with the provider.
193+
*/
162194
get objects() {
163195
return Array.from(this.$objs.values());
164196
}
165197

166-
// keys property returns all keys
198+
/**
199+
* Return all object keys which are registered with the provider.
200+
*/
167201
get keys() {
168202
return Array.from(this.$objs.keys());
169203
}
170204

171-
// objectForKey returns an object for specific key
205+
/**
206+
* Return an object which is registered with a key.
207+
*/
172208
objectForKey(key) {
173209
return this.$objs.get(key);
174210
}
175211

176-
// clear removes all objects from the provider
212+
/**
213+
* Remove any registered objects.
214+
*/
177215
clear() {
178216
this.$objs.clear();
179217
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@djthorpe/js-framework",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"description": "Javascript Framework",
55
"main": "dist/js/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)