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

@gommer/runtime

v1.0.27

Published

The library adds several functions to define class events and dependency properties. And also a few helper classes.

Downloads

37

Readme

The library adds several functions to define class events and dependency properties. And also a few helper classes.

Project preparation

Global Packages

npm i -g typescript

npm i -g ttypescript

Build your progect with ttsc command

Package.json

npm i -D @gommer/runtime-transform

npm i @gommer/runtime

npm i tslib

Tsconfig.json

{
    "compilerOptions": {
        ...
        "importHelpers": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "plugins": [
            { "transform": "@gommer/runtime-transform" }
        ],
        ...
    },
    ...
}

Events

Defines events

event(descriptor?: EventDescriptor0): Delegate0;
event<T1>(descriptor?: EventDescriptor1<T1>): Delegate1<T1>;
event<T1, T2>(descriptor?: EventDescriptor2<T1, T2>): Delegate2<T1, T2>;
event<T1, T2, T3>(descriptor?: EventDescriptor3<T1, T2, T3>): Delegate3<T1, T2, T3>;
event<T1, T2, T3, T4>(descriptor?: EventDescriptor4<T1, T2, T3, T4>): Delegate4<T1, T2, T3, T4>;
event<T1, T2, T3, T4, T5>(descriptor?: EventDescriptor5<T1, T2, T3, T4, T5>): Delegate5<T1, T2, T3, T4, T5>;
import {event} from "@gommer/runtime";

class Foo {
    event = event<number>({
        add(listener, handler) {
            
        },
        remove(listener, handler) {
            
        },
        emit(num: number) {
            
        }
    });
    
    emitEvent(num: number) {
        this.event.emit(num);
    }
}

class Bar {
    eventListener(sender: Foo, num: number) {
        console.log(num);
    }
}

const foo = new Foo();
const bar = new Bar();
foo.event.add(bar, bar.eventListener)
foo.emitEvent(32); // --> 32;

Dependency Property

Defines Dependency Property

A dependency property is defined by the property function. A function can define one of several types of properties for an object.

//G - Getter data type
//S - Setter data type. 
//If the S type is different from the G type, 
//then it is necessary to implement hook coerce

//Defining a simple dependency property
property<G, S = G>(descriptor?: PropertyDescriptor<G, S>): Property<G, S>;

//Defining a read only dependency property
property<G>(descriptor?: ReadOnlyPropertyDescriptor<G>): ReadOnlyProperty<G>;

//Defining a computed dependency property
property<G, S = G>(descriptor?: ComputedPropertyDescriptor<G, S>): ComputedProperty<G, S>;

Simple Dependency Property

This type of property is the simplest and most used one. This type of properties can be made dependent and also other properties can depend on it.

interface PropertyDescriptor<G, S> {
    //The default value for the property
    default?: G;

    //hook to convert the type of the input value
    coerce?: (value: S) => G;
    
    //The hook is called every time a property is changed
    change?: (current: G, old: G) => void;
    
    //You can specify a class event that will be emitted when the property changes 
    event?: BaseDelegate;
}
interface Property<G, S> {
    get<G>(): G;
    set<S>(val: S): void;
    clear(): void;

    readonly context: any;
    name(): string;
    hasValue(): boolean;
    isNull(): boolean;

    bind(sourceProperty: Property<any>, converter?: Converter<T> | ((source: any) => T));
    bind(sourceProperty: Property<any>[], converter: MultiSourceConverter<T>);

    twoway(sourceProperty: Property<any>, converter: Converter<T>);
    twoway(sourceProperty: Property<any>[], converter: MultiSourceConverter<T>);

    binding: BindingExpression<T> | MultiBindingExpression<T>;
    listiners: BindingExpression<T>[] | MultiBindingExpression<T>[];
}
//import runtime hook
import {property, event} from "@gommer/runtime";

class App {
    property = property<String, String | Number>({
        default: '',
        coerce(val: string | number) {
            return typeof val === number ? val.toString() : val;
        },
        change(current: string, old: string) {
            console.log(`Change vallue to "${current}" from "${old}"`);
        },
        event: this.propertyChanged
    })

    propertyChanged = event();
}

const a = new App();
const b = new App();

a.property.bind(b.property);
b.property.set(32);

console.log(a.property.get()); // --> "32"

Read Only Dependency Property

interface ReadOnlyPropertyDescriptor<T> {
    //a trigger that triggers a property change
    //can be like another property or event or a group of properties or events
    trigger: BaseProperty<any> | BaseDelegate | BaseProperty<any>[] | BaseDelegate[];
    
    //hook to update a property
    get(): T;

    //The hook is called every time a property is changed
    change?: (current: T, old: T) => void;

    //You can specify a class event that will be emitted when the property changes 
    event?: BaseDelegate;
}
interface ReadOnlyProperty<T> {
    get<G>(): G;
    
    readonly context: any;
    name: string;
    hasValue: boolean;
    isNull: boolean;

    binding: BindingExpression<T> | MultiBindingExpression<T>;
    listiners: BindingExpression<T>[] | MultiBindingExpression<T>[];
}
import {property} from "@gommer/runtime";

class App {
    firstName = property<String>({
        default: ''
    })
    
    lastName = property<String>({
        default: ''
    })

    //read only property
    fullName = property<String>({
        trigger: [this.firstName, this.lastName],
        get() {
            return `${this.firstName.get()} ${this.lastName.get()}`;
        }
    })
}

const a = new App();
a.firstName.set('John');
a.lastName.set('Bin');
console.log(a.fullName.get()) // --> "John Bin"

Computed Dependency Property

interface ComputedPropertyDescriptor<T, IT = T> {
    //a trigger that triggers a property change
    //can be like another property or event or a group of properties or events
    trigger: BaseProperty<any> | BaseDelegate | BaseProperty<any>[] | BaseDelegate[];

    //hook to update a property
    get: () => T;
    
    set: (value: IT) => void;
    
    coerce?: (value: IT) => T;

    //The hook is called every time a property is changed
    change?: (current: T, old: T) => void;

    //You can specify a class event that will be emitted when the property changes 
    event?: BaseDelegate;
}
import {property} from "@gommer/runtime";

class App {
    firstName = property<String>({
        default: ''
    })
    
    lastName = property<String>({
        default: ''
    })

    fullName = property<String>({
        trigger: [this.firstName, this.lastName],
        get() {
            return `${this.firstName.get()} ${this.lastName.get()}`;
        },
        set(val: string) {
            const [firstName, lastName] = val.split(' ');
            this.firstName.set(firstName || '');
            this.lastName.set(lastName || '');
        }
    })
}

const a = new App();
a.fullName.set("John Bin")
console.log(a.firstName.get('John')); // --> "John"
console.log(a.lastName.get('Bin'));   // --> "Bin"