Guest User

Untitled

a guest
Apr 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. (function () {
  2. Polymer({
  3. is: 'fm-private-storex',
  4. properties: {
  5. /**
  6. * @description Store all states
  7. */
  8. store: {
  9. type: Object,
  10. value: function () {
  11. return {};
  12. }
  13. },
  14. /**
  15. * @description Array with callbacks functions subscribers
  16. */
  17. subscriptions: {
  18. type: Array,
  19. value: function () {
  20. return [];
  21. }
  22. },
  23. /**
  24. * @description Mutations saved to apply
  25. */
  26. mutations: {
  27. type: Object,
  28. value: function () {
  29. return [];
  30. },
  31. observer: 'onUpdateMutations'
  32. }
  33. },
  34. /**
  35. * @description Observers to apply changes on store
  36. */
  37. observers: ['onUpdate(store.*)'],
  38. /**
  39. * @description In attached generates all init object
  40. */
  41. attached: function () {
  42. /**
  43. * @description Object on window to get a bridge communication
  44. * @type {{subscribe, commit, mutations: *}}
  45. */
  46. window.gs = window.gs || {
  47. subscribe: this.subscribe.bind(this),
  48. commit: this.commit.bind(this),
  49. mutations: this.mutations
  50. }
  51. /**
  52. * @description Proxy to protect the bridge store
  53. * @type {Proxy}
  54. */
  55. var pgs = new Proxy(window.gs, {
  56. set: function (obj, prop, value) {
  57. if (prop === 'commit') {
  58. throw new TypeError('"commit" option cannot be modified');
  59. } else if (prop === 'mutations') {
  60. if (typeof value !== 'object') {
  61. throw new TypeError('"mutations" option should be an object');
  62. }
  63. } else if (prop === 'subscribe') {
  64. if (typeof value !== 'function') {
  65. throw new TypeError('"subscribe" option should be a function');
  66. }
  67. } else {
  68. throw new TypeError('The only valid options are "commit", "subscriptions" and "mutations"');
  69. }
  70. Reflect.set(obj, prop, value);
  71. },
  72. deleteProperty: function(target, prop) {
  73. throw new Error('This object cannot be deleted')
  74. }
  75. })
  76. window.gs = pgs;
  77. },
  78. /**
  79. * @description Function to subscribe the callbacks fn
  80. * @param fn
  81. * @returns {function(this:subscribe)}
  82. */
  83. subscribe: function (fn) {
  84. this.subscriptions.push(fn);
  85. return function () {
  86. var idx = this.subscriptions.indexOf(fn)
  87. if (idx > -1) {
  88. this.subscriptions.splice(idx, 1);
  89. }
  90. }.bind(this)
  91. },
  92. /**
  93. * @description Function to apply the mutations
  94. * @param mutation
  95. * @param value
  96. */
  97. commit: function (mutation, value) {
  98. this.set('mutations', gs.mutations);
  99. if (this.mutations[mutation]) {
  100. (this.mutations[mutation])(this, value);
  101. } else {
  102. throw new Error('The mutation ' + mutation + ' does not exist.');
  103. }
  104.  
  105. },
  106. /**
  107. * @description Function that execute each fn from subscribers
  108. * @param store
  109. */
  110. onUpdate: function (store) {
  111. if (this.subscriptions) {
  112. this.subscriptions.forEach(function (t) {
  113. t(store);
  114. })
  115. }
  116. },
  117. onUpdateMutations: function (mutation) {
  118. console.log(mutation);
  119. }
  120. })
  121. })()
Add Comment
Please, Sign In to add comment