@wry/context
Advanced tools
Comparing version 0.1.1 to 0.1.2
/// <reference types="node" /> | ||
export declare class Slot<TValue> { | ||
hasValue(): boolean; | ||
getValue(): TValue | undefined; | ||
withValue<TResult>(value: TValue, callback: () => TResult): TResult; | ||
constructor(); | ||
hasValue: () => boolean; | ||
getValue: () => TValue | undefined; | ||
withValue: <TResult>(value: TValue, callback: () => TResult) => TResult; | ||
} | ||
@@ -7,0 +8,0 @@ export declare function bind<TArgs extends any[], TResult>(callback: (...args: TArgs) => TResult): (...args: TArgs) => TResult; |
var currentContext = null; | ||
var slotIdMap = new WeakMap(); | ||
// Pull down the prototype methods that we use onto the slotIdMap instance | ||
// so that they can't be tampered with by malicious code. | ||
slotIdMap.set = slotIdMap.set; | ||
slotIdMap.get = slotIdMap.get; | ||
var nextSlotId = 1; | ||
// Returns the ID of the given slot if the slot has a value defined. | ||
function lookup(slot) { | ||
var slotId = slotIdMap.get(slot); | ||
for (var context_1 = currentContext; context_1; context_1 = context_1.parent) { | ||
// We use the Slot object iself as a key to its value, which means the | ||
// value cannot be obtained without a reference to the Slot object. | ||
if (slotId in context_1.slots) { | ||
if (context_1 !== currentContext) { | ||
// Cache the value in currentContext.slots so the next lookup will | ||
// be faster. This caching is safe because the tree of contexts and | ||
// the values of the slots are logically immutable. | ||
currentContext.slots[slotId] = context_1.slots[slotId]; | ||
} | ||
return slotId; | ||
} | ||
} | ||
} | ||
var Slot = /** @class */ (function () { | ||
function Slot() { | ||
var _this = this; | ||
this.hasValue = function () { return !!lookup(_this); }; | ||
this.getValue = function () { | ||
var slotId = lookup(_this); | ||
if (slotId) { | ||
return currentContext.slots[slotId]; | ||
} | ||
}; | ||
this.withValue = function (value, callback) { | ||
var _a; | ||
var slots = (_a = { | ||
__proto__: null | ||
}, | ||
_a[slotIdMap.get(_this)] = value, | ||
_a); | ||
currentContext = { parent: currentContext, slots: slots }; | ||
try { | ||
return callback(); | ||
} | ||
finally { | ||
currentContext = currentContext.parent; | ||
} | ||
}; | ||
slotIdMap.set(this, nextSlotId++); | ||
} | ||
Slot.prototype.hasValue = function () { | ||
for (var context_1 = currentContext; context_1; context_1 = context_1.parent) { | ||
// We use the Slot object iself as a key to its value, which means the | ||
// value cannot be obtained without a reference to the Slot object. | ||
if (context_1.slots.has(this)) { | ||
if (context_1 !== currentContext) { | ||
// Cache the value in currentContext.slots so the next lookup will | ||
// be faster. This caching is safe because the tree of contexts and | ||
// the values of the slots are logically immutable. | ||
currentContext.slots.set(this, context_1.slots.get(this)); | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
Slot.prototype.getValue = function () { | ||
if (this.hasValue()) { | ||
return currentContext.slots.get(this); | ||
} | ||
}; | ||
Slot.prototype.withValue = function (value, callback) { | ||
var parent = currentContext; | ||
var slots = new WeakMap(); | ||
slots.set(this, value); | ||
currentContext = { parent: parent, slots: slots }; | ||
try { | ||
return callback(); | ||
} | ||
finally { | ||
currentContext = parent; | ||
} | ||
}; | ||
return Slot; | ||
@@ -39,0 +53,0 @@ }()); |
@@ -6,38 +6,52 @@ 'use strict'; | ||
var currentContext = null; | ||
var slotIdMap = new WeakMap(); | ||
// Pull down the prototype methods that we use onto the slotIdMap instance | ||
// so that they can't be tampered with by malicious code. | ||
slotIdMap.set = slotIdMap.set; | ||
slotIdMap.get = slotIdMap.get; | ||
var nextSlotId = 1; | ||
// Returns the ID of the given slot if the slot has a value defined. | ||
function lookup(slot) { | ||
var slotId = slotIdMap.get(slot); | ||
for (var context_1 = currentContext; context_1; context_1 = context_1.parent) { | ||
// We use the Slot object iself as a key to its value, which means the | ||
// value cannot be obtained without a reference to the Slot object. | ||
if (slotId in context_1.slots) { | ||
if (context_1 !== currentContext) { | ||
// Cache the value in currentContext.slots so the next lookup will | ||
// be faster. This caching is safe because the tree of contexts and | ||
// the values of the slots are logically immutable. | ||
currentContext.slots[slotId] = context_1.slots[slotId]; | ||
} | ||
return slotId; | ||
} | ||
} | ||
} | ||
var Slot = /** @class */ (function () { | ||
function Slot() { | ||
var _this = this; | ||
this.hasValue = function () { return !!lookup(_this); }; | ||
this.getValue = function () { | ||
var slotId = lookup(_this); | ||
if (slotId) { | ||
return currentContext.slots[slotId]; | ||
} | ||
}; | ||
this.withValue = function (value, callback) { | ||
var _a; | ||
var slots = (_a = { | ||
__proto__: null | ||
}, | ||
_a[slotIdMap.get(_this)] = value, | ||
_a); | ||
currentContext = { parent: currentContext, slots: slots }; | ||
try { | ||
return callback(); | ||
} | ||
finally { | ||
currentContext = currentContext.parent; | ||
} | ||
}; | ||
slotIdMap.set(this, nextSlotId++); | ||
} | ||
Slot.prototype.hasValue = function () { | ||
for (var context_1 = currentContext; context_1; context_1 = context_1.parent) { | ||
// We use the Slot object iself as a key to its value, which means the | ||
// value cannot be obtained without a reference to the Slot object. | ||
if (context_1.slots.has(this)) { | ||
if (context_1 !== currentContext) { | ||
// Cache the value in currentContext.slots so the next lookup will | ||
// be faster. This caching is safe because the tree of contexts and | ||
// the values of the slots are logically immutable. | ||
currentContext.slots.set(this, context_1.slots.get(this)); | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
Slot.prototype.getValue = function () { | ||
if (this.hasValue()) { | ||
return currentContext.slots.get(this); | ||
} | ||
}; | ||
Slot.prototype.withValue = function (value, callback) { | ||
var parent = currentContext; | ||
var slots = new WeakMap(); | ||
slots.set(this, value); | ||
currentContext = { parent: parent, slots: slots }; | ||
try { | ||
return callback(); | ||
} | ||
finally { | ||
currentContext = parent; | ||
} | ||
}; | ||
return Slot; | ||
@@ -44,0 +58,0 @@ }()); |
{ | ||
"name": "@wry/context", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"author": "Ben Newman <ben@eloper.dev>", | ||
@@ -31,3 +31,3 @@ "description": "Manage contextual information needed by (a)synchronous tasks without explicitly passing objects around", | ||
}, | ||
"gitHead": "062187b78037c7b83f27dacec81ac1001872fbd9" | ||
"gitHead": "3638614f776645eca20fd5630361f226aecd3f60" | ||
} |
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
15371
302