Skip to content

Commit

Permalink
Merge pull request #17 from WorldMaker/binding-crisis
Browse files Browse the repository at this point in the history
Bind subjects during construction again
  • Loading branch information
WorldMaker authored Feb 16, 2022
2 parents c2a7596 + a6a9322 commit 55a640f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
2 changes: 1 addition & 1 deletion projects/angular-pharkas/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-pharkas",
"version": "0.3.2",
"version": "0.3.3",
"peerDependencies": {
"@angular/common": "^11.1.1",
"@angular/core": "^11.1.1"
Expand Down
39 changes: 11 additions & 28 deletions projects/angular-pharkas/src/pharkas.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import {
animationFrameScheduler,
BehaviorSubject,
combineLatest,
from,
isObservable,
merge,
Expand All @@ -26,7 +25,6 @@ import {
observeOn,
share,
shareReplay,
tap,
throttleTime,
} from 'rxjs/operators'

Expand All @@ -47,7 +45,6 @@ interface PharkasDisplay<T> {
name: string
subject: BehaviorSubject<T>
observable: Observable<T>
direct?: boolean // observable is direct from subject
immediate: boolean
}

Expand Down Expand Up @@ -204,6 +201,7 @@ export class PharkasComponent<TViewModel> implements OnInit, OnDestroy {
throw new Error(`${name} is already bound`)
}
const subject = new BehaviorSubject(defaultValue)
this[subscription].add(observable.subscribe(subject))
this[props].set(name, {
type: 'display',
name,
Expand All @@ -230,6 +228,7 @@ export class PharkasComponent<TViewModel> implements OnInit, OnDestroy {
throw new Error(`${name} is already bound`)
}
const subject = new BehaviorSubject(defaultValue)
this[subscription].add(observable.subscribe(subject))
this[props].set(name, {
type: 'display',
name,
Expand Down Expand Up @@ -337,7 +336,6 @@ export class PharkasComponent<TViewModel> implements OnInit, OnDestroy {
name,
subject: this[state].get(localState)!,
observable: localState,
direct: true,
immediate: false,
} as PharkasProp<unknown>)
return localState
Expand Down Expand Up @@ -518,37 +516,22 @@ export class PharkasComponent<TViewModel> implements OnInit, OnDestroy {
//#endregion

ngOnInit(): void {
const displays: Observable<unknown>[] = []
const subjects: Subject<unknown>[] = []
for (const prop of this[props].values()) {
if (prop.type === 'display') {
if (prop.immediate) {
if (!prop.direct) {
this[subscription].add(
prop.observable
.pipe(
tap({
next: () => this.ref.detectChanges(),
})
)
.subscribe(prop.subject)
)
} else {
this[subscription].add(
prop.subject.subscribe({
next: () => this.ref.detectChanges(),
})
)
}
this[subscription].add(
prop.subject.subscribe({
next: () => this.ref.detectChanges(),
})
)
} else {
if (!prop.direct) {
this[subscription].add(prop.observable.subscribe(prop.subject))
}
displays.push(prop.observable)
subjects.push(prop.subject)
}
}
}
if (displays.length) {
const displayObservable = merge(...displays).pipe(
if (subjects.length) {
const displayObservable = merge(...subjects).pipe(
throttleTime(0, animationFrameScheduler),
share()
)
Expand Down

0 comments on commit 55a640f

Please sign in to comment.