Skip to content

Commit

Permalink
Merge pull request #5 from ssbc/refactor2
Browse files Browse the repository at this point in the history
tidy codebase
  • Loading branch information
staltz committed Apr 19, 2022
2 parents eef1580 + 65e6437 commit 835b01c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
test:
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]

runs-on: ubuntu-latest
timeout-minutes: 2

steps:
- name: Checkout the repo
uses: actions/checkout@v2
- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
69 changes: 36 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
module.exports = function (filter) {
var value = null, listeners = [], oncers = []
module.exports = function Obz(filter) {
let value = null
const listeners = []
let oncers = []

function trigger (_value) {
value = _value
var length = listeners.length
for (var i = 0; i < length && value === _value; i++) {
function trigger(nextValue) {
value = nextValue
let length = listeners.length
for (let i = 0; i < length && value === nextValue; i++) {
const listener = listeners[i]
listener(value)
if (listeners.length !== length) { // remove
if (listeners.length !== length) {
// something was removed
length = listeners.length
if (listener !== listeners[i]) {
// we removed an earlier listener, must decrement i also
i -= 1
}
}
}
// decrement from length, incase a !immediately
// decrement from length, in case an immediately
// listener is added during a trigger
var l = oncers.length
var _oncers = oncers
let oncersLen = oncers.length
const oldOncers = oncers
oncers = []
while (l-- && _value === value) {
_oncers.shift()(value)
while (oncersLen-- && nextValue === value) {
oldOncers.shift()(value)
}
}

function many (ready, immediately) {
var i = listeners.push(ready) - 1
if (value !== null && immediately !== false) ready(value)
return function () { //manually remove...
//fast path, will happen if an earlier listener has not been removed.
if (listeners[i] !== ready)
i = listeners.indexOf(ready)
function obz(listener, immediately) {
let i = listeners.push(listener) - 1
if (value !== null && immediately !== false) listener(value)
return function remove() {
// manually remove...
// fast path, will happen if an earlier listener has not been removed.
if (listeners[i] !== listener) i = listeners.indexOf(listener)
listeners.splice(i, 1)
}
}

many.set = function (_value) {
if (filter ? filter(value, _value) : true) trigger(many.value = _value)
return many
obz.set = function set(nextValue) {
if (filter ? filter(value, nextValue) : true) {
trigger((obz.value = nextValue))
}
return obz
}

many.once = function (once, immediately) {
if(value !== null && immediately !== false) {
once(value)
return function () {}
}
else {
var i = oncers.push(once) - 1
return function () {
if(oncers[i] !== once)
i = oncers.indexOf(once)
obz.once = function once(oncer, immediately) {
if (value !== null && immediately !== false) {
oncer(value)
return function noop() {}
} else {
const i = oncers.push(oncer) - 1
return function remove() {
if (oncers[i] !== oncer) i = oncers.indexOf(oncer)
}
}
}

return many
return obz
}

0 comments on commit 835b01c

Please sign in to comment.