Skip to content

Commit 3889ed7

Browse files
johnsoncodehkMox93AimWhy
authored
v2.0.0 code refactor (#61)
Co-authored-by: Mohamed Ragaiy <mohamed.ragaiy.saleh@gmail.com> Co-authored-by: 王洪莹 <565644124@qq.com>
1 parent 3e6993d commit 3889ed7

5 files changed

Lines changed: 425 additions & 569 deletions

File tree

README.md

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -107,56 +107,40 @@ This results in code that is difficult to understand, and you don't necessarily
107107
#### `propagate`
108108

109109
```ts
110-
function propagate(link: Link, targetFlag = SubscriberFlags.Dirty): void {
110+
function propagate(link: Link): void {
111111
do {
112112
const sub = link.sub;
113-
const subFlags = sub.flags;
114-
115-
let shouldNotify = false;
116-
117-
if (!(subFlags & (SubscriberFlags.Tracking | SubscriberFlags.Recursed | SubscriberFlags.Propagated))) {
118-
sub.flags = subFlags | targetFlag | SubscriberFlags.Notified;
119-
shouldNotify = true;
120-
} else if ((subFlags & SubscriberFlags.Recursed) && !(subFlags & SubscriberFlags.Tracking)) {
121-
sub.flags = (subFlags & ~SubscriberFlags.Recursed) | targetFlag | SubscriberFlags.Notified;
122-
shouldNotify = true;
123-
} else if (!(subFlags & SubscriberFlags.Propagated) && isValidLink(current, sub)) {
124-
sub.flags = subFlags | SubscriberFlags.Recursed | targetFlag | SubscriberFlags.Notified;
125-
shouldNotify = (sub as Dependency).subs !== undefined;
126-
}
127113

128-
if (shouldNotify) {
129-
const subSubs = (sub as Dependency).subs;
130-
if (subSubs !== undefined) {
131-
propagate(
132-
subSubs,
133-
subFlags & SubscriberFlags.Effect
134-
? SubscriberFlags.PendingEffect
135-
: SubscriberFlags.PendingComputed
136-
);
137-
}
138-
if (subFlags & SubscriberFlags.Effect) {
139-
if (queuedEffectsTail !== undefined) {
140-
queuedEffectsTail = queuedEffectsTail.linked = { target: sub, linked: undefined };
114+
let flags = sub.flags;
115+
116+
if (flags & (ReactiveFlags.Mutable | ReactiveFlags.Watching)) {
117+
if (!(flags & (ReactiveFlags.RecursedCheck | ReactiveFlags.Recursed | ReactiveFlags.Dirty | ReactiveFlags.Pending))) {
118+
sub.flags = flags | ReactiveFlags.Pending;
119+
} else if (!(flags & (ReactiveFlags.RecursedCheck | ReactiveFlags.Recursed))) {
120+
flags = ReactiveFlags.None;
121+
} else if (!(flags & ReactiveFlags.RecursedCheck)) {
122+
sub.flags = (flags & ~ReactiveFlags.Recursed) | ReactiveFlags.Pending;
123+
} else if (isValidLink(link, sub)) {
124+
if (!(flags & (ReactiveFlags.Dirty | ReactiveFlags.Pending))) {
125+
sub.flags = flags | ReactiveFlags.Recursed | ReactiveFlags.Pending;
126+
flags &= ReactiveFlags.Mutable;
141127
} else {
142-
queuedEffectsTail = queuedEffects = { target: sub, linked: undefined };
128+
flags = ReactiveFlags.None;
143129
}
130+
} else {
131+
flags = ReactiveFlags.None;
144132
}
145-
} else if (!(subFlags & (SubscriberFlags.Tracking | targetFlag))) {
146-
sub.flags = subFlags | targetFlag | SubscriberFlags.Notified;
147-
if ((subFlags & (SubscriberFlags.Effect | SubscriberFlags.Notified)) === SubscriberFlags.Effect) {
148-
if (queuedEffectsTail !== undefined) {
149-
queuedEffectsTail = queuedEffectsTail.linked = { target: sub, linked: undefined };
150-
} else {
151-
queuedEffectsTail = queuedEffects = { target: sub, linked: undefined };
133+
134+
if (flags & ReactiveFlags.Watching) {
135+
notify(sub);
136+
}
137+
138+
if (flags & ReactiveFlags.Mutable) {
139+
const subSubs = sub.subs;
140+
if (subSubs !== undefined) {
141+
propagate(subSubs);
152142
}
153143
}
154-
} else if (
155-
!(subFlags & targetFlag)
156-
&& (subFlags & SubscriberFlags.Propagated)
157-
&& isValidLink(link, sub)
158-
) {
159-
sub.flags = subFlags | targetFlag;
160144
}
161145

162146
link = link.nextSub!;
@@ -167,33 +151,35 @@ function propagate(link: Link, targetFlag = SubscriberFlags.Dirty): void {
167151
#### `checkDirty`
168152

169153
```ts
170-
function checkDirty(link: Link): boolean {
154+
function checkDirty(link: Link, sub: ReactiveNode): boolean {
171155
do {
172156
const dep = link.dep;
173-
if ('flags' in dep) {
174-
const depFlags = dep.flags;
175-
if ((depFlags & (SubscriberFlags.Computed | SubscriberFlags.Dirty)) === (SubscriberFlags.Computed | SubscriberFlags.Dirty)) {
176-
if (updateComputed(dep)) {
157+
const depFlags = dep.flags;
158+
159+
if (sub.flags & ReactiveFlags.Dirty) {
160+
return true;
161+
} else if ((depFlags & (ReactiveFlags.Mutable | ReactiveFlags.Dirty)) === (ReactiveFlags.Mutable | ReactiveFlags.Dirty)) {
162+
if (update(dep)) {
163+
const subs = dep.subs!;
164+
if (subs.nextSub !== undefined) {
165+
shallowPropagate(subs);
166+
}
167+
return true;
168+
}
169+
} else if ((depFlags & (ReactiveFlags.Mutable | ReactiveFlags.Pending)) === (ReactiveFlags.Mutable | ReactiveFlags.Pending)) {
170+
if (checkDirty(dep.deps!, dep)) {
171+
if (update(dep)) {
177172
const subs = dep.subs!;
178173
if (subs.nextSub !== undefined) {
179174
shallowPropagate(subs);
180175
}
181176
return true;
182177
}
183-
} else if ((depFlags & (SubscriberFlags.Computed | SubscriberFlags.PendingComputed)) === (SubscriberFlags.Computed | SubscriberFlags.PendingComputed)) {
184-
if (checkDirty(dep.deps!)) {
185-
if (updateComputed(dep)) {
186-
const subs = dep.subs!;
187-
if (subs.nextSub !== undefined) {
188-
shallowPropagate(subs);
189-
}
190-
return true;
191-
}
192-
} else {
193-
dep.flags = depFlags & ~SubscriberFlags.PendingComputed;
194-
}
178+
} else {
179+
dep.flags = depFlags & ~ReactiveFlags.Pending;
195180
}
196181
}
182+
197183
link = link.nextDep!;
198184
} while (link !== undefined);
199185

0 commit comments

Comments
 (0)