Guest User

Untitled

a guest
May 2nd, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. const ACTIVITY_INTERACTION = "ACTIVITY_INTERACTION";
  2.  
  3. // defined action to change AITs
  4. function createActivityInteraction(interactionTime) {
  5. return { type: ACTIVITY_INTERACTION, payload: { interactionTime } };
  6. }
  7.  
  8. // define initial state of AITs
  9. const INITIAL_STATE = {
  10. previousInteraction: localstorage.getItem("user@email.com.activity_interaction") || new Date(0).getTime(); // last time or epoch
  11. currentInteraction: new Date().getTime()
  12. }
  13.  
  14. // define epic (middleware) to handle the side effect of calculating NN from service response and store
  15. // then pushing it over a channel
  16. function activityEpic(action, store) {
  17. action.ofType(GET_ACTIVITIES)
  18. .switchMap(() => getActivities()
  19. .map(data => {
  20. getActivitiesSuccess(data);
  21. // determine net new
  22. const netNew = Object.values(store.activity.activities)
  23. .reduce((sum, activity) => {
  24. return activity.event_happened_at > store.previousInteraction;
  25. }, 0);
  26. // push NN
  27. chrome.runtime.sendMessage("id", { type: NET_NEW, payload: netNew });
  28. })
  29. .catch(response => getActivitiesError(response.status))
  30. );
  31. }
  32.  
  33. // define case to handle AIT state changes from new action
  34. // essentially, current -> previous, new -> current
  35. function activityReducer(state = INITIAL_STATE, action) {
  36. switch(action.type) {
  37. case ACTIVITY_INTERACTION:
  38. return {
  39. previousInteraction: state.currentInteraction,
  40. currentInteraction: action.payload.interactionTime
  41. };
  42. default:
  43. return state;
  44. }
  45. }
  46.  
  47. // the view should subscribe to the AIT previous state to determine if they should show the indicator
  48. class ContainerView {
  49. @select(["activity", "activities"]);
  50. readonly activities$: Observable<Array<IActivity>>;
  51.  
  52. @select(["activity", "previousInteraction"])
  53. readonly previousInteraction$: Observable<String>;
  54.  
  55. ngOnInit() {
  56. // trigger action to get activities
  57. activityActions.getActivities();
  58.  
  59. // trigger action to set AIT when the user focuses on the window
  60. window.addEventListener("focus", (event) => {
  61. const currentInteraction = new Date().getTime();
  62. localStorage.setItem("user@email.com.activity_interaction", currentInteraction));
  63. dispatch(createActivityInteraction(currentInteraction));
  64. });
  65.  
  66. // for other documents which may have loaded the activity app to keep them in sync
  67. // does not fire on the current document
  68. window.addEventListener("storage", (event) => {
  69. if (event.key === "user@email.com.activity_interaction") {
  70. dispatch(createActivityInteraction(event.newValue));
  71. }
  72. });
  73. }
  74. }
  75.  
  76. class PresentationView {
  77. @Input() interactionTime: string;
  78. @Input() activity: IActivity;
  79. public isNetNew: boolean = false;
  80.  
  81. // listen for when the interaction time changes
  82. ngOnChanges(changes) {
  83. // net new if the activity happened sooner than the previous interaction
  84. this.isNetNew = this.activity.event_happened_at > changes.interactionTime;
  85. }
  86. }
  87.  
  88. `
  89. <container-view>
  90. <presentation-view
  91. *ngFor="let activity of activities$ | async"
  92. [activity]="activity"
  93. [interactionTime]="previousInteraction$"></presentation-view>
  94. </container-view>
  95. `
Add Comment
Please, Sign In to add comment