Comparing version 1.0.4 to 1.1.0
{ | ||
"name": "statecore", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "A state-observable library.", | ||
@@ -5,0 +5,0 @@ "main": "statecore.js", |
@@ -17,6 +17,5 @@ (function moduleify(moduleFactory) { | ||
return { createStatecore: function createStatecore(state) { | ||
var undefined = (function _undefined() {})(); | ||
var allObservers = []; | ||
function statecoreDiscard() { state = undefined; allObservers = undefined; } | ||
function statecoreIsDiscarded() { return allObservers === undefined; } | ||
function statecoreDiscard() { state = null; allObservers.splice(0); allObservers = null; } | ||
function statecoreIsDiscarded() { return allObservers === null; } | ||
function statecoreGetState() { return state; } | ||
@@ -28,14 +27,19 @@ function statecoreSetState(newState) { | ||
} | ||
function statecoreRemoveObserver(observer) { | ||
if (statecoreIsDiscarded()) return; | ||
var copyObservers = allObservers; | ||
allObservers = []; | ||
for (var idx = 0; idx < copyObservers.length; idx += 1) { | ||
if (observer !== copyObservers[idx]) allObservers.push(copyObservers[idx]); | ||
} | ||
} | ||
function statecoreAddObserver(observer) { | ||
if (statecoreIsDiscarded()) throw new Error('This Statecore has been discarded!'); | ||
if (typeof observer !== 'function') throw new Error('The observer must be a function!'); | ||
allObservers.push(observer); | ||
return function removeObserver() { | ||
if (observer && allObservers) { | ||
var copyObservers = allObservers; | ||
allObservers = []; | ||
for (var idx = 0; idx < copyObservers.length; idx += 1) { | ||
if (observer !== copyObservers[idx]) allObservers.push(copyObservers[idx]); | ||
} | ||
if (observer) { | ||
statecoreRemoveObserver(observer); | ||
observer = null; | ||
} | ||
observer = undefined; | ||
}; | ||
@@ -48,4 +52,4 @@ } | ||
} | ||
return { statecoreGetState: statecoreGetState, statecoreSetState: statecoreSetState, statecoreAddObserver: statecoreAddObserver, statecoreNotifyAllObservers: statecoreNotifyAllObservers, statecoreDiscard: statecoreDiscard, statecoreIsDiscarded: statecoreIsDiscarded }; | ||
return { statecoreGetState: statecoreGetState, statecoreSetState: statecoreSetState, statecoreAddObserver: statecoreAddObserver, statecoreRemoveObserver: statecoreRemoveObserver, statecoreNotifyAllObservers: statecoreNotifyAllObservers, statecoreDiscard: statecoreDiscard, statecoreIsDiscarded: statecoreIsDiscarded }; | ||
}}; | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4946
53