Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connectTo): pass target instance to selector #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

doktordirk
Copy link

Allows selectors to be based on the target instance's state

Example:

interface Item {
  value: string;
}

interface State {
   items: Record<string, Item>;
}

@connectTo({
   selector: (state: State, itemView: ItemView) => pipe(map(state => state.items[itemView.id])),
   target: 'item',
})
export class ItemView {
  @bindable id: string;
  item: Item;
}

Allows selectors to be based on the target instance's state
@zewa666
Copy link
Member

zewa666 commented Feb 22, 2022

could you elaborate a bit on the use case of this? in your example it sounds like you want to dynamically decide the state prop based on instance values. what would be the purpose of doing so?

@doktordirk
Copy link
Author

hmm seems basic to me to only bind an index

<message repeat.for="id of messageIds"></message>

and maybe an editor

<message-editor id="messageId"></message-editor>

I'm not keen ton passing large object around

@zewa666
Copy link
Member

zewa666 commented Feb 25, 2022

not sure I get that one. you want to use the provided binding to determine the dynamic key which to use as your projections target inside messageEditor?

besides relying on exact order of bind/attached could turn out to be unstable when using the instance. may I ask you for a little bit kore concrete example?

@doktordirk
Copy link
Author

doktordirk commented Feb 26, 2022

Let's say i have a main/details view and then in the details view

activate(parameters) {
    this.messageId = parameters.messageId;
}

To get the message data there or in a sub-component on atm would have to do

bind() {
  this.subscribtion = this.store.state
   .pipe(
     pluck('messages'),
     map(messages => messages[this.messageId])
  ).subscribe(message => {
     this.message = message;
 });
}

unbind() {
  this.subscribtion.unsubscribe();
}

with the proposed change, one can do instead

@connectTo({
   message: (store: Store<MessageState>, messageDetails: MessageDetails) => store.state
     .pipe(
        pluck('messages'),
        map(messages => messages[messageDetails.messageId])
     )
})
class MessageDetails {
...

as for being potentially unreliable: Dunno, certainly exactly as reliable as the 'manual' version above. Given the bind order is defined here, the order also be the same. You see any potential issues there? In both versions, one could eg use map(messages => messageDetails.messageId] ?? messages[messageDetails.messageId]) .
Whatever issues there might be, it wouldn't be specific to using the target instance in connectTo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants