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