-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal bind shorthand #37
Merged
+174
−20
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d37c19c
Proposal - bind shorthand
cesarParra afc96d4
Merge branch 'main' into proposal-bind-shorthand
cesarParra 8f26f06
Merging latest from main
cesarParra d410544
Commit from GitHub Actions (CI)
github-actions[bot] aa2a5cc
Allows circular dependencies in effects with a stack depth limit
stanleyzapata 9f97fc6
Renamed variable Names
stanleyzapata d5a863c
Adds explanation for MAX_STACK_DEPTH constant
stanleyzapata 128d330
feat: circular dependecy management logic update
stanleyzapata 0afe1bf
chore(release): Version 1.4.0 - Automated release [skip ci]
semantic-release-bot df8b1e8
fix: Build Last Change (#40)
stanleyzapata f5ecff2
chore(release): Version 1.4.1 - Automated release [skip ci]
semantic-release-bot b23bffe
Merge remote-tracking branch 'origin/proposal-bind-shorthand'
cesarParra ee3acca
Commit from GitHub Actions (CI)
github-actions[bot] 153fe80
Bind decorator
cesarParra a390945
Commit from GitHub Actions (CI)
github-actions[bot] 87e2296
Doc update
cesarParra a58e890
Commit from GitHub Actions (CI)
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,4 +1,5 @@ | ||
<template> | ||
The current count is ($computed reactive property): {reactiveProperty} <br /> | ||
Binded: {bindTest} The current count is ($computed reactive property): | ||
{reactiveProperty} <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
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 |
---|---|---|
|
@@ -10,6 +10,9 @@ const UNSET = Symbol("UNSET"); | |
const COMPUTING = Symbol("COMPUTING"); | ||
const ERRORED = Symbol("ERRORED"); | ||
const READY = Symbol("READY"); | ||
// The maximum stack depth value is derived from Salesforce's maximum stack depth limit in triggers. | ||
// This value is chosen to prevent infinite loops while still allowing for a reasonable level of recursion. | ||
const MAX_STACK_DEPTH = 16; | ||
const defaultEffectOptions = { | ||
_fromComputed: false, | ||
identifier: Symbol() | ||
|
@@ -38,15 +41,22 @@ function $effect(fn, options) { | |
const _optionsWithDefaults = { ...defaultEffectOptions, ...options }; | ||
const effectNode = { | ||
error: null, | ||
state: UNSET | ||
state: UNSET, | ||
stackDepth: 0 | ||
}; | ||
const execute = () => { | ||
if (effectNode.state === COMPUTING) { | ||
throw new Error("Circular dependency detected"); | ||
if ( | ||
effectNode.state === COMPUTING && | ||
effectNode.stackDepth >= MAX_STACK_DEPTH | ||
) { | ||
throw new Error( | ||
`Circular dependency detected. Maximum stack depth of ${MAX_STACK_DEPTH} exceeded.` | ||
); | ||
} | ||
context.push(execute); | ||
try { | ||
effectNode.state = COMPUTING; | ||
effectNode.stackDepth++; | ||
fn(); | ||
effectNode.error = null; | ||
effectNode.state = READY; | ||
|
@@ -58,6 +68,9 @@ function $effect(fn, options) { | |
: handleEffectError(error, _optionsWithDefaults); | ||
} finally { | ||
context.pop(); | ||
if (effectNode.stackDepth > 0) { | ||
effectNode.stackDepth--; | ||
} | ||
} | ||
}; | ||
execute(); | ||
|
@@ -348,4 +361,28 @@ function $resource(fn, source, options) { | |
function isSignal(anything) { | ||
return !!anything && anything.brand === SIGNAL_OBJECT_BRAND; | ||
} | ||
export { $signal, $effect, $computed, $resource, isSignal }; | ||
class Binder { | ||
constructor(component, propertyName) { | ||
this.component = component; | ||
this.propertyName = propertyName; | ||
} | ||
to(signal) { | ||
$effect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this ok performance wise? |
||
// @ts-expect-error The property name will be found | ||
this.component[this.propertyName] = signal.value; | ||
}); | ||
return signal.value; | ||
} | ||
} | ||
function bind(component, propertyName) { | ||
return new Binder(component, propertyName); | ||
} | ||
export { | ||
$signal, | ||
$effect, | ||
$computed, | ||
$resource, | ||
bind, | ||
bind as $bind, | ||
isSignal | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand why you are doing this but I don't love it. Not sure what would be a better way to do the binding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's why I'm keeping it as a proposal stage until I get some feedback, not sure if you saw the issue I put up for discussion: #38
I don't want to merge if its going to be detrimental compare to what exists today, but I haven't found a better way of doing it with how Javascript works today. The only way I can think this could be done is by using decorators, but JS doesn't support those yet.