Skip to content

Commit

Permalink
Introduced component.state$()
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Balin committed May 1, 2019
1 parent 6242ea8 commit e1ea3d5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/databinding/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class App extends LinkedComponent {
}

render(){
const state$ = this.linkAll();
const state$ = this.state$();

return (
<div>
Expand Down
27 changes: 27 additions & 0 deletions examples/type-r/TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
!!!!

Generate lazy link accessors?
Yes, with dedicated class decorator. @linked
Or, make it configurable.

import { LinkAttributes } 'type-r-link'
import { Model } from 'type-r'
LinkAttributes( Model );

get $name(){
return this.$at( '$name' );
}

+ Cool in JS
- Doesn't work in TS

// New API.

user.$at('name') // user.linkAt( 'name' )

// For the Component:
user.state$() // user.linkAll()

// For the model:
user.pick$() // user.linkAll()

2 changes: 1 addition & 1 deletion examples/userslist-classes/src/userslist.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class EditUser extends LinkedComponent{
}

render(){
const user$ = this.linkAll();
const user$ = this.state$();

user$.name
.check( isRequired )
Expand Down
12 changes: 10 additions & 2 deletions valuelink/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type LinksCache< S, X extends keyof S> = {
export interface DataBindingSource< S >{
linkAt< K extends keyof S>( key : K ) : Link< S[ K ] >
linkAll<K extends keyof S>( ...keys : K[] ) : LinksCache< S, K >
$at< K extends keyof S>( key : K ) : Link< S[ K ] >
state$<K extends keyof S>( ...keys : K[] ) : LinksCache< S, K >
}

export abstract class LinkedComponent< P, S > extends React.Component< P, S > implements DataBindingSource< S > {
Expand All @@ -28,8 +30,14 @@ export abstract class LinkedComponent< P, S > extends React.Component< P, S > im
cache[ key ] = new StateLink( this, key, value );
}

linkAll<K extends keyof S>( ...keys : K[] ) : LinksCache< S, K >;
linkAll( ...args : ( keyof S )[] ){
// @deprecated use `this.state$()`
linkAll<K extends keyof S>( ...keys : K[] ) : LinksCache< S, K >
linkAll(){
return this.state$.apply( this, arguments );
}

state$<K extends keyof S>( ...keys : K[] ) : LinksCache< S, K >;
state$( ...args : ( keyof S )[] ){
const { state } = this,
cache = this.links || ( this.links = <any>{} ),
keys = args.length ? args : <( keyof S )[]>Object.keys( state );
Expand Down
8 changes: 7 additions & 1 deletion valuelink/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ export abstract class Link< T >{
return helpers( value ).clone( value );
}

/**
* Convert link to object to the object of links. Optionally filter by
*/
pick< K extends keyof T >( ...keys : K[]) : {[ P in K ]: Link<T[P]>}
pick() {
let links = {}, keys = arguments.length ? arguments : Object.keys( this.value );
Expand All @@ -183,7 +186,10 @@ export abstract class Link< T >{
return links;
}

$all() : {[ P in keyof T ]: Link<T[P]>}{
/**
* Convert link to object to the object of links with $-keys.
*/
$links() : {[ P in keyof T ]: Link<T[P]>}{
let links : LinksHash = {},
{ value } = this;

Expand Down

0 comments on commit e1ea3d5

Please sign in to comment.