Skip to content

Commit

Permalink
Refactor and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisPower1 committed Feb 24, 2023
1 parent 146f3e0 commit b559426
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
25 changes: 12 additions & 13 deletions core/ref/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export function Ref(obj) {
for (const refName in data) {
if (reservedRefNames.has(refName)) {
runReservedRefNameWarning(refName);
delete data[refName];

continue;
}
Expand Down Expand Up @@ -285,23 +286,25 @@ export function Ref(obj) {

runRefParsing(getId(IN), proxyTarget, refParser);

function runObserveCallBack(refName, value, oldValue) {
if (refParser.observed.size == 1 && !reservedRefNames.has(refName)) {
const callBack = refParser.observed.get("callBack");

callBack(refName, value, oldValue);
}
}

const reactor = new Proxy(proxyTarget, {
set(target, key, value, proxy) {
if (target[key] == value) return false;
if (key in target && target[key] == value) return false;

const oldValue = target[key];

if (isCallable(value)) {
value = value.call(proxy);
}
Reflect.set(...arguments);

if (refParser.observed.size == 1) {
const callBack = refParser.observed.get("callBack");

callBack(key, value, oldValue);
}

runObserveCallBack(key, value, oldValue);
if (!(key in proxy)) {
// Dynamic ref.

Expand Down Expand Up @@ -338,11 +341,7 @@ export function Ref(obj) {
proxyTarget[refName] = refValue;
}

if (refParser.observed.size == 1) {
const callBack = refParser.observed.get("callBack");

callBack(refName, refValue, oldRefValue);
}
runObserveCallBack(refName, refValue, oldRefValue);
}
} else runInvalidSetRefsValueError(o);
},
Expand Down
2 changes: 1 addition & 1 deletion core/renderif/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function runInvalidSetCondsValueError(arg) {
export function runNotDefinedIfPropWarning(propValue, child, data) {
ParserWarning(`
The conditional rendering parser found
an element wich has the "_if" attribute and the value
an element which has the "_if" attribute and the value
of this attribute is not a conditional property.
{
Expand Down
19 changes: 14 additions & 5 deletions core/renderif/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,14 @@ function runRenderingSystem(cache /*Set*/, data) {
}
}

function runObserveCallBack(prop, value) {
if (observer.size == 1) {
const callBack = observer.get("callBack");

callBack(prop, value);
}
}

const reservedProps = new Set(["setConds", "observe"]);
const observer = new Map();
const proxyTarget = Object.assign({}, data);
Expand All @@ -456,6 +464,7 @@ function runRenderingSystem(cache /*Set*/, data) {

const reactor = new Proxy(proxyTarget, {
set(target, prop, value) {
if (prop in target && target[prop] == value) return false;
if (!(prop in data) && !reservedProps.has(prop)) {
runNotDefinedConditionalPropWarning(prop);

Expand All @@ -473,11 +482,7 @@ function runRenderingSystem(cache /*Set*/, data) {
if (!reservedProps.has(prop)) {
checkWhatToRender(proxyTarget, prop);

if (observer.size == 1) {
const callBack = observer.get("callBack");

callBack(prop, value);
}
runObserveCallBack(prop, value);
}

return true;
Expand Down Expand Up @@ -522,7 +527,11 @@ function runRenderingSystem(cache /*Set*/, data) {
if (!hasOwnProperty(this, prop))
runNotDefinedConditionalPropWarning(prop);

if (this[prop] == cond) continue;

proxyTarget[prop] = cond;

runObserveCallBack(prop, cond);
}

checkWhatToRender(proxyTarget);
Expand Down
10 changes: 8 additions & 2 deletions core/renderlist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
import { toDOM } from "../template/index.js";

import {
runIllegalAttrsPropWarning,
runInvalidEventHandlerWarning,
runInvalidEventWarning,
runInvalidStyleWarning,
Expand Down Expand Up @@ -969,7 +970,7 @@ function runAttributeDiffing(target, oldAttributes, newAttributes) {
const oldAttrsArray = Object.keys(oldAttributes),
newAttrsArray = Object.keys(newAttributes),
greater = getGreater(oldAttrsArray, newAttrsArray),
specialAttrs = new Set(["value", "current", "checked"]);
specialAttrs = new Set(["value", "currentTime", "checked"]);

for (let i = 0; greater.length > i; i++) {
const oldAttrName = oldAttrsArray[i],
Expand All @@ -981,7 +982,12 @@ function runAttributeDiffing(target, oldAttributes, newAttributes) {
else if (!isDefined(newAttrValue) || isFalse(newAttrValue))
removeAttr(newAttrName);
else if (isDefined(newAttrValue) && !isFalse(newAttrValue)) {
if (newAttrValue !== oldAttrValue) {
if (
(newAttrName.startsWith("on") && validDomEvent(newAttrName)) ||
newAttrName == "style"
)
runIllegalAttrsPropWarning(newAttrName);
else if (newAttrName !== oldAttrName || newAttrValue !== oldAttrValue) {
if (specialAttrs.has(newAttrName)) target[newAttrName] = newAttrValue;
else target.setAttribute(newAttrName, newAttrValue);
}
Expand Down

0 comments on commit b559426

Please sign in to comment.