question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] callback pipe to help make logic in template more performant

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

NA

Expected behavior

A pipe would expect a callback function as the first argument, then run that function on the value e.g.

component

@Component(...)
export class SomeComponent {
    addWorld(value) {
        return value + ' world';
    }
}

template

{{ 'hello' | callback : addWorld }}

Minimal reproduction of the problem with instructions

Super duper easy to implement. I am happy to add if approved.

import { Pipe, PipeTransform } from '@angular/core';

/**
 * pipe allows clean component specific transformation of value without having to create a seperate pipe
 * for each set of transformation logic to improve performance. also useful for when same logic needed in template and component.ts
 * @param value the value to transform
 * @param callback the transformation function to run on value
 * @example
 * // if we were to run this callback directly in the template performance would suffer. YAY pipes!!!
 * {{ 'hello' | callback : (value) => value + ' world' }}
 */
@Pipe({
  name: 'callback'
})
export class CallbackPipe implements PipeTransform {

  transform(value: any, callback: (val: any) => {} ): any {
    return callback(value);
  }

}

What is the motivation / use case for changing the behavior?

Get the performance benefits of pipes while allowing user to make use of component logic. Which would mitigate the duplication of code across component and pipe when logic is needed in both the template and component.ts file.

This could be taught as best practice to help new users avoid putting logic directly in template e.g.

template

{{addWorld('hello')}}

Environment


Angular version: X.Y.Z


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ericmartinezrcommented, Sep 25, 2018

Wouldn’t this also be partially solved by https://github.com/angular/angular/issues/25976 ?

1reaction
pkozlowski-opensourcecommented, Feb 10, 2019

I think that this is duplicate of #25976 (maybe not in the proposed impl details, but in the need to address). Let’s move the discussion to #25976 (I’ve just provided some input in there).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing a callback function into a custom angular pipe - Medium
The body of the pipe simply calls the callback in case there is a callback passed in. Otherwise, it returns the value field....
Read more >
Angular's async pipe and performance - a code walk-through
The Angular async pipe is the cornerstone of making applications performant. How exactly does it work? Let's walk through the code together ...
Read more >
Why You Should Not Put Any Logic in the RxJS Subscribe ...
The async pipe is a feature provided by Angular to handle asynchronous data in the Angular template.
Read more >
New possibilities with Angular's push pipe - Part 1 - InDepth.Dev
So the basic functionality the async pipe provides is taking a Promise or an Observable over it's transform function. It receives the value...
Read more >
Angular Performance: Optimizing Expression Re-evaluation ...
So we created a pipe method which takes in an array of transformation functions, it passes the arr array through the functions, piping...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found