Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
a161abcc
Commit
a161abcc
authored
Jan 24, 2015
by
Gerben
Committed by
Aron Carroll
Feb 04, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Shuffle around lines of code
parent
49318aa4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
75 additions
and
82 deletions
+75
-82
annotation-sync.coffee
h/static/scripts/annotation-sync.coffee
+62
-68
cross-frame-bridge.coffee
h/static/scripts/cross-frame-bridge.coffee
+12
-14
cross-frame-discovery.coffee
h/static/scripts/cross-frame-discovery.coffee
+1
-0
No files found.
h/static/scripts/annotation-sync.coffee
View file @
a161abcc
...
...
@@ -24,8 +24,37 @@ class AnnotationSync
on
:
(
event
,
handler
)
->
throw
new
Error
(
'options.on unspecified for AnnotationSync.'
)
# Cache of annotations which have crossed the bridge for fast, encapsulated
# association of annotations received in arguments to window-local copies.
cache
:
null
constructor
:
(
options
,
bridge
)
->
@
options
=
$
.
extend
(
true
,
{},
@
options
,
options
)
@
cache
=
{}
@
_on
=
@
options
.
on
@
_emit
=
@
options
.
emit
# Listen locally for interesting events
for
event
,
handler
of
@
_eventListeners
this
.
_on
(
event
,
handler
)
# Register remotely invokable methods
for
method
,
func
in
@
_channelListeners
bridge
.
on
(
method
,
func
)
# Upon new connections, send over the items in our cache
onConnect
=
(
channel
)
=>
this
.
_syncCache
(
channel
)
bridge
.
onConnect
(
onConnect
)
getAnnotationForTag
:
(
tag
)
->
@
cache
[
tag
]
or
null
# Handlers for messages arriving through a channel
channelListeners
:
_
channelListeners
:
'beforeCreateAnnotation'
:
(
txn
,
annotation
)
=>
annotation
=
this
.
_parse
annotation
delete
@
cache
[
annotation
.
$
$tag
]
...
...
@@ -63,30 +92,38 @@ class AnnotationSync
annotations
=
(
this
.
_parse
a
for
a
in
annotations
)
@
_emit
(
'loadAnnotations'
,
annotations
)
# Cache of annotations which have crossed the bridge for fast, encapsulated
# association of annotations received in arguments to window-local copies.
cache
:
null
constructor
:
(
options
,
bridge
)
->
@
options
=
$
.
extend
(
true
,
{},
@
options
,
options
)
@
cache
=
{}
@
_on
=
@
options
.
on
@
_emit
=
@
options
.
emit
# Listen locally for interesting events
for
event
,
handler
of
@
eventListeners
this
.
_on
(
event
,
handler
)
onConnect
=
(
channel
)
=>
# Upon new connections, send over the items in our cache
this
.
_syncCache
(
channel
)
bridge
.
onConnect
(
onConnect
)
# Register remotely invokable methods
for
method
,
func
in
@
channelListeners
bridge
.
on
(
method
,
func
)
# Handlers for events coming from this frame, to send them across the channel
_eventListeners
:
'beforeAnnotationCreated'
:
(
event
,
annotation
)
=>
return
if
annotation
.
$
$tag
?
this
.
_mkCallRemotelyAndParseResults
(
'beforeCreateAnnotation'
)(
annotation
)
this
'annotationCreated'
:
(
event
,
annotation
)
=>
return
unless
annotation
.
$
$tag
?
and
@
cache
[
annotation
.
$
$tag
]
this
.
_mkCallRemotelyAndParseResults
(
'createAnnotation'
)(
annotation
)
this
'annotationUpdated'
:
(
event
,
annotation
)
=>
return
unless
annotation
.
$
$tag
?
and
@
cache
[
annotation
.
$
$tag
]
this
.
_mkCallRemotelyAndParseResults
(
'updateAnnotation'
)(
annotation
)
this
'annotationDeleted'
:
(
event
,
annotation
)
=>
return
unless
annotation
.
$
$tag
?
and
@
cache
[
annotation
.
$
$tag
]
onFailure
=
(
err
)
=>
if
err
then
@
annotator
.
setupAnnotation
annotation
# TODO
else
delete
@
cache
[
annotation
.
$
$tag
]
this
.
_mkCallRemotelyAndParseResults
(
'deleteAnnotation'
,
onFailure
)(
annotation
)
this
'annotationsLoaded'
:
(
event
,
annotations
)
=>
annotations
=
(
this
.
_format
a
for
a
in
annotations
when
not
a
.
$
$tag
)
return
unless
annotations
.
length
this
.
_notify
method
:
'loadAnnotations'
params
:
annotations
this
_syncCache
:
(
channel
)
=>
# Synchronise (here to there) the items in our cache
...
...
@@ -96,18 +133,6 @@ class AnnotationSync
method
:
'loadAnnotations'
params
:
annotations
getAnnotationForTag
:
(
tag
)
->
@
cache
[
tag
]
or
null
# Handlers for events coming from this frame, to send them across the channel
eventListeners
:
'beforeAnnotationCreated'
:
@
beforeAnnotationCreated
'annotationCreated'
:
@
annotationCreated
'annotationUpdated'
:
@
annotationUpdated
'annotationDeleted'
:
@
annotationDeleted
'annotationsLoaded'
:
@
annotationsLoaded
_mkCallRemotelyAndParseResults
:
(
method
,
callBack
)
->
fn
=
(
annotation
)
->
# Wrap the callback function to first parse returned items
...
...
@@ -123,37 +148,6 @@ class AnnotationSync
params
:
this
.
_format
(
annotation
)
@
bridge
.
call
(
options
)
beforeAnnotationCreated
:
(
event
,
annotation
)
=>
return
if
annotation
.
$
$tag
?
this
.
_mkCallRemotelyAndParseResults
(
'beforeCreateAnnotation'
)(
annotation
)
this
annotationCreated
:
(
event
,
annotation
)
=>
return
unless
annotation
.
$
$tag
?
and
@
cache
[
annotation
.
$
$tag
]
this
.
_mkCallRemotelyAndParseResults
(
'createAnnotation'
)(
annotation
)
this
annotationUpdated
:
(
event
,
annotation
)
=>
return
unless
annotation
.
$
$tag
?
and
@
cache
[
annotation
.
$
$tag
]
this
.
_mkCallRemotelyAndParseResults
(
'updateAnnotation'
)(
annotation
)
this
annotationDeleted
:
(
event
,
annotation
)
=>
return
unless
annotation
.
$
$tag
?
and
@
cache
[
annotation
.
$
$tag
]
onFailure
=
(
err
)
=>
if
err
then
@
annotator
.
setupAnnotation
annotation
# TODO
else
delete
@
cache
[
annotation
.
$
$tag
]
this
.
_mkCallRemotelyAndParseResults
(
'deleteAnnotation'
,
onFailure
)(
annotation
)
this
annotationsLoaded
:
(
event
,
annotations
)
=>
annotations
=
(
this
.
_format
a
for
a
in
annotations
when
not
a
.
$
$tag
)
return
unless
annotations
.
length
this
.
_notify
method
:
'loadAnnotations'
params
:
annotations
this
# Parse returned annotations to update cache with any changes made remotely
_parseResults
:
(
results
)
->
for
annotations
in
results
...
...
h/static/scripts/cross-frame-bridge.coffee
View file @
a161abcc
class
CrossFrameBridge
# Connected links to other frames
links
:
null
options
:
# Scope identifier to distinguish this channel from any others
scope
:
'crossFrameBridge'
...
...
@@ -16,6 +12,9 @@ class CrossFrameBridge
# Any callbacks for messages on the channel. Max one callback per method.
channelListeners
:
{}
# Connected links to other frames
links
:
null
constructor
:
(
options
)
->
@
options
=
$
.
extend
(
true
,
{},
@
options
,
options
)
@
onConnectListeners
=
[
@
options
.
onConnect
]
...
...
@@ -32,8 +31,6 @@ class CrossFrameBridge
onReady
:
(
channel
)
=>
for
callback
in
@
onConnectListeners
callback
.
call
(
this
,
channel
,
source
)
# Create the channel
channel
=
this
.
_buildChannel
channelOptions
# Attach channel message listeners
...
...
@@ -47,14 +44,6 @@ class CrossFrameBridge
channel
# Construct a channel to another frame
_buildChannel
:
(
options
)
->
# jschannel chokes on FF and Chrome extension origins.
if
(
options
.
origin
.
match
/^chrome-extension:\/\//
)
or
(
options
.
origin
.
match
/^resource:\/\//
)
options
=
$
.
extend
{},
options
,
{
origin
:
'*'
}
channel
=
Channel
.
build
(
options
)
# Make a method call on all links, collect the results and pass them to a
# callback when all results are collected. Parameters:
# - options.method (required): name of remote method to call
...
...
@@ -109,3 +98,12 @@ class CrossFrameBridge
# Add a function to be called upon a new connection
onConnect
:
(
callback
)
->
@
onConnectListeners
.
push
(
callback
)
# Construct a channel to another frame
_buildChannel
:
(
options
)
->
# jschannel chokes on FF and Chrome extension origins.
if
(
options
.
origin
.
match
/^chrome-extension:\/\//
)
or
(
options
.
origin
.
match
/^resource:\/\//
)
options
=
$
.
extend
{},
options
,
{
origin
:
'*'
}
channel
=
Channel
.
build
(
options
)
h/static/scripts/cross-frame-discovery.coffee
View file @
a161abcc
...
...
@@ -33,6 +33,7 @@ class CrossFrameDiscovery
$
(
window
).
off
'message'
,
this
.
_onMessage
return
# Send out a beacon to discover frames to connect with
_beacon
:
->
beaconMessage
=
if
@
options
.
server
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment