Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed May 28, 2024
1 parent 93ea44d commit fa8791b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 91 deletions.
179 changes: 91 additions & 88 deletions force-app/lwc/signals/core.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,107 @@
const context = [];
function _getCurrentObserver() {
return context[context.length - 1];
return context[context.length - 1];
}
function $effect(fn) {
const execute = () => {
context.push(execute);
try {
fn();
} finally {
context.pop();
}
};
execute();
const execute = () => {
context.push(execute);
try {
fn();
}
finally {
context.pop();
}
};
execute();
}
function $computed(fn) {
const computedSignal = $signal(fn());
$effect(() => {
computedSignal.value = fn();
});
return computedSignal.readOnly;
const computedSignal = $signal(fn());
$effect(() => {
computedSignal.value = fn();
});
return computedSignal.readOnly;
}
function $signal(value) {
let _value = value;
const subscribers = new Set();
function getter() {
const current = _getCurrentObserver();
if (current) {
subscribers.add(current);
}
return _value;
}
function setter(newValue) {
if (newValue === _value) {
return;
let _value = value;
const subscribers = new Set();
function getter() {
const current = _getCurrentObserver();
if (current) {
subscribers.add(current);
}
return _value;
}
_value = newValue;
for (const subscriber of subscribers) {
subscriber();
function setter(newValue) {
if (newValue === _value) {
return;
}
_value = newValue;
for (const subscriber of subscribers) {
subscriber();
}
}
}
return {
get value() {
return getter();
},
set value(newValue) {
setter(newValue);
},
readOnly: {
get value() {
return getter();
}
}
};
return {
get value() {
return getter();
},
set value(newValue) {
setter(newValue);
},
readOnly: {
get value() {
return getter();
}
}
};
}
function $resource(fn, source, options) {
function loadingState(data) {
function loadingState(data) {
return {
data: data,
loading: true,
error: null
};
}
let _isInitialLoad = true;
let _value = options?.initialValue ?? null;
let _previousParams;
const _signal = $signal(loadingState(_value));
const execute = async () => {
_signal.value = loadingState(_value);
const derivedSource = source instanceof Function ? source() : source;
if (!_isInitialLoad && derivedSource === _previousParams) {
// No need to fetch the data again if the params haven't changed
return;
}
try {
const data = await fn(derivedSource);
// Keep track of the previous value
_value = data;
_signal.value = {
data,
loading: false,
error: null
};
}
catch (error) {
_signal.value = {
data: null,
loading: false,
error
};
}
finally {
_previousParams = derivedSource;
_isInitialLoad = false;
}
};
$effect(execute);
return {
data: data,
loading: true,
error: null
data: _signal.readOnly,
refetch: async () => {
_isInitialLoad = true;
await execute();
}
};
}
let _isInitialLoad = true;
let _value = options?.initialValue ?? null;
let _previousParams;
const _signal = $signal(loadingState(_value));
const execute = async () => {
_signal.value = loadingState(_value);
const derivedSource = source instanceof Function ? source() : source;
if (!_isInitialLoad && derivedSource === _previousParams) {
// No need to fetch the data again if the params haven't changed
return;
}
try {
const data = await fn(derivedSource);
// Keep track of the previous value
_value = data;
_signal.value = {
data,
loading: false,
error: null
};
} catch (error) {
_signal.value = {
data: null,
loading: false,
error
};
} finally {
_previousParams = derivedSource;
_isInitialLoad = false;
}
};
$effect(execute);
return {
data: _signal.readOnly,
refetch: async () => {
_isInitialLoad = true;
await execute();
}
};
}
export { $signal, $effect, $computed, $resource };
3 changes: 0 additions & 3 deletions src/lwc/signals/counter.ts

This file was deleted.

0 comments on commit fa8791b

Please sign in to comment.