Skip to content

Commit

Permalink
Added new options to disable pausing and canceling.
Browse files Browse the repository at this point in the history
  • Loading branch information
William Troup committed Jan 8, 2024
1 parent d05403c commit 5c32c87
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
21 changes: 15 additions & 6 deletions dist/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,23 @@
}
function cancelWatchObject(storageId) {
if (_watches.hasOwnProperty(storageId)) {
fireCustomTrigger(_watches[storageId].options.onCancel, storageId);
clearTimeout(_watches[storageId].timer);
delete _watches[storageId];
var watchOptions = _watches[storageId].options;
if (watchOptions.allowCanceling || _watches_Cancel) {
fireCustomTrigger(watchOptions.onCancel, storageId);
clearTimeout(_watches[storageId].timer);
delete _watches[storageId];
}
}
}
function pauseWatchObject(storageId, milliseconds) {
var result = false;
if (_watches.hasOwnProperty(storageId)) {
var watchOptions = _watches[storageId].options;
watchOptions.starts = new Date();
watchOptions.starts.setMilliseconds(watchOptions.starts.getMilliseconds() + milliseconds);
result = true;
if (watchOptions.allowPausing) {
watchOptions.starts = new Date();
watchOptions.starts.setMilliseconds(watchOptions.starts.getMilliseconds() + milliseconds);
result = true;
}
}
return result;
}
Expand All @@ -204,6 +209,8 @@
options.maximumChangesBeforeCanceling = getDefaultNumber(options.maximumChangesBeforeCanceling, 0);
options.pauseTimeoutOnChange = getDefaultNumber(options.pauseTimeoutOnChange, 0);
options.propertyNames = getDefaultArray(options.propertyNames, null);
options.allowCanceling = getDefaultBoolean(options.allowCanceling, true);
options.allowPausing = getDefaultBoolean(options.allowPausing, null);
options = getWatchOptionsCustomTriggers(options);
return options;
}
Expand Down Expand Up @@ -317,6 +324,7 @@
var _parameter_Window = null;
var _string = {empty:""};
var _watches = {};
var _watches_Cancel = false;
var _configuration = {};
var _attribute_Name_Watch_Options = "data-observe-watch-options";
this.watch = function(object, options) {
Expand Down Expand Up @@ -431,6 +439,7 @@
collectDOMObjects();
});
_parameter_Window.addEventListener("unload", function() {
_watches_Cancel = true;
cancelWatchesForObjects();
});
if (!isDefined(_parameter_Window.$observe)) {
Expand Down
24 changes: 12 additions & 12 deletions dist/observe.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions docs/CHANGE_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Version 0.6.0:

#### **Binding Options / Function Options:**
- Added a new binding/option called "allowCanceling", which states the watch can be canceled (defaults to true).
- Added a new binding/option called "allowPausing", which states the watch can be paused (defaults to true).

#### **Public Functions:**
- Added new public function "pauseWatches()", which is used to pause all the watches for a specific number of milliseconds.
- Added new public function "resumeWatches()", which is used to resume all the watches currently paused.
Expand Down
2 changes: 2 additions & 0 deletions docs/binding/options/OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Below is a list of all the options supported in the "data-observe-watch-options"
| *number* | maximumChangesBeforeCanceling | States the total number of changes that are allowed before the watch is cancelled (defaults to 0, which is off) |
| *number* | pauseTimeoutOnChange | States the delay (in milliseconds) that should be used before checking for changes again after a change is detected (defaults to 0, which is off) |
| *string[]* | propertyNames | States the property names that should be watched for changes (defaults to all). |
| *boolean* | allowCanceling | States if the watch can be canceled (defaults to true). |
| *boolean* | allowPausing | States if the watch can be paused (defaults to true). |

<br/>

Expand Down
29 changes: 20 additions & 9 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

// Variables: Watches
_watches = {},
_watches_Cancel = false,

// Variables: Configuration
_configuration = {},
Expand Down Expand Up @@ -269,10 +270,14 @@

function cancelWatchObject( storageId ) {
if ( _watches.hasOwnProperty( storageId ) ) {
fireCustomTrigger( _watches[ storageId ].options.onCancel, storageId );
clearTimeout( _watches[ storageId ].timer );

delete _watches[ storageId ];
var watchOptions = _watches[ storageId ].options;

if ( watchOptions.allowCanceling || _watches_Cancel ) {
fireCustomTrigger( watchOptions.onCancel, storageId );
clearTimeout( _watches[ storageId ].timer );

delete _watches[ storageId ];
}
}
}

Expand All @@ -282,10 +287,12 @@
if ( _watches.hasOwnProperty( storageId ) ) {
var watchOptions = _watches[ storageId ].options;

watchOptions.starts = new Date();
watchOptions.starts.setMilliseconds( watchOptions.starts.getMilliseconds() + milliseconds );

result = true;
if ( watchOptions.allowPausing ) {
watchOptions.starts = new Date();
watchOptions.starts.setMilliseconds( watchOptions.starts.getMilliseconds() + milliseconds );

result = true;
}
}

return result;
Expand All @@ -309,7 +316,9 @@
options.maximumChangesBeforeCanceling = getDefaultNumber( options.maximumChangesBeforeCanceling, 0 );
options.pauseTimeoutOnChange = getDefaultNumber( options.pauseTimeoutOnChange, 0 );
options.propertyNames = getDefaultArray( options.propertyNames, null );

options.allowCanceling = getDefaultBoolean( options.allowCanceling, true );
options.allowPausing = getDefaultBoolean( options.allowPausing, null );

options = getWatchOptionsCustomTriggers( options );

return options;
Expand Down Expand Up @@ -779,6 +788,8 @@
} );

_parameter_Window.addEventListener( "unload", function() {
_watches_Cancel = true;

cancelWatchesForObjects();
} );

Expand Down

0 comments on commit 5c32c87

Please sign in to comment.