Bug Tracker

Opened 5 years ago

Closed 5 years ago

#14542 closed feature (notabug)

Add jQuery.Callbacks.forget

Reported by: simen.brekken@… Owned by: simen.brekken@…
Priority: undecided Milestone: None
Component: deferred Version: 1.10.2
Keywords: Cc: jaubourg
Blocked by: Blocking:

Description

This patch adds a new method to jQuery.Callbacks named forget which simply erases any previously fired value when the memory flag is present.

While this is a simple one-line function it's very useful for bullding things like caching upon jQuery.Callbacks

Pull request: https://github.com/jquery/jquery/pull/1429

Change History (5)

comment:1 Changed 5 years ago by timmywil

Owner: set to simen.brekken@…
Status: newpending

Can you elaborate on "caching upon jQuery.Callbacks"? I'm trying to imagine the use case.

comment:2 Changed 5 years ago by simen.brekken@…

Status: pendingnew

Here's an example of a cached service that explains the the use case.

var service = $.Callbacks('memory')

service.fetchGreeting = function(url) {
    return $.get('/hello', function(who) {
        service.fire(who) // World
    })
}

service.fetchGreeting()

// Later I'd like to know who said hello using the last valid greeting.
service.add(function(who) {
  console.log('Hello', who)
})

// Now at some point the data goes stale and isn't valid any more.
// Currently there is no way of clearing the callbacks memory without triggering handlers.
service.add(function(who) {
    console.log('Stale Hello', who, ':(')
})

// With the forget method this becomes trivial.
service.fetchGreeting()

// Data goes stale and is no longer valid.
service.forget()

// This won't run immediately because there's no memory of the last value.
service.add(function() {
  console.log('Fresh Hello', target)
})

service.fetchGreeting()

comment:3 Changed 5 years ago by timmywil

Cc: jaubourg added
Component: unfileddeferred
Status: newopen

@jaubourg What is your suggestion here?

comment:4 Changed 5 years ago by jaubourg

Regarding the use-case: why use Callbacks? Store Deferreds in an internal object and you're good.

function cacheFactory( getter ) {
  var cache = {};
  return {
    get: function( key ) {
      return cache[ key ] || ( cache[ key ] = $.when( getter( key ) ) ); 
    },
    clear: function( key ) {
      delete cache[ key ];
    }
}

// Then later in the program

var fetchCache = cacheFactoy( function( url ) {
  return $.ajax( url ); 
} );

function fetchGreeting() {
  return fetchCache.get( "/hello" );
}

As a general remark, the key problem is that your solution adds a method to the Callbacks instance. Why not have a object that hides the Callbacks instance so that you can change the internal Callbacks instance?

If we add a method like forget then we're breaking the Callbacks contract. A "memory once" Callbacks has to stay "invariant", so to speak, once fired.

comment:5 Changed 5 years ago by dmethvin

Resolution: notabug
Status: openclosed

Let's pass on this.

Note: See TracTickets for help on using tickets.