-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93ea44d
commit fa8791b
Showing
2 changed files
with
91 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file was deleted.
Oops, something went wrong.