not really known
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.

73 lines
2.0 KiB

  1. var ObjectValues = function ObjectValues(object) {
  2. return Object.keys(object).map(function (key) {
  3. return object[key];
  4. });
  5. };
  6. function State() {
  7. var _this = this;
  8. var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  9. this.state = state;
  10. var subscribeList = {};
  11. var render = function render(statesToChange) {
  12. var prevState = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  13. statesToChange.forEach(function (state) {
  14. subscribeList[state].forEach(function (subscribers) {
  15. var elem = document.querySelector(subscribers[0]);
  16. if (typeof subscribers[1] === 'function') {
  17. subscribers[1](elem, _this.state[state], prevState[state]);
  18. } else {
  19. var propertyToChange = subscribers[1];
  20. elem[propertyToChange] = _this.state[state];
  21. }
  22. });
  23. });
  24. };
  25. this.set = function (arg) {
  26. var stateToUpdate = typeof arg === 'function' ? arg(this.state) : arg;
  27. var prevState = this.state;
  28. Object.assign(this.state, stateToUpdate || {});
  29. for (var _state in stateToUpdate) {
  30. if (!(_state in subscribeList)) {
  31. subscribeList[_state] = [];
  32. }
  33. }
  34. render(Object.keys(stateToUpdate || {}), prevState);
  35. };
  36. this.subscribe = function (obj) {
  37. var _loop = function _loop(key) {
  38. if (key in subscribeList) {
  39. if (Array.isArray(obj[key])) {
  40. if (Array.isArray(obj[key][0])) {
  41. subscribeList[key].map(function (arr) {
  42. subscribeList[key].push(arr);
  43. });
  44. } else {
  45. subscribeList[key].push(obj[key]);
  46. }
  47. }
  48. } else {
  49. if (Array.isArray(obj[key])) {
  50. if (Array.isArray(obj[key][0])) {
  51. subscribeList[key] = obj[key];
  52. } else {
  53. subscribeList[key] = [obj[key]];
  54. }
  55. }
  56. }
  57. };
  58. for (var key in obj) {
  59. _loop(key);
  60. }
  61. render(Object.keys(obj));
  62. };
  63. }
  64. define(function () {
  65. return State;
  66. });