Skip to content

Commit

Permalink
7.0.0-rc.5:
Browse files Browse the repository at this point in the history
- Fixed issue where ComponentStateRef wasn't working correctly with StateEmitters.
- Fixed issue where StateEmitter values that were only read after being written to weren't receving value updates in order.
  • Loading branch information
lVlyke committed Nov 17, 2021
1 parent 00785a4 commit daf70b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lithiumjs/angular",
"version": "7.0.0-rc.4",
"version": "7.0.0-rc.5",
"description": "Reactive components made easy. Lithium provides utilities that enable seamless reactive state and event interactions for Angular components.",
"author": "Mychal Thompson <mychal.r.thompson@gmail.com>",
"repository": "https://github.com/lVlyke/lithium-angular",
Expand Down
4 changes: 2 additions & 2 deletions src/component-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Constructable, IfEquals, IfReadonly, StringKey } from "./lang-utils";
import type { AsyncSourceKey } from "./metadata";
import { AsyncSourceKey, EmitterMetadata } from "./metadata";
import { EventEmitter, FactoryProvider, InjectFlags, Injector, resolveForwardRef, Type } from "@angular/core";
import { combineLatest, forkJoin, from, merge, Observable, of, ReplaySubject, Subject, Subscription, throwError } from "rxjs";
import { distinctUntilChanged, filter, map, mergeMap, skip, switchMap, tap } from "rxjs/operators";
Expand Down Expand Up @@ -463,7 +463,7 @@ export namespace ComponentState {
const propDescriptor = Object.getOwnPropertyDescriptor(instance, prop.key);
const stateSubjectProp = stateKey<ComponentT, K>(prop.key);

if (typeof prop.key === "string" && !prop.key.endsWith("$")) {
if (typeof prop.key === "string" && !prop.key.endsWith("$") && !EmitterMetadata.GetMetadataMap(instance).get(prop.key)) {

if (!propDescriptor || propDescriptor.configurable) {
let lastValue: ComponentT[K] = instance[prop.key];
Expand Down
16 changes: 14 additions & 2 deletions src/state-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,18 @@ export namespace StateEmitter {

namespace Facade {

export function CreateSetter(type: EmitterType): (value: any) => void {
export function CreateSetter(type: EmitterType, getter: () => any): (value: any) => void {
let firstInvocation = true;

return function (this: any, value: any) {
let subjectInfo = EmitterMetadata.GetMetadataMap(this).get(type)!;

// Invoke the getter to make sure change detection has been started
if (firstInvocation && !subjectInfo.writeOnly) {
firstInvocation = false;
getter.call(this);
}

// If this is a static subject...
if (EmitterMetadata.SubjectInfo.IsStaticAlias(subjectInfo)) {
// Notify the subject of the new value
Expand Down Expand Up @@ -221,6 +229,10 @@ export namespace StateEmitter {
lastObservable = curObservable;
}

if (curObservable instanceof BehaviorSubject) {
lastValue = curObservable.value;
}

// Return the last value that was emitted
return lastValue;
};
Expand Down Expand Up @@ -334,8 +346,8 @@ export namespace StateEmitter {
}
}

const facadeSetter = subjectInfo.readOnly ? undefined : Facade.CreateSetter(emitterType);
const facadeGetter = Facade.CreateGetter(emitterType, initialValue);
const facadeSetter = subjectInfo.readOnly ? undefined : Facade.CreateSetter(emitterType, facadeGetter);
// Assign the facade getter and setter to the target instance for targetInstance EmitterType
Object.defineProperty(targetInstance, emitterType, {
enumerable: true,
Expand Down

0 comments on commit daf70b7

Please sign in to comment.