observ-varhash
Advanced tools
Comparing version 0.1.4 to 0.2.0
127
index.js
@@ -19,3 +19,3 @@ var Observ = require('observ') | ||
checkKey(key) | ||
initialState[key] = isFunc(observ) ? observ() : observ | ||
initialState[key] = isFn(observ) ? observ() : observ | ||
} | ||
@@ -26,6 +26,5 @@ | ||
var actions = methods(createValue) | ||
for (var k in actions) { | ||
obs[k] = actions[k] | ||
} | ||
obs.get = get.bind(obs) | ||
obs.put = put.bind(obs, createValue) | ||
obs.delete = del.bind(obs) | ||
@@ -35,3 +34,3 @@ for (key in hash) { | ||
if (isFunc(obs[key])) { | ||
if (isFn(obs[key])) { | ||
obs._removeListeners[key] = obs[key](watch(obs, key, currentTransaction)) | ||
@@ -49,3 +48,3 @@ } | ||
if (isFunc(observ) && observ() !== newState[key]) { | ||
if (isFn(observ) && observ() !== newState[key]) { | ||
observ.set(newState[key]) | ||
@@ -59,49 +58,46 @@ } | ||
function methods (createValue) { | ||
return { | ||
get: function (key) { | ||
return this[key] | ||
}, | ||
// access and mutate | ||
function get (key) { | ||
return this[key] | ||
} | ||
put: function (key, val) { | ||
checkKey(key) | ||
function put (createValue, key, val) { | ||
checkKey(key) | ||
var observ = createValue(val, key) | ||
var state = extend(this()) | ||
var observ = createValue(val, key) | ||
var state = extend(this()) | ||
state[key] = isFunc(observ) ? observ() : observ | ||
state[key] = isFn(observ) ? observ() : observ | ||
if (isFunc(this._removeListeners[key])) { | ||
this._removeListeners[key]() | ||
} | ||
if (isFn(this._removeListeners[key])) { | ||
this._removeListeners[key]() | ||
} | ||
this._removeListeners[key] = isFunc(observ) ? | ||
observ(watch(this, key)) : null | ||
this._removeListeners[key] = isFn(observ) ? | ||
observ(watch(this, key)) : null | ||
state._diff = diff(key, state[key]) | ||
setNonEnumerable(state, '_diff', diff(key, state[key])) | ||
this.set(state) | ||
this[key] = observ | ||
this.set(state) | ||
this[key] = observ | ||
return this | ||
}, | ||
return this | ||
} | ||
'delete': function (key) { | ||
var state = extend(this()) | ||
if (isFunc(this._removeListeners[key])) { | ||
this._removeListeners[key]() | ||
} | ||
function del (key) { | ||
var state = extend(this()) | ||
if (isFn(this._removeListeners[key])) { | ||
this._removeListeners[key]() | ||
} | ||
delete this._removeListeners[key] | ||
delete state[key] | ||
delete this._removeListeners[key] | ||
delete state[key] | ||
state._diff = diff(key, ObservVarhash.Tombstone) | ||
setNonEnumerable(state, '_diff', diff(key, ObservVarhash.Tombstone)) | ||
this.set(state) | ||
this.set(state) | ||
return this | ||
} | ||
} | ||
return this | ||
} | ||
// processing | ||
function watch (obs, key, currentTransaction) { | ||
@@ -111,3 +107,4 @@ return function (value) { | ||
state[key] = value | ||
state._diff = diff(key, value) | ||
setNonEnumerable(state, '_diff', diff(key, value)) | ||
currentTransaction = state | ||
@@ -125,34 +122,30 @@ obs.set(state) | ||
function checkKey (key) { | ||
var msg = getKeyError(key) | ||
if (msg) { | ||
throw new Error(msg) | ||
} | ||
function isFn (obj) { | ||
return typeof obj === 'function' | ||
} | ||
function getKeyError (key) { | ||
switch (key) { | ||
case 'name': { | ||
return formatError(key, 'Clashes with `Function.prototype.name`.') | ||
} | ||
case 'get': | ||
case 'put': | ||
case 'delete': | ||
case '_diff': | ||
case '_removeListeners': { | ||
return formatError(key, 'Clashes with observ-varhash method') | ||
} | ||
default: { | ||
return '' | ||
} | ||
} | ||
function setNonEnumerable(object, key, value) { | ||
Object.defineProperty(object, key, { | ||
value: value, | ||
writable: true, | ||
configurable: true, | ||
enumerable: false | ||
}) | ||
} | ||
function formatError (key, reason) { | ||
return 'cannot create an observ-varhash with key `' + key + '`, ' + reason | ||
// errors | ||
var blacklist = { | ||
name: 'Clashes with `Function.prototype.name`.', | ||
get: 'get is a reserved key of observ-varhash method', | ||
put: 'put is a reserved key of observ-varhash method', | ||
delete: 'delete is a reserved key of observ-varhash method', | ||
_diff: '_diff is a reserved key of observ-varhash method', | ||
_removeListeners: '_removeListeners is a reserved key of observ-varhash' | ||
} | ||
function isFunc (obj) { | ||
return typeof obj === 'function' | ||
function checkKey (key) { | ||
if (!blacklist[key]) return | ||
throw new Error( | ||
'cannot create an observ-varhash with key `' + key + '`. ' + blacklist[key] | ||
) | ||
} | ||
@@ -159,0 +152,0 @@ |
{ | ||
"name": "observ-varhash", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "An object with observable key value pairs that can be added and removed", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
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
6782
118