immutable-ops
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -46,5 +46,4 @@ 'use strict'; | ||
if (typeof opts.addMutated === 'function') { | ||
opts.addMutated(obj); | ||
} | ||
opts.batchManager.addMutated(obj); | ||
return obj; | ||
@@ -59,5 +58,6 @@ } | ||
function prepareNewObject(opts, instance) { | ||
if (opts.isWithMutations()) { | ||
if (opts.batchManager.isWithMutations()) { | ||
addCanMutateTag(opts, instance); | ||
} | ||
opts.createdObjects++; | ||
return instance; | ||
@@ -412,5 +412,6 @@ } | ||
function getMutationManager() { | ||
function getBatchManager() { | ||
var previousSessionStack = []; | ||
var currMutatedObjects = null; | ||
var objectsCreated = 0; | ||
@@ -431,14 +432,22 @@ return { | ||
currMutatedObjects.push(obj); | ||
objectsCreated++; | ||
}, | ||
mutatedObjects: function mutatedObjects() { | ||
getMutatedObjects: function getMutatedObjects() { | ||
return currMutatedObjects; | ||
}, | ||
getObjectsCreatedCount: function getObjectsCreatedCount() { | ||
return objectsCreated; | ||
}, | ||
close: function close() { | ||
currMutatedObjects.forEach(removeCanMutateTag); | ||
if (previousSessionStack.length) { | ||
currMutatedObjects = previousSessionStack.pop(); | ||
} else { | ||
currMutatedObjects = null; | ||
if (currMutatedObjects !== null) { | ||
currMutatedObjects.forEach(removeCanMutateTag); | ||
if (previousSessionStack.length) { | ||
currMutatedObjects = previousSessionStack.pop(); | ||
} else { | ||
currMutatedObjects = null; | ||
} | ||
objectsCreated = 0; | ||
} | ||
@@ -451,9 +460,8 @@ } | ||
var defaultOpts = { | ||
curried: true | ||
curried: true, | ||
batchManager: getBatchManager() | ||
}; | ||
var baseOpts = Object.assign({}, defaultOpts, userOpts || {}); | ||
var opts = Object.assign({ createdObjects: 0 }, defaultOpts, userOpts || {}); | ||
var opts = Object.assign({}, baseOpts, getMutationManager()); | ||
var boundOperations = bindOperationsToOptions(operations, opts); | ||
@@ -464,5 +472,5 @@ | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
opts.open(); | ||
opts.batchManager.open(); | ||
var returnValue = func.apply(null, args); | ||
opts.close(); | ||
opts.batchManager.close(); | ||
return returnValue; | ||
@@ -472,10 +480,21 @@ } | ||
boundOperations.batched = batchWrapper; | ||
boundOperations.batch = (0, _ramdaSrcWrap2['default'])(_ramdaSrc__2['default'], batchWrapper); | ||
boundOperations.createdObjectsCount = function () { | ||
return opts.createdObjects; | ||
}; | ||
boundOperations.getMutatedObjects = opts.batchManager.getMutatedObjects; | ||
boundOperations.__ = _ramdaSrc__2['default']; | ||
boundOperations.open = opts.batchManager.open; | ||
boundOperations.close = opts.batchManager.close; | ||
boundOperations.getBatchManager = getBatchManager; | ||
boundOperations.mutatedObjects = opts.mutatedObjects; | ||
boundOperations.useBatchManager = function (manager) { | ||
opts.batchManager.close(); | ||
opts.batchManager = manager; | ||
boundOperations.open = manager.open; | ||
boundOperations.close = manager.close; | ||
boundOperations.getMutatedObjects = manager.getMutatedObjects; | ||
}; | ||
boundOperations.__ = _ramdaSrc__2['default']; | ||
return boundOperations; | ||
} |
@@ -47,2 +47,17 @@ 'use strict'; | ||
it('useBatchManager', function () { | ||
var myManager = ops.getBatchManager(); | ||
ops.useBatchManager(myManager); | ||
var pusher = ops.push(0); | ||
var batchOperation = ops.batch(function (arr) { | ||
var first = pusher(arr); | ||
expect(ops.getMutatedObjects()).to.equal(myManager.getMutatedObjects()); | ||
expect(ops.getMutatedObjects()[0]).to.equal(first); | ||
return first; | ||
}); | ||
batchOperation([]); | ||
}); | ||
describe('object', function () { | ||
@@ -49,0 +64,0 @@ describe('batched mutations', function () { |
{ | ||
"name": "immutable-ops", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A collection of functions to perform immutable operations on plain JavaScript objects", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -47,3 +47,11 @@ immutable-ops | ||
// Batch mutations | ||
// Run a function batched | ||
batched, | ||
// Wrap a function to be executed as a batch | ||
batch, | ||
// Open a batch | ||
open, | ||
// Close a batch | ||
close, | ||
@@ -115,2 +123,10 @@ // Placeholder for currying. | ||
### open() | ||
Opens a batch session. From this point on, any operations done through the `ops` instance that `open` was called from will be applied mutatively **if** the object it's operating on was created after opening the session. | ||
### close() | ||
Closes the current batch session. | ||
## Object API | ||
@@ -117,0 +133,0 @@ |
@@ -20,5 +20,4 @@ import forOwn from 'lodash/forOwn'; | ||
if (typeof opts.addMutated === 'function') { | ||
opts.addMutated(obj); | ||
} | ||
opts.batchManager.addMutated(obj); | ||
return obj; | ||
@@ -33,5 +32,6 @@ } | ||
function prepareNewObject(opts, instance) { | ||
if (opts.isWithMutations()) { | ||
if (opts.batchManager.isWithMutations()) { | ||
addCanMutateTag(opts, instance); | ||
} | ||
opts.createdObjects++; | ||
return instance; | ||
@@ -388,5 +388,6 @@ } | ||
function getMutationManager() { | ||
function getBatchManager() { | ||
const previousSessionStack = []; | ||
let currMutatedObjects = null; | ||
let objectsCreated = 0; | ||
@@ -407,14 +408,22 @@ return { | ||
currMutatedObjects.push(obj); | ||
objectsCreated++; | ||
}, | ||
mutatedObjects() { | ||
getMutatedObjects() { | ||
return currMutatedObjects; | ||
}, | ||
getObjectsCreatedCount() { | ||
return objectsCreated; | ||
}, | ||
close() { | ||
currMutatedObjects.forEach(removeCanMutateTag); | ||
if (previousSessionStack.length) { | ||
currMutatedObjects = previousSessionStack.pop(); | ||
} else { | ||
currMutatedObjects = null; | ||
if (currMutatedObjects !== null) { | ||
currMutatedObjects.forEach(removeCanMutateTag); | ||
if (previousSessionStack.length) { | ||
currMutatedObjects = previousSessionStack.pop(); | ||
} else { | ||
currMutatedObjects = null; | ||
} | ||
objectsCreated = 0; | ||
} | ||
@@ -428,8 +437,7 @@ }, | ||
curried: true, | ||
batchManager: getBatchManager(), | ||
}; | ||
const baseOpts = Object.assign({}, defaultOpts, (userOpts || {})); | ||
const opts = Object.assign({ createdObjects: 0 }, defaultOpts, (userOpts || {})); | ||
const opts = Object.assign({}, baseOpts, getMutationManager()); | ||
const boundOperations = bindOperationsToOptions(operations, opts); | ||
@@ -440,5 +448,5 @@ | ||
const args = Array.prototype.slice.call(arguments, 1); | ||
opts.open(); | ||
opts.batchManager.open(); | ||
const returnValue = func.apply(null, args); | ||
opts.close(); | ||
opts.batchManager.close(); | ||
return returnValue; | ||
@@ -448,10 +456,19 @@ } | ||
boundOperations.batched = batchWrapper; | ||
boundOperations.batch = wrap(placeholder, batchWrapper); | ||
boundOperations.createdObjectsCount = () => opts.createdObjects; | ||
boundOperations.getMutatedObjects = opts.batchManager.getMutatedObjects; | ||
boundOperations.__ = placeholder; | ||
boundOperations.open = opts.batchManager.open; | ||
boundOperations.close = opts.batchManager.close; | ||
boundOperations.getBatchManager = getBatchManager; | ||
boundOperations.mutatedObjects = opts.mutatedObjects; | ||
boundOperations.useBatchManager = manager => { | ||
opts.batchManager.close(); | ||
opts.batchManager = manager; | ||
boundOperations.open = manager.open; | ||
boundOperations.close = manager.close; | ||
boundOperations.getMutatedObjects = manager.getMutatedObjects; | ||
}; | ||
boundOperations.__ = placeholder; | ||
return boundOperations; | ||
} |
@@ -29,2 +29,17 @@ import chai from 'chai'; | ||
it('useBatchManager', () => { | ||
const myManager = ops.getBatchManager(); | ||
ops.useBatchManager(myManager); | ||
const pusher = ops.push(0); | ||
const batchOperation = ops.batch((arr) => { | ||
const first = pusher(arr); | ||
expect(ops.getMutatedObjects()).to.equal(myManager.getMutatedObjects()); | ||
expect(ops.getMutatedObjects()[0]).to.equal(first); | ||
return first; | ||
}); | ||
batchOperation([]); | ||
}); | ||
describe('object', () => { | ||
@@ -31,0 +46,0 @@ describe('batched mutations', () => { |
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
64588
1380
317