From 5c5e44df615ce90a9963e9cb10b67e548104ca2d Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Wed, 29 Aug 2018 17:15:55 -0700 Subject: [PATCH] An abstract class for capturing nomad stats It follows the form of poll -> json parse -> append, Where append is defined in subclasses to add data from the new frame to long-lived rolling arrays of data. --- .../utils/classes/abstract-stats-tracker.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ui/app/utils/classes/abstract-stats-tracker.js diff --git a/ui/app/utils/classes/abstract-stats-tracker.js b/ui/app/utils/classes/abstract-stats-tracker.js new file mode 100644 index 000000000..25aa24ca4 --- /dev/null +++ b/ui/app/utils/classes/abstract-stats-tracker.js @@ -0,0 +1,27 @@ +import Mixin from '@ember/object/mixin'; +import { assert } from '@ember/debug'; + +export default Mixin.create({ + url: '', + + fetch() { + assert('StatTrackers need a fetch method, which should have an interface like window.fetch'); + }, + + append(/* frame */) { + assert( + 'StatTrackers need an append method, which takes the JSON response from a request to url as an argument' + ); + }, + + poll() { + const url = this.get('url'); + assert('Url must be defined', url); + + return this.get('fetch')(url) + .then(res => { + return res.json(); + }) + .then(frame => this.append(frame)); + }, +});