@6river/context
Advanced tools
Comparing version 1.0.0-beta.2 to 1.0.0-beta.3
@@ -6,2 +6,3 @@ export declare type ErrorType = Error | null; | ||
error(): ErrorType; | ||
value<K, V>(key: K): V | null; | ||
} | ||
@@ -16,1 +17,2 @@ export declare const cancelledErr: Error; | ||
export declare function withTimeout(parent: Context, timeout: number): [Context, CancelFunc]; | ||
export declare function withValue<K, V>(parent: Context, key: K, value: V): Context; |
@@ -23,2 +23,5 @@ "use strict"; | ||
} | ||
value(key) { | ||
return null; | ||
} | ||
} | ||
@@ -78,8 +81,14 @@ const backgroundCtx = new EmptyCtx(); | ||
function parentCancelCtx(parent) { | ||
if (parent instanceof CancelCtx) { | ||
return parent; | ||
let c = parent; | ||
for (;;) { | ||
if (c instanceof CancelCtx) { | ||
return c; | ||
} | ||
else if (c instanceof ValueCtx) { | ||
c = c.ctx; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
@@ -118,2 +127,5 @@ // removeChild removes a context from its parent. | ||
} | ||
value(key) { | ||
return this.parent.value(key); | ||
} | ||
// cancel resolves ctx.done() promise, cancels each of c's children, and, if | ||
@@ -214,2 +226,34 @@ // removeFromParent is true, removes ctx from its parent's children. | ||
} | ||
// A ValueCtx carries a key-value pair. | ||
// It implements Value for that key and delegates all other calls to the embedded Context. | ||
class ValueCtx { | ||
constructor(ctx, key, val) { | ||
this.ctx = ctx; | ||
this.key = key; | ||
this.val = val; | ||
} | ||
deadline() { | ||
return this.ctx.deadline(); | ||
} | ||
done() { | ||
return this.ctx.done(); | ||
} | ||
error() { | ||
return this.ctx.error(); | ||
} | ||
value(key) { | ||
if (this.key === key) { | ||
return (this.val); | ||
} | ||
return this.ctx.value(key); | ||
} | ||
} | ||
// withValue returns a copy of parent in which the value associated with key is val. | ||
// | ||
// Use context Values only for request-scoped data that transits processes and | ||
// APIs, not for passing optional parameters to functions. | ||
function withValue(parent, key, value) { | ||
return new ValueCtx(parent, key, value); | ||
} | ||
exports.withValue = withValue; | ||
//# sourceMappingURL=context.js.map |
@@ -140,3 +140,13 @@ "use strict"; | ||
}); | ||
describe('withValue', function () { | ||
it('should create context with embedded value accessible by the key', function () { | ||
const root = context_1.withValue(context_1.background(), 'foo', 'bar'); | ||
const [child, cancelChild] = context_1.withCancel(root); | ||
const grandChild = context_1.withValue(child, 'baz', 0); | ||
const [grandGrandChild, cancelGrandGrand] = context_1.withTimeout(grandChild, 10000); | ||
expect(grandGrandChild.value('baz')).to.equal(0); | ||
expect(grandGrandChild.value('foo')).to.equal('bar'); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=context.test.js.map |
@@ -50,3 +50,3 @@ { | ||
"types": "./dist/src/context.d.ts", | ||
"version": "1.0.0-beta.2" | ||
"version": "1.0.0-beta.3" | ||
} |
@@ -28,2 +28,11 @@ export type ErrorType = Error | null; | ||
error(): ErrorType; | ||
// value returns the value associated with this context for key, or null | ||
// if no value is associated with key. Successive calls to Value with | ||
// the same key returns the same result. | ||
// | ||
// Use context values only for request-scoped data that transits | ||
// processes and API boundaries, not for passing optional parameters to | ||
// functions. | ||
value<K, V>(key: K): V | null; | ||
} | ||
@@ -57,2 +66,7 @@ | ||
} | ||
value<K, V>(key: K): V | null { | ||
return null; | ||
} | ||
} | ||
@@ -131,6 +145,12 @@ | ||
function parentCancelCtx(parent: Context): CancelCtx | null { | ||
if (parent instanceof CancelCtx) { | ||
return parent; | ||
} else { | ||
return null; | ||
let c = parent; | ||
for (;;) { | ||
if (c instanceof CancelCtx) { | ||
return c; | ||
} else if (c instanceof ValueCtx) { | ||
c = c.ctx; | ||
} else { | ||
return null; | ||
} | ||
} | ||
@@ -179,2 +199,6 @@ } | ||
value<K, V>(key: K): V | null { | ||
return this.parent.value<K, V>(key); | ||
} | ||
// cancel resolves ctx.done() promise, cancels each of c's children, and, if | ||
@@ -290,1 +314,34 @@ // removeFromParent is true, removes ctx from its parent's children. | ||
} | ||
// A ValueCtx carries a key-value pair. | ||
// It implements Value for that key and delegates all other calls to the embedded Context. | ||
class ValueCtx implements Context { | ||
constructor(public ctx: Context, private key: any, private val: any) {} | ||
deadline(): Date | null { | ||
return this.ctx.deadline(); | ||
} | ||
done(): Promise<void> { | ||
return this.ctx.done(); | ||
} | ||
error(): ErrorType { | ||
return this.ctx.error(); | ||
} | ||
value<K, V>(key: K): V | null { | ||
if (this.key === key) { | ||
return <V>(this.val); | ||
} | ||
return this.ctx.value<K, V>(key); | ||
} | ||
} | ||
// withValue returns a copy of parent in which the value associated with key is val. | ||
// | ||
// Use context Values only for request-scoped data that transits processes and | ||
// APIs, not for passing optional parameters to functions. | ||
export function withValue<K, V>(parent: Context, key: K, value: V): Context { | ||
return new ValueCtx(parent, key, value); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
38458
706