S.dispose.spec.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. describe("S.root(dispose)", function () {
  2. it("disables updates and sets computation's value to undefined", function () {
  3. S.root(function (dispose) {
  4. var c = 0,
  5. d = S.data(0),
  6. f = S(function () { c++; return d(); });
  7. expect(c).toBe(1);
  8. expect(f()).toBe(0);
  9. d(1);
  10. expect(c).toBe(2);
  11. expect(f()).toBe(1);
  12. dispose();
  13. d(2);
  14. expect(c).toBe(2);
  15. expect(f()).toBe(1);
  16. });
  17. });
  18. // unconventional uses of dispose -- to insure S doesn't behaves as expected in these cases
  19. it("works from the body of its own computation", function () {
  20. S.root(function (dispose) {
  21. var c = 0,
  22. d = S.data(0),
  23. f = S(function () { c++; if (d()) dispose(); d(); });
  24. expect(c).toBe(1);
  25. d(1);
  26. expect(c).toBe(2);
  27. d(2);
  28. expect(c).toBe(2);
  29. });
  30. });
  31. it("works from the body of a subcomputation", function () {
  32. S.root(function (dispose) {
  33. var c = 0,
  34. d = S.data(0),
  35. f = S(function () {
  36. c++;
  37. d();
  38. S(function () { if (d()) dispose(); });
  39. });
  40. expect(c).toBe(1);
  41. d(1);
  42. expect(c).toBe(2);
  43. d(2);
  44. expect(c).toBe(2);
  45. });
  46. });
  47. });