-
Notifications
You must be signed in to change notification settings - Fork 4
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
9e91376
commit a6231fd
Showing
6 changed files
with
99 additions
and
204 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
11 changes: 3 additions & 8 deletions
11
examples/computed-from-multiple-signals/lwc/contactInfoForm/contactInfoForm.js
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
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,6 +1,4 @@ | ||
<template> | ||
The current count is ($reactTo): {currentCount} <br /> | ||
The current count is ($computed reactive property): {reactiveProperty} <br /> | ||
The current multiplied count is ($computed): {counterMultiplied} <br /> | ||
The counter plus two value is (nested computed): {counterPlusTwo} | ||
</template> |
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,19 +1,11 @@ | ||
import { LightningElement } from "lwc"; | ||
import { $computed, $reactTo } from "c/signals"; | ||
import { $computed } from "c/signals"; | ||
import { counter, counterPlusTwo } from "c/demoSignals"; | ||
|
||
export default class CountTracker extends LightningElement { | ||
get currentCount() { | ||
return $reactTo(counter); | ||
} | ||
|
||
reactiveProperty = $computed(() => (this.reactiveProperty = counter.value)) | ||
.value; | ||
|
||
get counterMultiplied() { | ||
return $computed(() => counter.value * 2).value; | ||
} | ||
|
||
counterPlusTwo = $computed(() => (this.counterPlusTwo = counterPlusTwo.value)) | ||
.value; | ||
} |
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,111 +1,101 @@ | ||
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; | ||
} | ||
function $reactTo(signal) { | ||
let _value = signal.value; | ||
$effect(() => { | ||
_value = signal.value; | ||
}); | ||
return _value; | ||
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; | ||
let _value = value; | ||
const subscribers = new Set(); | ||
function getter() { | ||
const current = _getCurrentObserver(); | ||
if (current) { | ||
subscribers.add(current); | ||
} | ||
function setter(newValue) { | ||
_value = newValue; | ||
for (const subscriber of subscribers) { | ||
subscriber(); | ||
} | ||
return _value; | ||
} | ||
function setter(newValue) { | ||
_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) { | ||
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); | ||
function loadingState(data) { | ||
return { | ||
data: _signal.readOnly, | ||
refetch: async () => { | ||
_isInitialLoad = true; | ||
await execute(); | ||
} | ||
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: _signal.readOnly, | ||
refetch: async () => { | ||
_isInitialLoad = true; | ||
await execute(); | ||
} | ||
}; | ||
} | ||
export { $signal, $effect, $computed, $reactTo, $resource }; | ||
export { $signal, $effect, $computed, $resource }; |
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