kobservable
Advanced tools
Comparing version 1.1.5 to 1.2.0
@@ -34,2 +34,5 @@ var equals = function equals(v1, v2) { | ||
}; | ||
observableContainer.subscriptionsCount = function () { | ||
return subscriptions.size; | ||
}; | ||
return observableContainer; | ||
@@ -47,3 +50,2 @@ } | ||
})); | ||
; | ||
if (!equals(computedData, memoizedData)) { | ||
@@ -99,6 +101,80 @@ memoizedData = computedData; | ||
}; | ||
computedContainer.subscriptionsCount = function () { | ||
return subscriptions.size; | ||
}; | ||
return computedContainer; | ||
} | ||
export { computed };export default observable; | ||
var slicedToArray = function () { | ||
function sliceIterator(arr, i) { | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"]) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
return function (arr, i) { | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} else if (Symbol.iterator in Object(arr)) { | ||
return sliceIterator(arr, i); | ||
} else { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
} | ||
}; | ||
}(); | ||
function throttled(source, delay) { | ||
var timeout = void 0; | ||
var inner = observable(source()); | ||
var subscription = function subscription(value) { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(function () { | ||
inner(value); | ||
}, delay); | ||
}; | ||
source.subscribe(subscription); | ||
var result = computed([inner], function (_ref) { | ||
var _ref2 = slicedToArray(_ref, 1); | ||
var innerValue = _ref2[0]; | ||
return innerValue; | ||
}); | ||
var innerUnsubscribe = result.unsubscribe; | ||
result.unsubscribe = function (observer) { | ||
innerUnsubscribe(observer); | ||
if (!result.subscriptionsCount()) { | ||
source.unsubscribe(subscription); | ||
} | ||
}; | ||
var innerSubscribe = result.subscribe; | ||
result.subscribe = function (observer) { | ||
if (!result.subscriptionsCount()) { | ||
source.subscribe(subscription); | ||
} | ||
innerSubscribe(observer); | ||
}; | ||
return result; | ||
} | ||
export { computed, throttled };export default observable; | ||
//# sourceMappingURL=kobservable-next.js.map |
import observable from './observable'; | ||
import computed from './computed'; | ||
import throttled from './throttled'; | ||
export default observable; | ||
export { computed }; | ||
export { computed, throttled }; |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.kobservable = global.kobservable || {}))); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.kobservable = global.kobservable || {}))); | ||
}(this, function (exports) { 'use strict'; | ||
var equals = function equals(v1, v2) { | ||
return Number.isNaN(v1) && Number.isNaN(v2) ? true : v1 === v2; | ||
}; | ||
var equals = function equals(v1, v2) { | ||
return Number.isNaN(v1) && Number.isNaN(v2) ? true : v1 === v2; | ||
}; | ||
/** | ||
* Creates a new observable instance. | ||
* @param initialValue The initial value. | ||
*/ | ||
function observable(initialValue) { | ||
var data = initialValue; | ||
var subscriptions = new Set(); | ||
var observableContainer = function observableContainer(value) { | ||
if (arguments.length) { | ||
if (!equals(data, value)) { | ||
data = value; | ||
subscriptions.forEach(function (subscription) { | ||
return subscription(data); | ||
}); | ||
} | ||
return this; | ||
} else { | ||
return data; | ||
} | ||
}; | ||
observableContainer.subscribe = function (subscription) { | ||
subscriptions.add(subscription); | ||
}; | ||
observableContainer.unsubscribe = function (subscription) { | ||
subscriptions.delete(subscription); | ||
}; | ||
observableContainer.unsubscribeAll = function () { | ||
subscriptions.clear(); | ||
}; | ||
return observableContainer; | ||
} | ||
/** | ||
* Creates a new observable instance. | ||
* @param initialValue The initial value. | ||
*/ | ||
function observable(initialValue) { | ||
var data = initialValue; | ||
var subscriptions = new Set(); | ||
var observableContainer = function observableContainer(value) { | ||
if (arguments.length) { | ||
if (!equals(data, value)) { | ||
data = value; | ||
subscriptions.forEach(function (subscription) { | ||
return subscription(data); | ||
}); | ||
} | ||
return this; | ||
} else { | ||
return data; | ||
} | ||
}; | ||
observableContainer.subscribe = function (subscription) { | ||
subscriptions.add(subscription); | ||
}; | ||
observableContainer.unsubscribe = function (subscription) { | ||
subscriptions.delete(subscription); | ||
}; | ||
observableContainer.unsubscribeAll = function () { | ||
subscriptions.clear(); | ||
}; | ||
observableContainer.subscriptionsCount = function () { | ||
return subscriptions.size; | ||
}; | ||
return observableContainer; | ||
} | ||
var empty = {}; | ||
function computed(sources, compute) { | ||
var memoizedData = empty; | ||
var attached = false; | ||
var subscriptions = new Set(); | ||
var updateData = function updateData() { | ||
var computedData = compute(sources.map(function (getter) { | ||
return getter(); | ||
})); | ||
; | ||
if (!equals(computedData, memoizedData)) { | ||
memoizedData = computedData; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var subscription = function subscription() { | ||
if (updateData()) { | ||
subscriptions.forEach(function (subscription) { | ||
return subscription(memoizedData); | ||
}); | ||
} | ||
}; | ||
var attach = function attach() { | ||
sources.forEach(function (source) { | ||
return source.subscribe(subscription); | ||
}); | ||
attached = true; | ||
}; | ||
var detach = function detach() { | ||
sources.forEach(function (source) { | ||
return source.unsubscribe(subscription); | ||
}); | ||
attached = false; | ||
}; | ||
var checkSubscriptions = function checkSubscriptions() { | ||
if (subscriptions.size && !attached) { | ||
attach(); | ||
} else if (!subscriptions.size && attached) { | ||
detach(); | ||
} | ||
}; | ||
var computedContainer = function computedContainer() { | ||
if (memoizedData === empty) { | ||
updateData(); | ||
} | ||
return memoizedData; | ||
}; | ||
computedContainer.subscribe = function (s) { | ||
subscriptions.add(s); | ||
checkSubscriptions(); | ||
}; | ||
computedContainer.unsubscribe = function (s) { | ||
subscriptions.delete(s); | ||
checkSubscriptions(); | ||
}; | ||
computedContainer.unsubscribeAll = function () { | ||
subscriptions.clear(); | ||
checkSubscriptions(); | ||
}; | ||
return computedContainer; | ||
var empty = {}; | ||
function computed(sources, compute) { | ||
var memoizedData = empty; | ||
var attached = false; | ||
var subscriptions = new Set(); | ||
var updateData = function updateData() { | ||
var computedData = compute(sources.map(function (getter) { | ||
return getter(); | ||
})); | ||
if (!equals(computedData, memoizedData)) { | ||
memoizedData = computedData; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var subscription = function subscription() { | ||
if (updateData()) { | ||
subscriptions.forEach(function (subscription) { | ||
return subscription(memoizedData); | ||
}); | ||
} | ||
}; | ||
var attach = function attach() { | ||
sources.forEach(function (source) { | ||
return source.subscribe(subscription); | ||
}); | ||
attached = true; | ||
}; | ||
var detach = function detach() { | ||
sources.forEach(function (source) { | ||
return source.unsubscribe(subscription); | ||
}); | ||
attached = false; | ||
}; | ||
var checkSubscriptions = function checkSubscriptions() { | ||
if (subscriptions.size && !attached) { | ||
attach(); | ||
} else if (!subscriptions.size && attached) { | ||
detach(); | ||
} | ||
}; | ||
var computedContainer = function computedContainer() { | ||
if (memoizedData === empty) { | ||
updateData(); | ||
} | ||
return memoizedData; | ||
}; | ||
computedContainer.subscribe = function (s) { | ||
subscriptions.add(s); | ||
checkSubscriptions(); | ||
}; | ||
computedContainer.unsubscribe = function (s) { | ||
subscriptions.delete(s); | ||
checkSubscriptions(); | ||
}; | ||
computedContainer.unsubscribeAll = function () { | ||
subscriptions.clear(); | ||
checkSubscriptions(); | ||
}; | ||
computedContainer.subscriptionsCount = function () { | ||
return subscriptions.size; | ||
}; | ||
return computedContainer; | ||
} | ||
var slicedToArray = function () { | ||
function sliceIterator(arr, i) { | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"]) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
exports['default'] = observable; | ||
exports.computed = computed; | ||
return function (arr, i) { | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} else if (Symbol.iterator in Object(arr)) { | ||
return sliceIterator(arr, i); | ||
} else { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
} | ||
}; | ||
}(); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
function throttled(source, delay) { | ||
var timeout = void 0; | ||
var inner = observable(source()); | ||
var subscription = function subscription(value) { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(function () { | ||
inner(value); | ||
}, delay); | ||
}; | ||
source.subscribe(subscription); | ||
var result = computed([inner], function (_ref) { | ||
var _ref2 = slicedToArray(_ref, 1); | ||
var innerValue = _ref2[0]; | ||
return innerValue; | ||
}); | ||
var innerUnsubscribe = result.unsubscribe; | ||
result.unsubscribe = function (observer) { | ||
innerUnsubscribe(observer); | ||
if (!result.subscriptionsCount()) { | ||
source.unsubscribe(subscription); | ||
} | ||
}; | ||
var innerSubscribe = result.subscribe; | ||
result.subscribe = function (observer) { | ||
if (!result.subscriptionsCount()) { | ||
source.subscribe(subscription); | ||
} | ||
innerSubscribe(observer); | ||
}; | ||
return result; | ||
} | ||
exports['default'] = observable; | ||
exports.computed = computed; | ||
exports.throttled = throttled; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
//# sourceMappingURL=kobservable.js.map |
@@ -11,2 +11,6 @@ export interface IObserver<T> { | ||
/** | ||
* The number of current subscriptions. | ||
*/ | ||
subscriptionsCount(): number; | ||
/** | ||
* Unsubscribes an observer. | ||
@@ -13,0 +17,0 @@ * @param observer The observer. |
{ | ||
"name": "kobservable", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "Observable constructor with KnockoutJS style", | ||
@@ -5,0 +5,0 @@ "main": "dist/kobservable.js", |
@@ -51,3 +51,3 @@ # K-Observable | ||
`npm install` | ||
`npm install && npm run build` | ||
@@ -54,0 +54,0 @@ ## Test |
@@ -17,3 +17,3 @@ import {ISubscribable, IObserver} from './subscribable'; | ||
const updateData = () => { | ||
const computedData = compute(sources.map(getter => getter()));; | ||
const computedData = compute(sources.map(getter => getter())); | ||
if (!equals(computedData, memoizedData)) { | ||
@@ -67,4 +67,5 @@ memoizedData = computedData; | ||
}; | ||
computedContainer.subscriptionsCount = () => subscriptions.size; | ||
return computedContainer; | ||
} |
import observable from './observable'; | ||
import computed from './computed'; | ||
import throttled from './throttled'; | ||
export default observable; | ||
export {computed}; | ||
export {computed, throttled}; |
@@ -56,3 +56,5 @@ import {ISubscribable, IObserver} from './subscribable'; | ||
observableContainer.subscriptionsCount = () => subscriptions.size; | ||
return observableContainer; | ||
} |
@@ -12,2 +12,6 @@ export interface IObserver<T> { | ||
/** | ||
* The number of current subscriptions. | ||
*/ | ||
subscriptionsCount(): number; | ||
/** | ||
* Unsubscribes an observer. | ||
@@ -14,0 +18,0 @@ * @param observer The observer. |
@@ -9,3 +9,4 @@ { | ||
"lib": [ | ||
"es6" | ||
"es6", | ||
"dom" | ||
], | ||
@@ -12,0 +13,0 @@ "target": "es6" |
@@ -33,2 +33,3 @@ import observable, {computed} from '../dist/kobservable'; | ||
source.subscribe(incSourceCount); | ||
t.is(source.subscriptionsCount(), 1); | ||
@@ -39,2 +40,4 @@ let middleCount = 0; | ||
middle.subscribe(incMiddleCount); | ||
t.is(source.subscriptionsCount(), 2); | ||
t.is(middle.subscriptionsCount(), 1); | ||
@@ -45,2 +48,5 @@ let endCount = 0; | ||
end.subscribe(incEndCount); | ||
t.is(source.subscriptionsCount(), 2); | ||
t.is(middle.subscriptionsCount(), 2); | ||
t.is(end.subscriptionsCount(), 1); | ||
@@ -65,3 +71,12 @@ t.is(sourceCount, 0); | ||
t.is(source.subscriptionsCount(), 2); | ||
t.is(middle.subscriptionsCount(), 2); | ||
t.is(end.subscriptionsCount(), 1); | ||
end.unsubscribe(incEndCount); | ||
t.is(source.subscriptionsCount(), 2); | ||
t.is(middle.subscriptionsCount(), 1); | ||
t.is(end.subscriptionsCount(), 0); | ||
source(3); | ||
@@ -73,2 +88,14 @@ | ||
t.is(endCount, 1); | ||
source.unsubscribe(incSourceCount); | ||
t.is(source.subscriptionsCount(), 1); | ||
t.is(middle.subscriptionsCount(), 1); | ||
t.is(end.subscriptionsCount(), 0); | ||
middle.unsubscribe(incMiddleCount); | ||
t.is(source.subscriptionsCount(), 0); | ||
t.is(middle.subscriptionsCount(), 0); | ||
t.is(end.subscriptionsCount(), 0); | ||
}); | ||
@@ -75,0 +102,0 @@ |
@@ -20,3 +20,5 @@ import observable from '../dist/kobservable'; | ||
let observedValue; | ||
t.is(property.subscriptionsCount(), 0); | ||
property.subscribe(value => observedValue = value); | ||
t.is(property.subscriptionsCount(), 1); | ||
const expectedValue = 'wololoooo'; | ||
@@ -32,3 +34,5 @@ property(expectedValue); | ||
property.subscribe(subscription); | ||
t.is(property.subscriptionsCount(), 1); | ||
property.unsubscribe(subscription); | ||
t.is(property.subscriptionsCount(), 0); | ||
property('anotherValue'); | ||
@@ -35,0 +39,0 @@ t.is(lastValue, ':)'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
62138
25
743