Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an idle property to objects. #894

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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