S.mutation.spec.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. describe("Computations which modify data", function () {
  2. it("freeze data while executing computation", function () {
  3. S.root(function () {
  4. var a = S.data(false),
  5. b = S.data(0),
  6. cb,
  7. c = S(function () { if (a()) { b(1); cb = b(); a(false); } });
  8. b(0);
  9. a(true);
  10. expect(b()).toBe(1);
  11. expect(cb).toBe(0);
  12. });
  13. });
  14. it("freeze data while propagating", function () {
  15. S.root(function () {
  16. var seq = "",
  17. a = S.data(false),
  18. b = S.data(0),
  19. db,
  20. c = S(function () { if (a()) { seq += "c"; b(1); a(false); } }),
  21. d = S(function () { if (a()) { seq += "d"; db = b(); } });
  22. b(0);
  23. seq = "";
  24. a(true);
  25. expect(seq).toBe("cd");
  26. expect(b()).toBe(1);
  27. expect(db).toBe(0); // d saw b(0) even though it ran after c whcih modified b() to b(1)
  28. });
  29. });
  30. it("continue running until changes stop", function () {
  31. S.root(function () {
  32. var seq = "",
  33. a = S.data(0);
  34. S(function () { seq += a(); if (a() < 10) a(a() + 1); });
  35. expect(seq).toBe("012345678910");
  36. expect(a()).toBe(10);
  37. });
  38. });
  39. it("propagate changes topologically", function () {
  40. S.root(function () {
  41. //
  42. // d1 d2
  43. // | \ / |
  44. // | c1 |
  45. // | ^ |
  46. // | : |
  47. // b1 b2 b3
  48. // \ | /
  49. // a1
  50. //
  51. var seq = "",
  52. a1 = S.data(0),
  53. c1 = S.data(0),
  54. b1 = S(function () { a1(); }),
  55. b2 = S(function () { c1(a1()); }),
  56. b3 = S(function () { a1(); }),
  57. d1 = S(function () { b1(); seq += "c4(" + c1() + ")"; }),
  58. d2 = S(function () { b3(); seq += "c5(" + c1() + ")"; });
  59. seq = "";
  60. a1(1);
  61. expect(seq).toBe("c4(0)c5(0)c4(1)c5(1)");
  62. });
  63. });
  64. })