Skip to content

Commit

Permalink
0.6.0 - stopSyncingWith, subscribable dispose, and various clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
gkubisa authored and rniemeyer committed Apr 7, 2017
1 parent 3eed0cb commit 9f12688
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 126 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# http://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
charset = "utf8"

[*.js]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function(grunt) {
},
watch: {
scripts: {
files: ['src/*.*'],
files: ['src/*.*', 'spec/*.spec.js'],
tasks: ['default'],
options: {
nospawn: true
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ var comparer = function(newValue, oldValue) {
this.value = ko.observable(value).syncWith("mytopic", false, false, comparer);
```

**stopSyncingWith** *- stopSyncingWith(topic)*

The `stopSyncingWith` function removes the subscription that an observable has on a topic as well as the subscription used to automatically publish changes to the observable.

```js
this.value.stopSyncingWith("mytopic");
```

**dispose** *- dispose()*

The `dispose` function removes all the subscriptions that an observable has on any topic as well as all the subscriptions used to automatically publish changes to the observable.

This function is attached to the observable when `publishOn`, `subscribeTo` or `syncWith` is called. If the observable already has a pre-existing `dispose` function, then it is automatically called in addition to performing the clean-up steps mentioned above.

```js
this.value.dispose();
```

Dependencies
------------
* Knockout 2.0+
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "knockout-postbox",
"version": "0.5.2",
"main": "build/knockout-postbox.min.js",
"dependencies": {
"knockout": ">= 2.0"
Expand All @@ -16,6 +15,7 @@
"src",
"Gruntfile.js",
"package.json",
".gitignore"
".gitignore",
".editorconfig"
]
}
54 changes: 52 additions & 2 deletions build/knockout-postbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-postbox 0.5.2 | (c) 2015 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-postbox 0.6.0 | (c) 2017 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
//CommonJS
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
Expand All @@ -11,7 +11,7 @@
factory(ko, ko.postbox = {});
}
}(function(ko, exports, undefined) {
var disposeTopicSubscription, existingSubscribe,
var disposeTopicSubscription, ensureDispose, existingSubscribe,
subscriptions = {},
subId = 1;

Expand Down Expand Up @@ -93,10 +93,52 @@
return cacheItem && exports.serializer(newValue) === cacheItem.serialized;
};

// Ensures that a `subscribable` has a `dispose` method which cleans up all
// subscriptions added by `knockout-postbox`.
ensureDispose = function() {
var existingDispose,
self = this;

// Make sure we're adding the custom `dispose` method at most once.
if (!self.willDisposePostbox) {
self.willDisposePostbox = true;

existingDispose = self.dispose;
self.dispose = function() {
var topic, types, type, sub,
subs = self.postboxSubs;

if (subs) {
for (topic in subs) {
if (subs.hasOwnProperty(topic)) {
types = subs[topic];
if (types) {
for (type in types) {
if (types.hasOwnProperty(type)) {
sub = types[type];
if (sub && typeof sub.dispose == "function") {
sub.dispose();
}
}
}
}
}
}
}

if (existingDispose) {
existingDispose.call(self);
}
};
}
};

//augment observables/computeds with the ability to automatically publish updates on a topic
ko.subscribable.fn.publishOn = function(topic, skipInitialOrEqualityComparer, equalityComparer) {
var skipInitialPublish, subscription, existingDispose;

ensureDispose.call(this);

if (topic) {
//allow passing the equalityComparer as the second argument
if (typeof skipInitialOrEqualityComparer === "function") {
Expand Down Expand Up @@ -163,6 +205,8 @@
var initializeWithLatestValue, current, callback, subscription, existingDispose,
self = this;

ensureDispose.call(this);

//allow passing the filter as the second argument
if (typeof initializeWithLatestValueOrTransform === "function") {
transform = initializeWithLatestValueOrTransform;
Expand Down Expand Up @@ -218,5 +262,11 @@
return this;
};

ko.subscribable.fn.stopSyncingWith = function(topic) {
this.unsubscribeFrom(topic).stopPublishingOn(topic);

return this;
};

ko.postbox = exports;
}));
4 changes: 2 additions & 2 deletions build/knockout-postbox.min.js

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

16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
{
"name": "knockout-postbox",
"version": "0.5.2",
"version": "0.6.0",
"repository": {
"type": "git",
"url": "git://github.com/rniemeyer/knockout-postbox.git"
},
"main": "build/knockout-postbox.js",
"scripts": {
"test": "grunt jasmine",
"watch": "grunt watch",
"build": "grunt"
},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "0.x.x",
"grunt-contrib-jshint": "0.x.x",
"grunt-contrib-watch": "0.x.x",
"grunt-cli": "^1.2.0",
"grunt-contrib-concat": "0.x.x",
"grunt-contrib-jasmine": "0.x.x",
"grunt-template-jasmine-istanbul": "0.x.x"
"grunt-contrib-jshint": "0.x.x",
"grunt-contrib-uglify": "0.x.x",
"grunt-contrib-watch": "0.x.x",
"grunt-template-jasmine-istanbul": "^0.4.0"
}
}
Loading

0 comments on commit 9f12688

Please sign in to comment.