@netsells/catch-continue
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "@netsells/catch-continue", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -19,2 +19,3 @@ import CatchContinue from '../src/index'; | ||
let fooArgs; | ||
let barArgs; | ||
@@ -32,6 +33,15 @@ class Methods { | ||
wrap() { | ||
this.cc.wrap(this).foo('bar', 8); | ||
bar(...args) { | ||
barArgs = args; | ||
return new Promise(r => setTimeout(r, 1)); | ||
} | ||
async wrap() { | ||
const wrapper = this.cc.wrap(this); | ||
wrapper.foo('bar', 8); | ||
wrapper.bar('bam', 9); | ||
} | ||
async run() { | ||
@@ -44,2 +54,3 @@ await this.cc.run(); | ||
fooArgs = null; | ||
barArgs = null; | ||
methods = new Methods(); | ||
@@ -51,4 +62,5 @@ | ||
it('can be used to wrap instance methods and delay running them until run called', () => { | ||
expect(methods.cc.segments).toHaveLength(1); | ||
expect(methods.cc.segments).toHaveLength(2); | ||
expect(fooArgs).toBe(null); | ||
expect(barArgs).toBe(null); | ||
}); | ||
@@ -63,2 +75,3 @@ | ||
expect(fooArgs).toEqual(['bar', 8]); | ||
expect(barArgs).toEqual(['bam', 9]); | ||
}); | ||
@@ -108,3 +121,3 @@ }); | ||
it('can be used to wrap instance methods and delay running them until run called', () => { | ||
expect(methods.cc.segments).toHaveLength(2); | ||
expect(methods.cc.segments).toHaveLength(1); | ||
expect(fooArgs).toBe(null); | ||
@@ -126,2 +139,10 @@ expect(barArgs).toBe(null); | ||
describe('add', () => { | ||
it('will return the segment ID', () => { | ||
expect(cc.add(() => {})).toBe(0); | ||
expect(cc.add(() => {})).toBe(1); | ||
expect(cc.add(() => {})).toBe(2); | ||
}); | ||
}); | ||
describe('with a single error', () => { | ||
@@ -128,0 +149,0 @@ beforeEach(() => { |
@@ -14,21 +14,21 @@ /** | ||
/** | ||
* Wrap a class instance to generate a class continue for every function | ||
* call. Uses proxies to store each call until the segments are ran. | ||
* Convert a chained method to a function method and append it to a segment. | ||
* | ||
* @param {Function} funcInstance | ||
* @private | ||
* @param {Function} segmentId | ||
* @returns {Proxy} | ||
*/ | ||
asyncWrap(funcInstance) { | ||
wrapSegment(segmentId) { | ||
return new Proxy({}, { | ||
get: (_, prop) => { | ||
return (...args) => { | ||
let retVal; | ||
const wrappedFunc = this.segments[segmentId]; | ||
this.add(async () => { | ||
const instance = funcInstance(); | ||
this.segments[segmentId] = async () => { | ||
const instance = await wrappedFunc(); | ||
retVal = await instance[prop](...args); | ||
}); | ||
return await instance[prop](...args); | ||
}; | ||
return this.asyncWrap(() => retVal); | ||
return this.wrapSegment(segmentId); | ||
}; | ||
@@ -46,3 +46,13 @@ }, | ||
wrap(instance) { | ||
return this.asyncWrap(() => instance); | ||
return new Proxy({}, { | ||
get: (_, prop) => { | ||
return (...args) => { | ||
const segmentId = this.add(async () => { | ||
return await instance[prop](...args); | ||
}); | ||
return this.wrapSegment(segmentId); | ||
}; | ||
}, | ||
}); | ||
} | ||
@@ -54,2 +64,3 @@ | ||
* @param {Function} func | ||
* @returns {number} | ||
*/ | ||
@@ -61,2 +72,4 @@ add(func) { | ||
]; | ||
return this.segments.length - 1; | ||
} | ||
@@ -63,0 +76,0 @@ |
Sorry, the diff of this file is not supported yet
11861
298