wc-context
Advanced tools
Comparing version 0.5.2 to 0.5.3
var orphanMap = {}; | ||
var resolved = Promise.resolve(); | ||
var orphanResolveQueue = { | ||
contexts: new Set(), | ||
running: false, | ||
add: function add(context) { | ||
var _this = this; | ||
this.contexts.add(context); | ||
if (!this.running) { | ||
this.running = true; | ||
resolved.then(function () { | ||
_this.contexts.forEach(function (context) { | ||
var orphans = orphanMap[context]; | ||
orphans.forEach(function (orphan) { | ||
var event = sendContextEvent(orphan, context); | ||
if (event.detail.handled) { | ||
orphans.delete(orphan); | ||
} | ||
}); | ||
}); | ||
_this.contexts.clear(); | ||
_this.running = false; | ||
}); | ||
} | ||
} | ||
}; | ||
function addOrphan(el, name) { | ||
@@ -48,3 +76,3 @@ var orphans = orphanMap[name] || (orphanMap[name] = new Set()); | ||
set: function set(value) { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -55,3 +83,3 @@ var childContext = this.__wcChildContext; | ||
if (childContext[propName] !== propValue) { | ||
updateContext(_this, propName, propValue); | ||
updateContext(_this2, propName, propValue); | ||
} | ||
@@ -72,5 +100,7 @@ childContext[propName] = propValue; | ||
var oldValue = targetEl.__wcContext[name]; | ||
targetEl.__wcContext[name] = value; | ||
if (targetEl.contextChangedCallback) { | ||
targetEl.contextChangedCallback(name, oldValue, value); | ||
if (oldValue !== value) { | ||
targetEl.__wcContext[name] = value; | ||
if (targetEl.contextChangedCallback) { | ||
targetEl.contextChangedCallback(name, oldValue, value); | ||
} | ||
} | ||
@@ -81,8 +111,3 @@ observers.push(targetEl); | ||
if (orphans && orphans.size) { | ||
orphans.forEach(function (orphan) { | ||
var event = sendContextEvent(orphan, name); | ||
if (event.detail.handled) { | ||
orphans.delete(orphan); | ||
} | ||
}); | ||
orphanResolveQueue.add(name); | ||
} | ||
@@ -122,5 +147,7 @@ } | ||
var oldValue = observer.__wcContext[name]; | ||
observer.__wcContext[name] = value; | ||
if (observer.contextChangedCallback) { | ||
observer.contextChangedCallback(name, oldValue, value); | ||
if (oldValue !== value) { | ||
observer.__wcContext[name] = value; | ||
if (observer.contextChangedCallback) { | ||
observer.contextChangedCallback(name, oldValue, value); | ||
} | ||
} | ||
@@ -127,0 +154,0 @@ }); |
{ | ||
"name": "wc-context", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "Simple context for HTML custom elements", | ||
@@ -5,0 +5,0 @@ "repository": "blikblum/wc-context", |
const orphanMap = {} | ||
const resolved = Promise.resolve() | ||
const orphanResolveQueue = { | ||
contexts: new Set(), | ||
running: false, | ||
add (context) { | ||
this.contexts.add(context) | ||
if (!this.running) { | ||
this.running = true | ||
resolved.then(() => { | ||
this.contexts.forEach(context => { | ||
const orphans = orphanMap[context] | ||
orphans.forEach(orphan => { | ||
const event = sendContextEvent(orphan, context) | ||
if (event.detail.handled) { | ||
orphans.delete(orphan) | ||
} | ||
}) | ||
}) | ||
this.contexts.clear() | ||
this.running = false | ||
}) | ||
} | ||
} | ||
} | ||
function addOrphan (el, name) { | ||
@@ -68,5 +94,7 @@ const orphans = orphanMap[name] || (orphanMap[name] = new Set()) | ||
const oldValue = targetEl.__wcContext[name] | ||
targetEl.__wcContext[name] = value | ||
if (targetEl.contextChangedCallback) { | ||
targetEl.contextChangedCallback(name, oldValue, value) | ||
if (oldValue !== value) { | ||
targetEl.__wcContext[name] = value | ||
if (targetEl.contextChangedCallback) { | ||
targetEl.contextChangedCallback(name, oldValue, value) | ||
} | ||
} | ||
@@ -77,8 +105,3 @@ observers.push(targetEl) | ||
if (orphans && orphans.size) { | ||
orphans.forEach(orphan => { | ||
const event = sendContextEvent(orphan, name) | ||
if (event.detail.handled) { | ||
orphans.delete(orphan) | ||
} | ||
}) | ||
orphanResolveQueue.add(name) | ||
} | ||
@@ -118,5 +141,7 @@ } | ||
const oldValue = observer.__wcContext[name] | ||
observer.__wcContext[name] = value | ||
if (observer.contextChangedCallback) { | ||
observer.contextChangedCallback(name, oldValue, value) | ||
if (oldValue !== value) { | ||
observer.__wcContext[name] = value | ||
if (observer.contextChangedCallback) { | ||
observer.contextChangedCallback(name, oldValue, value) | ||
} | ||
} | ||
@@ -123,0 +148,0 @@ }) |
@@ -172,2 +172,8 @@ /* eslint-env jest */ | ||
}) | ||
test('should not notify the observer when context is updated with same value', () => { | ||
callback.mockClear() | ||
updateContext(grandfatherEl, 'key', 'value') | ||
expect(callback).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
@@ -174,0 +180,0 @@ }) |
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
210050
645