-
Notifications
You must be signed in to change notification settings - Fork 1k
Add a definition for a generic signal to web core #960
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
Merged
+233
−10
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4c6329e
Add basic concept of a generic signal
ava-cassiopeia f0f1975
add test coverage
ava-cassiopeia fd31874
add license headers
ava-cassiopeia 1794cc4
Address review comments
ava-cassiopeia 68b80be
Merge branch 'main' into avac-generic-signals
ava-cassiopeia 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import assert from 'node:assert'; | ||
| import {describe, it} from 'node:test'; | ||
| import {Signal as PSignal, computed as pComputed} from '@preact/signals-core'; | ||
| import { | ||
| signal as aSignal, | ||
| computed as aComputed, | ||
| Signal as ASignal, | ||
| WritableSignal as AWritableSignal, | ||
| isSignal, | ||
| } from '@angular/core'; | ||
|
|
||
| import {FrameworkSignal} from './signals'; | ||
|
|
||
| describe('FrameworkSignal', () => { | ||
| // Test FrameworkSignal with two sample implemenations that wrap Angular and | ||
ava-cassiopeia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Preact signals. Angular and Preact signals are good representitive samples, | ||
| // because the two common patterns - `()` vs. `.value` - are represented by | ||
| // Angular and Preact respectively. | ||
|
|
||
| describe('Angular variation', () => { | ||
| const AngularSignal: FrameworkSignal<ASignal<any>, AWritableSignal<any>> = { | ||
| computed: <T>(fn: () => T) => aComputed(fn), | ||
| isSignal: (val: unknown) => isSignal(val), | ||
| wrap: <T>(val: T) => aSignal(val), | ||
| unwrap: <T>(val: ASignal<T>) => val(), | ||
| set: <T>(signal: AWritableSignal<T>, value: T) => signal.set(value), | ||
| }; | ||
|
|
||
| it('round trip wraps and unwraps successfully', () => { | ||
| const val = 'hello'; | ||
| const wrapped = AngularSignal.wrap(val); | ||
| assert.strictEqual(AngularSignal.unwrap(wrapped), val); | ||
| }); | ||
|
|
||
ava-cassiopeia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| it('handles updates well', () => { | ||
| const signal = AngularSignal.wrap('first'); | ||
| const computedVal = AngularSignal.computed(() => `prefix ${signal()}`); | ||
|
|
||
| assert.strictEqual(signal(), 'first'); | ||
| assert.strictEqual(AngularSignal.unwrap(signal), 'first'); | ||
| assert.strictEqual(computedVal(), 'prefix first'); | ||
| assert.strictEqual(AngularSignal.unwrap(computedVal), 'prefix first'); | ||
|
|
||
| AngularSignal.set(signal, 'second'); | ||
|
|
||
| assert.strictEqual(signal(), 'second'); | ||
| assert.strictEqual(AngularSignal.unwrap(signal), 'second'); | ||
| assert.strictEqual(computedVal(), 'prefix second'); | ||
| assert.strictEqual(AngularSignal.unwrap(computedVal), 'prefix second'); | ||
| }); | ||
|
|
||
| describe('.isSignal()', () => { | ||
| it('validates a signal', () => { | ||
| const val = 'hello'; | ||
| const wrapped = AngularSignal.wrap(val); | ||
| assert.ok(AngularSignal.isSignal(wrapped)); | ||
| }); | ||
|
|
||
| it('rejects a non-signal', () => { | ||
| assert.strictEqual(AngularSignal.isSignal('hello'), false); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Preact variation', () => { | ||
| const PreactSignal: FrameworkSignal<PSignal> = { | ||
| computed: <T>(fn: () => T) => pComputed(fn), | ||
| isSignal: (val: unknown) => val instanceof PSignal, | ||
| wrap: <T>(val: T) => new PSignal(val), | ||
| unwrap: <T>(val: PSignal<T>) => val.value, | ||
| set: <T>(signal: PSignal<T>, value: T) => (signal.value = value), | ||
| }; | ||
|
|
||
| it('round trip wraps and unwraps successfully', () => { | ||
| const val = 'hello'; | ||
| const wrapped = PreactSignal.wrap(val); | ||
| assert.strictEqual(PreactSignal.unwrap(wrapped), val); | ||
| }); | ||
|
|
||
| it('handles updates well', () => { | ||
| const signal = PreactSignal.wrap('first'); | ||
| const computed = PreactSignal.computed(() => `prefix ${signal.value}`); | ||
|
|
||
| assert.strictEqual(signal.value, 'first'); | ||
| assert.strictEqual(PreactSignal.unwrap(signal), 'first'); | ||
| assert.strictEqual(computed.value, 'prefix first'); | ||
| assert.strictEqual(PreactSignal.unwrap(computed), 'prefix first'); | ||
|
|
||
| PreactSignal.set(signal, 'second'); | ||
|
|
||
| assert.strictEqual(signal.value, 'second'); | ||
| assert.strictEqual(PreactSignal.unwrap(signal), 'second'); | ||
| assert.strictEqual(computed.value, 'prefix second'); | ||
| assert.strictEqual(PreactSignal.unwrap(computed), 'prefix second'); | ||
| }); | ||
|
|
||
| describe('.isSignal()', () => { | ||
| it('validates a signal', () => { | ||
| const val = 'hello'; | ||
| const wrapped = PreactSignal.wrap(val); | ||
| assert.ok(PreactSignal.isSignal(wrapped)); | ||
| }); | ||
|
|
||
| it('rejects a non-signal', () => { | ||
| assert.strictEqual(PreactSignal.isSignal('hello'), false); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
ava-cassiopeia marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * A generic representation of a Signal that could come from any framework. | ||
| * For any library building on top of A2UI's web core lib, this must be | ||
| * implemented for their associated signals implementation. | ||
| */ | ||
| export interface FrameworkSignal<SignalType, WriteableSignalType = SignalType> { | ||
| /** | ||
| * Create a computed signal for this framework. | ||
| */ | ||
| computed<T>(fn: () => T): SignalType; | ||
ava-cassiopeia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Check if an arbitrary object is a framework signal. | ||
| */ | ||
| isSignal(val: unknown): val is SignalType; | ||
|
|
||
| /** | ||
| * Wrap the value in a signal. | ||
| */ | ||
| wrap<T>(val: T): WriteableSignalType; | ||
|
|
||
| /** | ||
| * Extract the value from a signal. | ||
| */ | ||
| unwrap<T>(val: SignalType): T; | ||
|
|
||
| /** | ||
| * Sets the value of the provided framework signal. | ||
| */ | ||
| set<T>(signal: WriteableSignalType, value: T): void; | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
The
AngularSignalimplementation forsetusesAWritableSignal<T>as the type for the signal parameter, which is appropriate for a writable signal. However, theFrameworkSignalinterface (as currently defined) expectsSignalType. With the proposed change toFrameworkSignalto includeWritableSignalType, this line would need to be updated to useAWritableSignal<any>as the second generic parameter forFrameworkSignal.This adjustment ensures type consistency with the refined
FrameworkSignalinterface, making the Angular implementation correctly typed.