Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
rivets
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
70452402
authored
2013-08-16 20:44:15 -0700
by
Michael Richards
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Implement basic array mutation observing in the default adapter.
1 parent
7ad15ba1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
14 deletions
src/adapters.coffee
src/adapters.coffee
View file @
7045240
...
...
@@ -6,22 +6,53 @@ Rivets.adapters['.'] =
counter
:
0
weakmap
:
{}
subscribe
:
(
obj
,
keypath
,
callback
)
->
weakReference
:
(
obj
)
->
unless
obj
[
@
id
]
?
id
=
@
counter
++
Object
.
defineProperty
obj
,
@
id
,
enumerable
:
false
configurable
:
false
writable
:
false
value
:
id
@
weakmap
[
id
]
=
callbacks
:
{}
Object
.
defineProperty
obj
,
@
id
,
value
:
id
@
weakmap
[
obj
[
@
id
]]
stubFunction
:
(
obj
,
fn
)
->
original
=
obj
[
fn
]
map
=
@
weakReference
obj
weakmap
=
@
weakmap
obj
[
fn
]
=
->
response
=
original
.
apply
obj
,
arguments
for
r
,
k
of
map
.
pointers
callback
()
for
callback
in
weakmap
[
r
]
?
.
callbacks
[
k
]
?
[]
response
@
weakmap
[
id
]
=
{}
observeMutations
:
(
obj
,
ref
,
keypath
)
->
if
Object
.
prototype
.
toString
.
call
(
obj
)
is
'[object Array]'
map
=
@
weakReference
obj
map
=
@
weakmap
[
obj
[
@
id
]]
unless
map
.
pointers
?
map
.
pointers
=
{}
functions
=
[
'push'
,
'pop'
,
'shift'
,
'unshift'
,
'sort'
,
'reverse'
,
'splice'
]
@
stubFunction
obj
,
fn
for
fn
in
functions
unless
map
[
keypath
]
?
map
[
keypath
]
=
[]
map
.
pointers
[
ref
]
?=
[]
unless
keypath
in
map
.
pointers
[
ref
]
map
.
pointers
[
ref
].
push
keypath
unobserveMutations
:
(
obj
,
ref
,
keypath
)
->
if
keyapths
=
@
weakReference
(
obj
).
pointers
?
[
ref
]
keypaths
.
splice
keypaths
.
indexOf
(
keypath
),
1
subscribe
:
(
obj
,
keypath
,
callback
)
->
callbacks
=
@
weakReference
(
obj
).
callbacks
unless
callbacks
[
keypath
]
?
callbacks
[
keypath
]
=
[]
value
=
obj
[
keypath
]
Object
.
defineProperty
obj
,
keypath
,
...
...
@@ -29,14 +60,17 @@ Rivets.adapters['.'] =
set
:
(
newValue
)
->
if
newValue
isnt
value
value
=
newValue
callback
()
for
callback
in
map
[
keypath
]
callback
()
for
callback
in
callbacks
[
keypath
]
unless
callback
in
callbacks
[
keypath
]
callbacks
[
keypath
].
push
callback
unless
callback
in
map
[
keypath
]
map
[
keypath
].
push
callback
@
observeMutations
obj
[
keypath
],
obj
[
@
id
],
keypath
unsubscribe
:
(
obj
,
keypath
,
callback
)
->
callbacks
=
@
weakmap
[
obj
[
@
id
]][
keypath
]
callbacks
=
@
weakmap
[
obj
[
@
id
]]
.
callbacks
[
keypath
]
callbacks
.
splice
callbacks
.
indexOf
(
callback
),
1
@
observeMutations
obj
[
keypath
],
obj
[
@
id
],
keypath
read
:
(
obj
,
keypath
)
->
obj
[
keypath
]
...
...
Please
register
or
sign in
to post a comment