Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tidy codebase #5

Merged
merged 4 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}