Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import RelayStoreData from 'react-relay/lib/RelayStoreData';
  2. import RelayQueryPath from 'react-relay/lib/RelayQueryPath';
  3. import printRelayQuery from 'react-relay/lib/printRelayQuery';
  4.  
  5. import {List,Map} from 'immutable';
  6.  
  7. var REMOVE_KEYS = [
  8. '_storage'
  9. ];
  10.  
  11. var getId = (() => {
  12. var id = 1;
  13. return () => {
  14. return id++;
  15. }
  16. })()
  17.  
  18. var copyStore = (value) => {
  19. if (Array.isArray(value)) {
  20. return List(value.map(v => copyStore(v)));
  21. } else if (value instanceof RelayQueryPath) {
  22. return Map({
  23. // TODO: serialize the path into something
  24. });
  25. } else if (value !== null && typeof(value) === 'object') {
  26. return Map(Object.keys(value)
  27. .filter(key => REMOVE_KEYS.indexOf(key) === -1)
  28. .map(key => [key, copyStore(value[key])])
  29. );
  30. } else {
  31. return value;
  32. }
  33. }
  34.  
  35. class History {
  36. constructor() {
  37. this.subscribers = Map();
  38. this.changes = List();
  39. }
  40.  
  41. watch(store) {
  42. // monkey patch store to send us change events ...
  43. // this is shitty / wrong / etc
  44. var handleQueryPayload = store.handleQueryPayload;
  45. store.handleQueryPayload = (query, response, forceIndex) => {
  46. handleQueryPayload.call(store, query, response, forceIndex);
  47. this.publish(store, 'HANDLE_QUERY_PAYLOAD', {query, response, forceIndex});
  48. }
  49.  
  50. var handleUpdatePayload = store.handleUpdatePayload;
  51. store.handleUpdatePayload = (query, response, _ref) => {
  52. handleUpdatePayload.call(store, query, response, _ref);
  53. this.publish(store, 'HANDLE_UPDATE_PAYLOAD', {query, response, _ref});
  54. }
  55.  
  56. // get initial store
  57. this.publish(store, 'INITIAL_STORE', {});
  58. }
  59.  
  60. current() {
  61. return this.changes.last();
  62. }
  63.  
  64. previous() {
  65. return this.changes.takeLast(2).first();
  66. }
  67.  
  68. getChanges() {
  69. return this.changes;
  70. }
  71.  
  72. publish(store, type, {query,response}) {
  73. var change = Map({
  74. id: getId(),
  75. store: copyStore(store.getQueuedStore()),
  76. type: type,
  77. date: new Date(),
  78. query: query ? printRelayQuery(query) : undefined,
  79. response: response
  80. });
  81.  
  82. this.changes = this.changes.push(change);
  83.  
  84. this.subscribers.forEach(cb => {
  85. cb(change, this.previous(), this.changes);
  86. });
  87. }
  88.  
  89. subscribe(name, cb) {
  90. this.subscribers = this.subscribers.set(name, cb);
  91. }
  92.  
  93. unsubscribe(name) {
  94. this.subscribers = this.subscribers.delete(name);
  95. }
  96. }
  97.  
  98. var history = new History();
  99. history.watch(RelayStoreData.getDefaultInstance());
  100.  
  101. export default history;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement