Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Jul 24, 2023
1 parent 346179c commit bce5132
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
12 changes: 11 additions & 1 deletion packages/core/rxjs-interop/test/to_signal_spec.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {DestroyRef, EnvironmentInjector, Injector, runInInjectionContext} from '@angular/core';
import {computed, EnvironmentInjector, Injector, runInInjectionContext} from '@angular/core';
import {toSignal} from '@angular/core/rxjs-interop';
import {BehaviorSubject, ReplaySubject, Subject} from 'rxjs';

Expand Down Expand Up @@ -109,6 +109,16 @@ describe('toSignal()', () => {
expect(counter()).toBe(1);
});

fit('should disallow toSignal in computed - WIP', () => {
const counter$ = new BehaviorSubject(0);
const injector = Injector.create({providers: []}) as EnvironmentInjector;
const computedCounter = runInInjectionContext(injector, computed(() => {
const counter =
toSignal(counter$, {requireSync: true});
return counter() * 2;
}));
});

describe('with no initial value', () => {
it('should return `undefined` if read before a value is emitted', test(() => {
const counter$ = new Subject<number>();
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/signals/src/computed.ts
Expand Up @@ -63,10 +63,6 @@ const ERRORED: any = Symbol('ERRORED');
class ComputedImpl<T> extends ReactiveNode {
constructor(private computation: () => T, private equal: (oldValue: T, newValue: T) => boolean) {
super();
if (!this.producerReactiveNodesCreationAllowed) {
throw new Error(
'A new computed signal creation is disallowed in the current reactive context.');
}
}
/**
* Current value of the computation.
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/signals/src/graph.ts
Expand Up @@ -92,6 +92,13 @@ interface ReactiveEdge {
* last observed.
*/
export abstract class ReactiveNode {
constructor() {
if (!this.producerReactiveNodesCreationAllowed) {
throw new Error(
'The current reactive context (usually a computed or an effect) disallow creation of new signals while it is running.');
}
}

private readonly id = _nextReactiveId++;

/**
Expand Down

0 comments on commit bce5132

Please sign in to comment.