Skip to content

Commit

Permalink
Merge pull request #894 from OpenGeoscience/idle-property
Browse files Browse the repository at this point in the history
Add an idle property to objects.
  • Loading branch information
manthey authored Aug 14, 2018
2 parents 3657696 + c676e9e commit 7d93da4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

## Unreleased

### Features
- Added an idle property to objects (#894)

### Changes

- Changed build process: optional dependencies are now included in the bundle by default (#890)


## Version 0.17.0

### Features
Expand Down
19 changes: 17 additions & 2 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ var object = function () {
m_promiseCount = 0;

/**
* Bind a handler that will be called once when all internal promises are
* resolved.
* Bind a handler that will be called one time when all internal promises are
* resolved. If there are no outstanding promises, this is invoked
* synchronously.
*
* @param {function} handler A function taking no arguments.
* @returns {this}
Expand All @@ -38,6 +39,20 @@ var object = function () {
return m_this;
};

/**
* Getter for the idle state. Read only.
*
* @property {boolean} idle `true` if the object is idle (`onIdle` would call
* a handler immediately).
* @name geo.object#idle
*/
Object.defineProperty(this, 'idle', {
get: function () {
return !m_promiseCount;
},
configurable: true
});

/**
* Add a new promise object preventing idle event handlers from being called
* until it is resolved.
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/deferred.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('Testing onIdle event handling', function () {
it('no deferred', function () {
var obj = geo.object(), called = false;

expect(obj.idle).toBe(true);
obj.onIdle(function () {
called = true;
});
Expand All @@ -19,10 +20,12 @@ describe('Testing onIdle event handling', function () {
var obj = geo.object(), defer = $.Deferred();

obj.addPromise(defer);
expect(obj.idle).toBe(false);
window.setTimeout(function () {
var called = false;
defer.resolve();

expect(obj.idle).toBe(true);
obj.onIdle(function () {
called = true;
});
Expand Down

0 comments on commit 7d93da4

Please sign in to comment.