Commit 6dc9ad86 authored by Robert Knight's avatar Robert Knight

Use `watch` utility in `frame-sync`

parent c313ec3b
...@@ -5,6 +5,7 @@ import events from '../events'; ...@@ -5,6 +5,7 @@ import events from '../events';
import Discovery from '../../shared/discovery'; import Discovery from '../../shared/discovery';
import uiConstants from '../ui-constants'; import uiConstants from '../ui-constants';
import * as metadata from '../util/annotation-metadata'; import * as metadata from '../util/annotation-metadata';
import { watch } from '../util/watch';
/** /**
* @typedef FrameInfo * @typedef FrameInfo
...@@ -49,25 +50,17 @@ export default function FrameSync($rootScope, $window, store, bridge) { ...@@ -49,25 +50,17 @@ export default function FrameSync($rootScope, $window, store, bridge) {
* notify connected frames about new/updated/deleted annotations. * notify connected frames about new/updated/deleted annotations.
*/ */
function setupSyncToFrame() { function setupSyncToFrame() {
// List of loaded annotations in previous state
let prevAnnotations = [];
let prevFrames = [];
let prevPublicAnns = 0; let prevPublicAnns = 0;
store.subscribe(function () { watch(
const state = store.getState(); store.subscribe,
if ( [() => store.getState().annotations.annotations, () => store.frames()],
state.annotations.annotations === prevAnnotations && ([annotations, frames], [prevAnnotations]) => {
state.frames === prevFrames
) {
return;
}
let publicAnns = 0; let publicAnns = 0;
const inSidebar = new Set(); const inSidebar = new Set();
const added = []; const added = [];
state.annotations.annotations.forEach(function (annot) { annotations.forEach(function (annot) {
if (metadata.isReply(annot)) { if (metadata.isReply(annot)) {
// The frame does not need to know about replies // The frame does not need to know about replies
return; return;
...@@ -85,8 +78,6 @@ export default function FrameSync($rootScope, $window, store, bridge) { ...@@ -85,8 +78,6 @@ export default function FrameSync($rootScope, $window, store, bridge) {
const deleted = prevAnnotations.filter(function (annot) { const deleted = prevAnnotations.filter(function (annot) {
return !inSidebar.has(annot.$tag); return !inSidebar.has(annot.$tag);
}); });
prevAnnotations = state.annotations.annotations;
prevFrames = state.frames;
// We currently only handle adding and removing annotations from the frame // We currently only handle adding and removing annotations from the frame
// when they are added or removed in the sidebar, but not re-anchoring // when they are added or removed in the sidebar, but not re-anchoring
...@@ -102,7 +93,6 @@ export default function FrameSync($rootScope, $window, store, bridge) { ...@@ -102,7 +93,6 @@ export default function FrameSync($rootScope, $window, store, bridge) {
inFrame.delete(annot.$tag); inFrame.delete(annot.$tag);
}); });
const frames = store.frames();
if (frames.length > 0) { if (frames.length > 0) {
if ( if (
frames.every(function (frame) { frames.every(function (frame) {
...@@ -118,7 +108,8 @@ export default function FrameSync($rootScope, $window, store, bridge) { ...@@ -118,7 +108,8 @@ export default function FrameSync($rootScope, $window, store, bridge) {
} }
} }
} }
}); }
);
} }
/** /**
......
...@@ -55,7 +55,7 @@ describe('sidebar/services/frame-sync', function () { ...@@ -55,7 +55,7 @@ describe('sidebar/services/frame-sync', function () {
beforeEach(function () { beforeEach(function () {
fakeStore = createFakeStore( fakeStore = createFakeStore(
{ annotations: [] }, { annotations: { annotations: [] } },
{ {
connectFrame: sinon.stub(), connectFrame: sinon.stub(),
destroyFrame: sinon.stub(), destroyFrame: sinon.stub(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment