1.0.27 • Published 3 years ago

@gommer/runtime v1.0.27

Weekly downloads
23
License
-
Repository
-
Last release
3 years ago

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"
1.0.26

3 years ago

1.0.27

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.19

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.11

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago