Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reset a jQuery deferred object state?

Is it possible to reset a resolved jQuery object to an 'unresolved' state and kick off it's initialization and callbacks all over again?

The specific thing I'm doing is that I have a jQuery deferred wrapper over the local file system api. From there I build up higher level deferreds for the things I care about:

var getFs = defFs.requestQuota(PERSISTENT, 1024*1024)
              .pipe (bytes) -> defFs.requestFs(PERSISTENT, bytes)

var getCacheContents = getFs.pipe (fileSystem) -> 
      defFs.getDirectory('Cache', fileSystem.root).pipe (dir) ->
          defFs.readEntries(dir)

Now most of the time, when I call getCacheContents I don't mind the memo-ized values being returned, in fact I prefer it. But, on the occasion when I want to write to the cache I really would like the ability to reset that pipe and have it re-select and rescan the cache next time its accessed.

I could cobble something together from $.Callbacks but a deferred-based solution would really be ideal.

like image 394
George Mauer Avatar asked Aug 01 '12 15:08

George Mauer


2 Answers

No. A Promise is by definition a thing that resolves only once - from unresolved to fulfilled OR to rejected. You will not be able to do this with jQuery's Deferreds.

What you are actually searching for are Signals. They are to be fired more than once, but provide a similiar interface. There are some implementations around, you might ceck out js-signals or wire.js.

like image 137
Bergi Avatar answered Nov 09 '22 22:11

Bergi


The only solution I could find is to reset the $.Deferred object and return new Promise from that one. It works together with some internal API dirty checking (if something gets edited / deleted), but would be more performant to just reset the existing $.Deferred and let it re-resolve on the next Promise request.

like image 3
dain Avatar answered Nov 09 '22 22:11

dain