Skip to content

Commit e9b2d82

Browse files
committed
Remove call function
1 parent 10e27aa commit e9b2d82

File tree

10 files changed

+18
-23
lines changed

10 files changed

+18
-23
lines changed

core.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const parse = (expr) => {
113113
try {
114114
let result = fn?.call(this, state)
115115
// if cb is given (to handle async/await exprs, usually directive update) - call it with result and return a cleanup function
116-
if (cb) return result?.then ? (result.then(v => _out = cb(v)), () => _out && call(_out)) : cb(result)
116+
if (cb) return result?.then ? (result.then(v => _out = cb(v)), () => typeof _out === 'function' && _out()) : cb(result)
117117
else return result
118118
} catch (e) {
119119
console.error(`∴ ${e}\n\n${currentDir}="${expr}"`)
@@ -178,9 +178,6 @@ export const frag = (tpl) => {
178178
}
179179
}
180180

181-
// if value is function - return result of its call
182-
export const call = (v, arg) => typeof v === 'function' ? v(arg) : v
183-
184181
// camel to kebab
185182
export const dashcase = (str) => str.replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g, (match, i) => (i ? '-' : '') + match.toLowerCase());
186183

directive/_.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { attr, call } from "../core.js";
1+
import { attr } from "../core.js";
22

3-
export default (el, st, ex, name) => v => attr(el, name, v && call(v, el.getAttribute(name)))
3+
export default (el, st, ex, name) => v => attr(el, name, typeof v === 'function' ? v(el.getAttribute(name)) : v)

directive/class.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { clsx, call, decorate } from "../core.js";
1+
import { clsx, decorate } from "../core.js";
22

33
export default (el, st, ex, name) => {
44
let _cur = new Set, _new
@@ -8,7 +8,7 @@ export default (el, st, ex, name) => {
88

99
return (v) => {
1010
_new = new Set
11-
if (v) clsx(call(v, el.className)).split(' ').map(c => c && _new.add(c))
11+
if (v) clsx(typeof v === 'function' ? v(el.className) : v).split(' ').map(c => c && _new.add(c))
1212
for (let c of _cur) if (_new.has(c)) _new.delete(c); else el.classList.remove(c);
1313
for (let c of _cur = _new) el.classList.add(c)
1414
}

directive/event.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { call, parse, decorate } from "../core.js"
1+
import { parse, decorate } from "../core.js"
22

33
export default (el, state, expr, name) => {
44
// wrap inline cb into function
55
// if (!/^(?:[\w$]+|\([^()]*\))\s*=>/.test(expr) && !/^function\b/.test(expr)) expr = `()=>{${expr}}`;
66

77
const [type, ...mods] = name.slice(2).split('.'),
88
evaluate = parse(expr).bind(el),
9-
trigger = decorate(Object.assign(e => evaluate(state, (fn) => fn && call(fn, e)), { target: el }), mods);
9+
trigger = decorate(Object.assign(e => evaluate(state, (fn) => typeof fn === 'function' ? fn(e) : fn), { target: el }), mods);
1010

1111
trigger.target.addEventListener(type, trigger, trigger)
1212
return {

directive/fx.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
import { call } from "../core.js"
2-
3-
export default () => call
1+
export default () => (fn) => typeof fn === 'function' && fn()

directive/scope.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sprae, { store, call, untracked, _state, _signals, signal } from '../core.js'
1+
import sprae, { store, untracked, _state, _signals, signal } from '../core.js'
22

33
export default (el, rootState) => {
44
// 0 run pre-creates state to provide scope for the first effect - it can write vars in it, so we should already have it
@@ -8,7 +8,7 @@ export default (el, rootState) => {
88
// 1st run spraes subtree with values from scope - it can be postponed by modifiers (we isolate reads from parent effect)
99
// 2nd+ runs update subscope
1010
return values => {
11-
values = call(values, state);
11+
values = typeof values === 'function' ? values(state) : values;
1212

1313
// we bind to subscope to alleviate friction using scope method directly
1414
// also returned props should force-create signals in subscope, not overwriting parent

directive/sequence.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// events directive with enabled aliases and sequences like :onclick.ctrl.once..keyup.enter
2-
import { _dispose, call, parse, decorate } from "../core.js"
2+
import { _dispose, parse, decorate } from "../core.js"
33

44
export default (el, state, expr, names) => {
55
let cur, // current step callback
@@ -12,7 +12,7 @@ export default (el, state, expr, names) => {
1212
const evaluate = parse(expr).bind(el)
1313

1414
const trigger = decorate(Object.assign(
15-
e => (!i ? evaluate(state, (fn) => cur = fn && call(fn, e)) : (cur = cur(e)), off(), off = steps[(i + 1) % length]()),
15+
e => (!i ? evaluate(state, (fn) => cur = typeof fn === 'function' ? fn(e) : fn) : (cur = cur(e)), off(), off = steps[(i + 1) % length]()),
1616
{ target: el }
1717
), mods)
1818

directive/style.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { call, attr, decorate } from "../core.js";
1+
import { attr, decorate } from "../core.js";
22

33
export default (el, st, ex, name) => {
44
let _static;
@@ -8,7 +8,7 @@ export default (el, st, ex, name) => {
88

99
return v => {
1010
if (!_static) { _static = el.getAttribute("style") }
11-
if (v) v = call(v, el.style)
11+
v = typeof v === "function" ? v(el.style) : v
1212
if (typeof v === "string") attr(el, "style", _static + '; ' + v);
1313
else {
1414
if (_static) attr(el, "style", _static);

directive/text.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { frag, call } from "../core.js"
1+
import { frag } from "../core.js"
22

33
export default el => (
44
// <template :text="a"/> or previously initialized template
55
el.content && el.replaceWith(el = frag(el).childNodes[0]),
6-
v => (v = v && call(v, el.textContent), el.textContent = v == null ? "" : v)
6+
v => (v = typeof v === 'function' ? v(el.textContent) : v, el.textContent = v == null ? "" : v)
77
)

sprae.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import store from "./store.js";
22
import { batch, computed, effect, signal, untracked } from './core.js';
33
import * as signals from './signal.js';
4-
import sprae, { use, decorate, directive, modifier, parse, throttle, debounce, _off, _state, _on, _dispose, _add, call } from './core.js';
4+
import sprae, { use, decorate, directive, modifier, parse, throttle, debounce, _off, _state, _on, _dispose, _add } from './core.js';
55
import pkg from './package.json' with { type: 'json' };
66

77
import _if from "./directive/if.js";
@@ -53,7 +53,7 @@ const dir = (target, name, expr, state) => {
5353
change = signal(0), // signal authorized to trigger effect: 0 = init; >0 = trigger
5454
count = 0, // called effect count
5555
evaluate = update.eval ?? parse(expr).bind(target),
56-
_out, out = () => (_out && call(_out), _out=null) // effect trigger and invoke may happen in the same tick, so it will be effect-within-effect call - we need to store output of evaluate to return from trigger effect
56+
_out, out = () => (typeof _out === 'function' && _out(), _out=null) // effect trigger and invoke may happen in the same tick, so it will be effect-within-effect call - we need to store output of evaluate to return from trigger effect
5757

5858
state = target[_state] ?? state
5959

0 commit comments

Comments
 (0)