@semantic-ui/reactivity
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -9,8 +9,5 @@ { | ||
"dependencies": { | ||
"@semantic-ui/utils": "^0.6.1" | ||
"@semantic-ui/utils": "^0.7.0" | ||
}, | ||
"devDependencies": { | ||
"vitest": "^1.5.2" | ||
}, | ||
"version": "0.6.1" | ||
"version": "0.7.0" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Reaction } from './reaction.js'; | ||
import { Scheduler } from './scheduler.js'; | ||
@@ -9,5 +9,5 @@ export class Dependency { | ||
depend() { | ||
if (Reaction.current) { | ||
this.subscribers.add(Reaction.current); | ||
Reaction.current.dependencies.add(this); | ||
if (Scheduler.current) { | ||
this.subscribers.add(Scheduler.current); | ||
Scheduler.current.dependencies.add(this); | ||
} | ||
@@ -14,0 +14,0 @@ } |
@@ -1,9 +0,4 @@ | ||
import { Signal } from './signal.js'; | ||
import { Reaction } from './reaction.js'; | ||
import { Dependency } from './dependency.js'; | ||
export { | ||
Signal, | ||
Reaction, | ||
Dependency, | ||
}; | ||
export { Signal } from './signal.js'; | ||
export { Reaction } from './reaction.js'; | ||
export { Dependency } from './dependency.js'; | ||
export { Scheduler } from './scheduler.js'; |
@@ -0,1 +1,2 @@ | ||
import { Scheduler } from './scheduler.js'; | ||
import { isEqual, clone } from '@semantic-ui/utils'; | ||
@@ -6,38 +7,2 @@ import { Dependency } from './dependency.js'; | ||
static current = null; | ||
static pendingReactions = new Set(); | ||
static afterFlushCallbacks = []; | ||
static isFlushScheduled = false; | ||
static create(callback) { | ||
const reaction = new Reaction(callback); | ||
reaction.run(); | ||
return reaction; | ||
} | ||
static scheduleFlush() { | ||
if (!Reaction.isFlushScheduled) { | ||
Reaction.isFlushScheduled = true; | ||
if (typeof queueMicrotask === 'function') { | ||
// <https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide> | ||
queueMicrotask(() => Reaction.flush()); | ||
} else { | ||
Promise.resolve().then(() => Reaction.flush()); | ||
} | ||
} | ||
} | ||
static flush() { | ||
Reaction.isFlushScheduled = false; | ||
Reaction.pendingReactions.forEach(reaction => reaction.run()); | ||
Reaction.pendingReactions.clear(); | ||
Reaction.afterFlushCallbacks.forEach(callback => callback()); | ||
Reaction.afterFlushCallbacks = []; | ||
} | ||
static afterFlush(callback) { | ||
Reaction.afterFlushCallbacks.push(callback); | ||
} | ||
constructor(callback) { | ||
@@ -55,3 +20,3 @@ this.callback = callback; | ||
} | ||
Reaction.current = this; | ||
Scheduler.current = this; | ||
this.dependencies.forEach(dep => dep.cleanUp(this)); | ||
@@ -61,4 +26,4 @@ this.dependencies.clear(); | ||
this.firstRun = false; | ||
Reaction.current = null; | ||
Reaction.pendingReactions.delete(this); | ||
Scheduler.current = null; | ||
Scheduler.pendingReactions.delete(this); | ||
} | ||
@@ -71,4 +36,3 @@ | ||
} | ||
Reaction.pendingReactions.add(this); | ||
Reaction.scheduleFlush(); | ||
Scheduler.scheduleReaction(this); | ||
} | ||
@@ -82,23 +46,27 @@ | ||
// Static proxies for developer experience | ||
static get current() { return Scheduler.current; } | ||
static flush = Scheduler.flush; | ||
static scheduleFlush = Scheduler.scheduleFlush; | ||
static afterFlush = Scheduler.afterFlush; | ||
static getSource = Scheduler.getSource; | ||
/* | ||
Makes sure anything called inside this function does not trigger reactions | ||
*/ | ||
static create(callback) { | ||
const reaction = new Reaction(callback); | ||
reaction.run(); | ||
return reaction; | ||
} | ||
static nonreactive(func) { | ||
const previousReaction = Reaction.current; | ||
Reaction.current = null; | ||
const previousReaction = Scheduler.current; | ||
Scheduler.current = null; | ||
try { | ||
return func(); | ||
} finally { | ||
Reaction.current = previousReaction; | ||
Scheduler.current = previousReaction; | ||
} | ||
} | ||
/* | ||
Guard prevents a value from triggering reactions if the value is identical | ||
*/ | ||
static guard(f, equalCheck = isEqual) { | ||
// guard is not necessary if this is not a reactive context | ||
if (!Reaction.current) { | ||
if (!Scheduler.current) { | ||
return f(); | ||
@@ -108,3 +76,3 @@ } | ||
let value, newValue; | ||
dep.depend(); // Create dependency on guard function | ||
dep.depend(); | ||
const comp = new Reaction(() => { | ||
@@ -117,17 +85,5 @@ newValue = f(); | ||
}); | ||
comp.run(); // Initial run to capture dependencies | ||
comp.run(); | ||
return newValue; | ||
} | ||
static getSource() { | ||
if (!Reaction.current || !Reaction.current.context || !Reaction.current.context.trace) { | ||
console.log('No source available or no current reaction.'); | ||
return; | ||
} | ||
let trace = Reaction.current.context.trace; | ||
trace = trace.split('\n').slice(2).join('\n'); | ||
trace = `Reaction triggered by:\n${trace}`; | ||
console.info(trace); | ||
return trace; | ||
} | ||
} |
import { beforeEach, describe, it, expect, vi } from 'vitest'; | ||
import { Signal, Reaction } from '@semantic-ui/reactivity'; | ||
import { Signal, Reaction, Scheduler } from '@semantic-ui/reactivity'; | ||
@@ -8,5 +8,5 @@ describe('Reaction', () => { | ||
// Reset Reaction state before each test if needed | ||
Reaction.current = null; | ||
Reaction.pendingReactions.clear(); | ||
Reaction.afterFlushCallbacks = []; | ||
Scheduler.current = null; | ||
Scheduler.pendingReactions.clear(); | ||
Scheduler.afterFlushCallbacks = []; | ||
}); | ||
@@ -13,0 +13,0 @@ |
0
10
42898
+ Added@semantic-ui/utils@0.7.0(transitive)
- Removed@semantic-ui/utils@0.6.1(transitive)
Updated@semantic-ui/utils@^0.7.0