МУРОМ является долговечным игровым редактором, работающим в браузере | MUROM is a durable game editor working in a browser http://opengamestudio.org/murom
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
1.9KB

  1. /*
  2. *
  3. * UUID
  4. *
  5. */
  6. // Create GUID / UUID in JavaScript?
  7. // https://stackoverflow.com/a/2117523
  8. муром.uuid = function()
  9. {
  10. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
  11. /[xy]/g,
  12. function(c)
  13. {
  14. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  15. return v.toString(16);
  16. }
  17. );
  18. };
  19. /*
  20. *
  21. * Уведомитель / Reporter
  22. *
  23. */
  24. // ReporterSubscription class.
  25. function ReporterSubscription(id, callback, reporter)
  26. {
  27. this.id = id;
  28. this.callback = callback;
  29. this.reporter = reporter;
  30. }
  31. // Reporter class.
  32. function Reporter(name)
  33. {
  34. this.name =
  35. (typeof name !== "undefined") ?
  36. name :
  37. "";
  38. this.subscriptions = [];
  39. }
  40. Reporter.prototype.report = function()
  41. {
  42. for (var id in this.subscriptions)
  43. {
  44. var subscription = this.subscriptions[id];
  45. subscription.callback();
  46. }
  47. }
  48. Reporter.prototype.subscribe = function(callback)
  49. {
  50. var id = murom.uuid();
  51. var subscription = new ReporterSubscription(id, callback, this);
  52. this.subscriptions.push(subscription);
  53. return subscription;
  54. }
  55. Reporter.prototype.subscribeMany = function(funcs)
  56. {
  57. for (var i = 0; i < funcs.length; ++i)
  58. {
  59. var func = funcs[i];
  60. this.subscribe(func);
  61. }
  62. }
  63. var Уведомитель = Reporter;
  64. Reporter.prototype.уведомить = function()
  65. {
  66. this.report();
  67. };
  68. Reporter.prototype.подписать = function(функция)
  69. {
  70. this.subscribe(функция);
  71. };
  72. Reporter.prototype.подписатьМного = function(funcs)
  73. {
  74. this.subscribeMany(funcs);
  75. };
  76. /*
  77. *
  78. * Пуск / Start
  79. *
  80. */
  81. муром.пуск = new Уведомитель();
  82. // Can also be accessed as 'murom.run'.
  83. Object.defineProperty(murom, "run", {
  84. get: function()
  85. {
  86. return муром.пуск;
  87. }
  88. });
  89. murom.start = function()
  90. {
  91. муром.пуск.уведомить();
  92. };