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

zoneless

v0.2.0

Published

## Purpose

Downloads

3

Readme

THIS IS EXPERIMENTAL

Purpose

This project aims to remove zone.js by using JavaScript Proxies and the Angular's new inject function to get the ChangeDetectorRef of the component and notify when properties are changed.

Thus, you can remove zone.js from your project and both improve the performance of your application and the bundle size.

Usage

Installation

Just install with npm:

npm install zoneless

Import and use


import { useState } from 'zoneless';

@Component({
    selector: 'app-root',
    template: `
        <h1>Zoneless</h1>
        <p>Counter: {{ state.counter }}</p>
        <button (click)="increment()">Increment</button>
    `,
    })
})
export class AppComponent {
    state = useState({
        counter: 0,
    });

    increment() {
        this.state.counter++; // everything works as usual
    }
}

Changes you need top make to remove zone.js

First, you need to remove zone.js from your polyfills. Starting from Angular 15, polyfills are loaded using a property in angular.json:

"projects": {
    "my-project": {
        "architect": {
            "build": {
                "options": {
                    "polyfills": [
                        "zone.js" // remove this line
                    ]
                }
            }
        }
    }
}

And disable using ngZone in main.ts:

platformBrowserDynamic().bootstrapModule(AppModule, { ngZone: 'noop' })

That's it, now you have a zoneless application!

Computed properties

You can also use computed properties, derived from an existing state:


import { useState, computed } from 'zoneless';

@Component({
    selector: 'app-root',
    template: `
        <h1>Zoneless</h1>
        <p>Counter: {{ state.counter }}</p>
        <p>Counter * 2: {{ doubleCounter() }}</p>
        <button (click)="increment()">Increment</button>
    `,
    })
})
export class AppComponent {
    state = useState({
        counter: 0,
    });

    doubleCounter = computed(
        () => this.state.counter * 2,
        () => [this.state.counter],
    );

    increment() {
        this.state.counter++; // everything works as usual
    }
}

Notice two things:

  1. The computed function takes a function as the first argument, which is the function that will be called to get the value of the computed property.
  2. Then it takes another function, which returns an array of dependencies. These are the properties that will be watched for changes. If any of them changes, the computed property will be recalculated.
  3. It itself returns a function, so we need to call it in the template to get the value. This is to make Angular's change detection actually know the value has changed. The computing function will not if no dependencies have changed.
  4. The dependency array actually let's us depenend on several other states

Using Observable-s

async pipe might no longer work, but instead, you can use another function provided by the library, useObservable:

import { useObservable } from 'zoneless';

@Component({
    selector: 'app-root',
    template: `
        <h1>Zoneless</h1>
        <p>Timer: {{ interval() }}</p>
    `,
    })
})
export class AppComponent {
    state = useObservable(interval(1_000));
}

So we no longer need the async pipe, but we can still use Observable-s.

Note that the useObservable function returns a function, so you need to call it to get the value. This is to make Angular's change detection actually know the value has changed.

The useObservable function also automatically unsubscribes from the Observable when the component is destroyed.

Known issues:

No issues have been reported as of now, but this is experimental, so use at your own risk.

Contributing

Any contribution is welcome, be it a comment, and open issue, PR or anything else. As mentioned before, this is experimental, so any feedback is welcome. The aim is to experiment this further and see if it can be used in production.