Skip to content

Commit

Permalink
add batching tests
Browse files Browse the repository at this point in the history
  • Loading branch information
solkimicreb committed Nov 25, 2018
1 parent a9d269a commit 036d3e7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
23 changes: 20 additions & 3 deletions __tests__/batching.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('batching', () => {
.then(value => {
expect(value).toBe(12)
// eslint-disable-next-line
throw 15
throw 15;
})
.catch(err => {
expect(err).toBe(15)
Expand All @@ -158,14 +158,31 @@ describe('batching', () => {
})
})

test('should not break method this value', done => {
test('should not break method this value and args', done => {
const socket = new WebSocket('ws://www.example.com')

socket.onclose = function() {
socket.onclose = function (ev) {
expect(ev).toBeDefined()
expect(this).toBe(socket)
done()
}

socket.close()
})

test('should not break callback this value and args', done => {
const ctx = {}

setTimeout(
function (arg1, arg2) {
expect(arg1).toBe('Test')
expect(arg2).toBe('Test2')
expect(this).toBe(ctx)
done()
}.bind(ctx),
0,
'Test',
'Test2'
)
})
})
29 changes: 9 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
{
"name": "react-easy-state",
"version": "6.0.5",
"description": "React state management with a minimal API. Made with ES6 Proxies.",
"description":
"React state management with a minimal API. Made with ES6 Proxies.",
"main": "dist/cjs.es6.js",
"module": "dist/es.es6.js",
"types": "types/index.d.ts",
"files": [
"dist",
"types"
],
"files": ["dist", "types"],
"scripts": {
"test": "jest --coverage",
"test-watch": "jest --watch",
"test-update": "jest --updateSnapshot",
"test-builds": "node ./scripts/testBuilds.js",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"lint": "standard",
"lint-fix": "prettier --ignore-path '.gitignore' --write '**/*.{js,jsx}' '!**/build/**' && standard --fix",
"lint-fix":
"prettier --ignore-path '.gitignore' --write '**/*.{js,jsx}' '!**/build/**' && standard --fix",
"build-examples": "node ./scripts/buildExamples.js",
"link-examples": "node ./scripts/linkExamples.js",
"unlink-examples": "node ./scripts/unlinkExamples.js",
"build": "node ./scripts/build.js",
"build-toc": "node ./scripts/buildToc.js"
},
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"snapshotSerializers": ["enzyme-to-json/serializer"],
"collectCoverageFrom": [
"**/src/**",
"**/examples/**/*.{js,jsx}",
Expand Down Expand Up @@ -107,16 +104,8 @@
},
"standard": {
"parser": "babel-eslint",
"env": [
"jest"
],
"ignore": [
"**/build/**",
"**/node_modules/**"
]
"env": ["browser", "jest"],
"ignore": ["**/build/**", "**/node_modules/**"]
},
"pre-push": [
"lint",
"test"
]
"pre-push": ["lint", "test"]
}
32 changes: 16 additions & 16 deletions src/scheduler.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
const tasks = new Set()
let isStopped = false

export function add(task) {
export function add (task) {
if (isStopped) {
tasks.add(task)
} else {
runTask(task)
}
}

export function remove(task) {
export function remove (task) {
tasks.delete(task)
}

// this replaces the passed function with a function
// that batches all of its callback arguments
function batchCallbacks(fn) {
return function batchedCallbacks(...args) {
function batchCallbacks (fn) {
return function batchedCallbacks (...args) {
const batchedArgs = args.map(
arg =>
typeof arg === 'function'
? function(...args) {
return batch(arg, this, args)
}
? function (...args) {
return batch(arg, this, args)
}
: arg
)
return fn.apply(this, batchedArgs)
}
}

// bathes obj.onevent = fn like calls
function batchMethod(obj, method) {
function batchMethod (obj, method) {
const descriptor = Object.getOwnPropertyDescriptor(obj, method)
if (!descriptor) return
const newDescriptor = Object.assign({}, descriptor, {
set(value) {
set (value) {
const batched =
typeof value === 'function'
? function(...args) {
return batch(value, this, args)
}
? function (...args) {
return batch(value, this, args)
}
: value
return descriptor.set.call(this, batched)
}
Expand All @@ -49,7 +49,7 @@ function batchMethod(obj, method) {

// this runs the passed function and delays all re-renders
// until the function is finished running
export function batch(fn, ctx, args) {
export function batch (fn, ctx, args) {
try {
isStopped = true
return fn.apply(ctx, args)
Expand All @@ -60,7 +60,7 @@ export function batch(fn, ctx, args) {
}
}

function runTask(task) {
function runTask (task) {
task()
}

Expand Down Expand Up @@ -89,9 +89,9 @@ if (globalObj) {
)
}
// eslint-disable-next-line
Promise.prototype.then = batchCallbacks(Promise.prototype.then)
Promise.prototype.then = batchCallbacks(Promise.prototype.then);
// eslint-disable-next-line
Promise.prototype.catch = batchCallbacks(Promise.prototype.catch)
Promise.prototype.catch = batchCallbacks(Promise.prototype.catch);

// this batches websocket event handlers
if (globalObj.WebSocket) {
Expand Down

0 comments on commit 036d3e7

Please sign in to comment.