npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rxjs-observed-decorator

v1.2.2

Published

Simple class property decorator which ties a class property to an RxJS Subject. Works for BehaviorSubject (default), Subject & ReplaySubject

Downloads

545

Readme

rxjs-observed-decorator

Adds a drop-dead simple decorator which ties a class variable to an RxJS Subject.

Installation

npm install rxjs-observed-decorator --save

Requires you to add "experimentalDecorators": true, to tsconfig.json

// tsconfig.json
{
    "compilerOptions": {
        ...
        "experimentalDecorators": true,
        ...
    }
}

Important Usage Notes

  • Do not attempt to initialize the Observable property. The decorator handles that for you.
  • If you are using strict mode, you can add ! to your observable definitions to avoid errors.
@Observed() property = '';
readonly property$!: Observable<string>;

Angular Examples

A simple Service with an @Observed() property

@Injectable({ providedIn: 'root' })
export class UserService {
    
    @Observed() users: User[] = null;
    readonly users$!: Observable<User[]>;

    constructor(private http: HttpClient) {}

    getUsers() {
        this.http.get('users').subscribe(users => {
            
            // the property setter calls '.next()' behind the scenes
            this.users = users;

        });
    }

}

A simple Component that uses the service's Observable

@Component({ ... })
export class UserListComponent implements OnInit {

    users$: Observable<User[]>;

    constructor(private userService: UserService) {
    }

    ngOnInit() {
        this.users$ = this.userService.users$;

        this.userService.getUsers();
    }
}

Component Template

<ng-container *ngIf="(users$ | async) as users else loading">
    <div *ngFor="let user of users">...</div>
</ng-container>
<ng-template #loading>Loading Users...</ng-template>

Generic Examples

Behavior Subject (default)

export class MyClass {

    @Observed() myProperty = 'initial value';
    
    // Observable property is automatically created.
    readonly myProperty$!: Observable<string>;

    constructor() {}
}

const instance = new MyClass();

instance.myProperty$.subscribe(value => console.log(value));

instance.myProperty = 'a'; 
instance.myProperty = 'b';
instance.myProperty = 'c';

// output:

// initial value
// a
// b
// c

Subject

export class MyClass {

    @Observed('subject') myNumber: number;
    readonly myNumber$!: Observable<number>;

    constructor() {}
}

const instance = new MyClass();

instance.myNumber = 1; 

instance.myNumber$.subscribe(value => console.log(value));

instance.myNumber = 2;
instance.myNumber = 3;

// output:

// 2
// 3

Replay Subject

See RxJS ReplaySubject for replayOptions

interface Animal {
    mass: number;
    color: string;
}

export class MyClass {

    @Observed({ type: 'replay', replayOptions: {} }) 
    animal: Animal = null;
    
    readonly animal$!: Observable<Animal>;

    constructor() {}
}

const instance = new MyClass();

instance.animal = { mass: 50, color: 'orange' }; 

instance.animal$.subscribe(animal => console.log(`mass: ${ animal.mass }, color: ${ animal.color }`));

instance.animal = { mass: 60, color: 'green' };
instance.animal = { mass: 10, color: 'blue' };

// output:

// mass: 50, color: orange
// mass: 60, color: green
// mass: 10, color: blue

Options

Observed() Decorator takes in an optional parameter for options. options can either be a string of the subject type, or an Object with more parameters as defined below.

class MyClass {
    // using the subject type:
    @Observed('subject') propA = '';

    // using the options object:
    @Observed({ type: 'subject' }) propB = '';
}

| Option | Possible Values | Notes | | - | - | - | | type | • 'subject''replay''behavior' | Default value is 'behavior' | | replayOptions | See RxJS ReplaySubject | Should only be used with type: 'replay'|

The default value for options is 'behavior'.