dispose.js 856 B

123456789101112131415161718192021222324252627282930313233
  1. // Call dispose callback on each cache purge
  2. "use strict";
  3. var callable = require("es5-ext/object/valid-callable")
  4. , forEach = require("es5-ext/object/for-each")
  5. , extensions = require("../lib/registered-extensions")
  6. , apply = Function.prototype.apply;
  7. extensions.dispose = function (dispose, conf, options) {
  8. var del;
  9. callable(dispose);
  10. if ((options.async && extensions.async) || (options.promise && extensions.promise)) {
  11. conf.on("deleteasync", del = function (id, resultArray) {
  12. apply.call(dispose, null, resultArray);
  13. });
  14. conf.on("clearasync", function (cache) {
  15. forEach(cache, function (result, id) {
  16. del(id, result);
  17. });
  18. });
  19. return;
  20. }
  21. conf.on("delete", del = function (id, result) {
  22. dispose(result);
  23. });
  24. conf.on("clear", function (cache) {
  25. forEach(cache, function (result, id) {
  26. del(id, result);
  27. });
  28. });
  29. };