Comparing version 1.1.0 to 1.1.1
module.exports = function (filter) { | ||
var value, listeners = [], listener = null | ||
let value | ||
let listeners = [] | ||
let listener = null | ||
function trigger (_value) { | ||
eventually.value = value = _value | ||
if(listener) { | ||
_listener = listener | ||
if (listener) { | ||
const _listener = listener | ||
listener = null | ||
_listener(value) | ||
return | ||
} else if (listeners.length) { | ||
while (listeners.length) { listeners.shift()(value) } | ||
} | ||
else if(listeners.length) { | ||
while(listeners.length) | ||
listeners.shift()(value) | ||
} | ||
} | ||
function eventually (ready) { | ||
if(value != null) | ||
ready(value) | ||
else if(!listener) | ||
listener = ready | ||
if (value != null) ready(value) | ||
else if (!listener) listener = ready | ||
else if (!listeners.length) { | ||
listeners = [listener, ready] | ||
listener = null | ||
} | ||
else | ||
listeners.push(ready) | ||
} else listeners.push(ready) | ||
} | ||
eventually.set = function (_value) { | ||
if(filter ? filter(value, _value) : true) trigger(_value) | ||
if (filter ? filter(value, _value) : true) trigger(_value) | ||
return eventually | ||
@@ -37,9 +33,1 @@ } | ||
} | ||
@@ -1,8 +0,7 @@ | ||
var observer = require('./') | ||
const observer = require('./') | ||
module.exports = function filter (observ, test) { | ||
var obv = observer() | ||
var listeners = [] | ||
var rm = observ(function (_value) { | ||
if(test(_value)) obv.set(_value) | ||
const obv = observer() | ||
observ(function (_value) { | ||
if (test(_value)) obv.set(_value) | ||
}) | ||
@@ -13,11 +12,6 @@ | ||
module.exports = function filter(observ, test) { | ||
module.exports = function filter (observ, test) { | ||
return function (ready) { | ||
return function (ready) { | ||
} | ||
} | ||
25
index.js
@@ -1,2 +0,2 @@ | ||
module.exports = function Obz(filter) { | ||
module.exports = function Obz (filter) { | ||
let value = null | ||
@@ -6,6 +6,6 @@ const listeners = [] | ||
function trigger(nextValue) { | ||
function trigger (nextValue) { | ||
value = nextValue | ||
let length = listeners.length | ||
for (let i = 0; i < length && value === nextValue; i++) { | ||
for (let i = 0; i < length && value === nextValue; i++) { // eslint-disable-line | ||
const listener = listeners[i] | ||
@@ -31,3 +31,3 @@ if (listener(value) === false) { | ||
oncers = [] | ||
while (oncersLen-- && nextValue === value) { | ||
while (oncersLen-- && nextValue === value) { // eslint-disable-line | ||
oldOncers.shift()(value) | ||
@@ -37,3 +37,3 @@ } | ||
function obz(listener, immediately) { | ||
function obz (listener, immediately) { | ||
let i = listeners.push(listener) - 1 | ||
@@ -43,6 +43,6 @@ if (value !== null && immediately !== false) { | ||
remove() | ||
return function noop() {} | ||
return function noop () {} | ||
} | ||
} | ||
function remove() { | ||
function remove () { | ||
// manually remove... | ||
@@ -56,3 +56,3 @@ // fast path, will happen if an earlier listener has not been removed. | ||
obz.set = function set(nextValue) { | ||
obz.set = function set (nextValue) { | ||
if (filter ? filter(value, nextValue) : true) { | ||
@@ -64,10 +64,11 @@ trigger((obz.value = nextValue)) | ||
obz.once = function once(oncer, immediately) { | ||
obz.once = function once (oncer, immediately) { | ||
if (value !== null && immediately !== false) { | ||
oncer(value) | ||
return function noop() {} | ||
return function noop () {} | ||
} else { | ||
const i = oncers.push(oncer) - 1 | ||
return function remove() { | ||
let i = oncers.push(oncer) - 1 | ||
return function remove () { | ||
if (oncers[i] !== oncer) i = oncers.indexOf(oncer) | ||
if (i >= 0) oncers.splice(i, 1) | ||
} | ||
@@ -74,0 +75,0 @@ } |
{ | ||
"name": "obz", | ||
"description": "simple and lightweight observer", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"homepage": "https://github.com/ssbc/obz", | ||
@@ -10,12 +10,13 @@ "repository": { | ||
}, | ||
"dependencies": {}, | ||
"scripts": { | ||
"test": "npm run test:js && npm run lint", | ||
"test:js": "tape test/*.js", | ||
"lint": "standard --fix" | ||
}, | ||
"devDependencies": { | ||
"tape": "^4.6.0" | ||
"standard": "^17.1.0", | ||
"tape": "^5.7.0" | ||
}, | ||
"author": "'Dominic Tarr' <dominic.tarr@gmail.com> (dominictarr.com)", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "set -e; for t in test/*.js; do node $t; done" | ||
}, | ||
"readme": "# obz\n\nA fork of https://github.com/dominictarr/obv\n\nRepresents a value changing in time.\n\nSometimes you have a sequence of values over time,\nusually we use streams for this. However, in some cases,\nthe latest value, or the next value matters more\nthan the entire history of values.\n\nFor example, if you are drawing the mouse pointer,\nyou just need the current position, not the historical positions.\n\nAn observable is a simple way to represent these instantaniously changing values.\n\n## Obv() => observable\n\nreturns an observable instance.\n\n## observable(listener=function, immediate=boolean) => remove\n\nregister `listener` with the observable. `immediate` is true by default.\n`listener` is called with the current value (if one has been set), set to false to disable.\n\nA function `remove` is returned, calling this function deregisters the listener.\n\nAnother way of deregistering the listener is by returning `false` from inside the\nlistener function.\n\n## observable.set(value=any)\n\nset the current value of this observable. Any registered listeners will be called.\n\n## observable.once(listener=function, immediate=boolean) => remove\n\nLike the above call to `observable()` except the listener will only be triggered _once_.\n\nThis is useful for representing variables which must be set after an async operation\n(say, initializing a database connection), but if the value is initalized\nyou can act on it immediately.\n\nIf you call `observable.once(listener, false)` that triggers at the _next_\ntime the value is set, which so far I have used to create live streams.\n\n## observable.value\n\nThe current value of the observable is provided as a property.\nI recommend not using null as a observable value in your program,\nbecause it makes testing the current value awkward.\n\n## License\n\nMIT\n\n" | ||
} | ||
"license": "MIT" | ||
} |
@@ -1,22 +0,21 @@ | ||
function noop () {} | ||
module.exports = function (filter) { | ||
var value | ||
let value | ||
let listener | ||
function observer (ready) { | ||
if(value) | ||
if(ready(value)) return noop | ||
else | ||
listener = ready | ||
if (value) { | ||
if (ready(value)) return noop | ||
else { listener = ready } | ||
} | ||
return function () { | ||
if(listener === ready) listener = null | ||
if (listener === ready) listener = null | ||
} | ||
} | ||
observer.set = function (value) { | ||
if(filter(value, _value)) { | ||
if(listener) listener(value = _value) | ||
observer.set = function (_value) { | ||
if (filter(value, _value)) { | ||
if (listener) listener(value = _value) | ||
} | ||
} | ||
} | ||
@@ -1,10 +0,9 @@ | ||
var tape = require('tape') | ||
const tape = require('tape') | ||
module.exports = function (observable) { | ||
tape('set listener and trigger', function (t) { | ||
var o = observable() | ||
var value = Math.random(), checked = 0 | ||
var rm = o(function (_value) { | ||
checked ++ | ||
const o = observable() | ||
const value = Math.random(); let checked = 0 | ||
const rm = o(function (_value) { | ||
checked++ | ||
t.equal(_value, value) | ||
@@ -32,11 +31,11 @@ }) | ||
tape('set value before listener', function (t) { | ||
var o = observable() | ||
const o = observable() | ||
var value = Math.random(), checked = 0 | ||
const value = Math.random(); let checked = 0 | ||
o.set(value) | ||
var rm = o(function (_value) { | ||
const rm = o(function (_value) { | ||
t.equal(_value, value) | ||
checked ++ | ||
checked++ | ||
}) | ||
@@ -57,15 +56,15 @@ | ||
t.end() | ||
}) | ||
tape('add listener within trigger', function (t) { | ||
var o = observable() | ||
const o = observable() | ||
var value = Math.random(), checked = 0, checking = false | ||
const value = Math.random() | ||
let checked = 0 | ||
o(function (_value) { | ||
checked ++ | ||
checked++ | ||
t.equal(_value, value) | ||
o(function (_value) { | ||
checked ++ | ||
checked++ | ||
t.equal(_value, value) | ||
@@ -82,3 +81,3 @@ }) | ||
tape('remove itself, simple', function (t) { | ||
var o = observable() | ||
const o = observable() | ||
t.plan(1) | ||
@@ -98,8 +97,7 @@ | ||
tape('remove itself, complex', function (t) { | ||
var o = observable() | ||
const o = observable() | ||
t.plan(5) | ||
var remove = o(function (val) { | ||
const remove = o(function (val) { | ||
t.equals(val, 10) | ||
@@ -126,3 +124,3 @@ return false | ||
tape('remove itself (immediately)', function (t) { | ||
var o = observable() | ||
const o = observable() | ||
@@ -142,18 +140,15 @@ o.set(10) | ||
tape('flatten recursion', function (t) { | ||
var o = observable() | ||
const o = observable() | ||
var value = Math.random(), checked = 1, checking = false | ||
let checked = 1 | ||
function recurse () { | ||
checking = true | ||
checked ++ | ||
if(checked < 3) | ||
o.set(checked) | ||
checking = false | ||
checked++ | ||
if (checked < 3) { o.set(checked) } | ||
} | ||
var last, last2 | ||
let last, last2 | ||
o(function (v) { | ||
console.log(last, v) | ||
if(last) t.ok(v > last, 'monotonic increasing listeners') | ||
if (last) t.ok(v > last, 'monotonic increasing listeners') | ||
last = v | ||
@@ -164,8 +159,6 @@ }) | ||
console.log(last2, v) | ||
if(last2) t.ok(v > last2, 'monotonic increasing listeners') | ||
if (last2) t.ok(v > last2, 'monotonic increasing listeners') | ||
last2 = v | ||
}) | ||
o.set(checked) | ||
@@ -177,62 +170,12 @@ | ||
tape('once', function (t) { | ||
var o = observable() | ||
var fired = 0 | ||
o.once(function (v) { | ||
fired ++ | ||
t.equal(v, 1) | ||
}) | ||
o.set(1) | ||
o.once(function (v) { | ||
fired ++ | ||
t.equal(v, 2) | ||
}, false) | ||
o.once(function (v) { | ||
fired ++ | ||
t.equal(v, 1) | ||
}) | ||
t.equal(fired, 2) | ||
o.set(2) | ||
o.set(3) | ||
t.equal(fired, 3) | ||
t.end() | ||
}) | ||
tape('once, !immediately, inside trigger', function (t) { | ||
var o = observable() | ||
var fired = 0 | ||
o.once(function (v) { | ||
t.equal(v, 1) | ||
fired ++ | ||
o.once(function (v) { | ||
fired ++ | ||
t.equal(v, 2) | ||
}, false) | ||
}) | ||
o.set(1) | ||
t.equal(fired, 1) | ||
o.set(2) | ||
t.equal(fired, 2) | ||
t.end() | ||
}) | ||
tape('bug', (t) => { | ||
t.plan(4) | ||
var o = observable() | ||
const o = observable() | ||
var a = o(() => { | ||
t.pass("first was called") | ||
const a = o(() => { | ||
t.pass('first was called') | ||
}) | ||
o(() => { | ||
t.pass("second was called") | ||
t.pass('second was called') | ||
// This removes the previous listener, which changes the number of | ||
@@ -243,5 +186,4 @@ // listeners. | ||
o(() => { | ||
t.pass("third was called") | ||
t.pass('third was called') | ||
// `listeners.length` started at 3, but when the above listener removes | ||
@@ -260,6 +202,7 @@ // it reduces the listener count to 2. When Obv tries to run the third | ||
}) | ||
} | ||
if(!module.parent) module.exports (require('../')) | ||
if ( | ||
!module.parent || | ||
module.parent.path.endsWith('node_modules/tape/bin') | ||
) module.exports(require('../')) |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
11
328
12042
2