This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsnapshot.coffee
More file actions
executable file
·124 lines (99 loc) · 3.11 KB
/
Copy pathsnapshot.coffee
File metadata and controls
executable file
·124 lines (99 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{EventEmitter} = require 'events'
{Model,daisy,dash} = require 'bongo'
Traverse = require 'traverse'
class SnapshotFeed extends Model
@share()
@set
sharedMethods :
static : ['joinFeed']
schema :
items : [String]
@joinFeed =(name, callback)->
@one {name}, (err, feed)->
if err
callback err
else unless feed?
callback new Error 'No feed.'
else
out = []
Object
.keys(feed.items)
.sort((a, b)-> a - b)
.forEach (key)->
out.push feed.items[key]
callback null, "feed:#{out.join('\n')}"
module.exports = class Snapshot
watchedConstructors_ = []
@watchConstructor =(constructor)->
watchedConstructors_.push constructor
@isWatching =(constructor)->
constructor in watchedConstructors_
createSnapshot =(model, errs, callback)->
if model.fetchSubject?
model.fetchSubject (err, subject)->
if err
errs.push err
callback()
else
unless subject?
callback new Error 'No subject!'
else unless 'function' is typeof subject.fetchTeaser
model.update $set: snapshot: JSON.stringify(subject), -> callback()
else
subject.fetchTeaser? (err, teaser)->
if err
errs.push err
else
model.update $set: snapshot: JSON.stringify(teaser), -> callback()
else
model.update $set: snapshot: JSON.stringify(model)
callback()
updateSnapshots =(errs, queue, models)->
for model in models
queue.push createSnapshot.bind null, model, errs, -> queue.fin()
updateAllSnapshots =(id, callback)->
queue = []
errs = []
constructor = watchedConstructors_[0]
constructor?.all snapshotIds: id, (err, models)->
updateSnapshots errs, queue, models if models.length
dash queue, ->
if errs.length
[err] = errs
else
err = null
callback? err
@clearById =(id, callback)->
updateAllSnapshots id, callback
@create = (node, callback=->)->
createSnapshot node, [], callback
@capture = (node, teaser, index, callback)->
delete node.data?.snapshot unless teaser?
snapshot = new Snapshot(teaser ? node)
# {encapsulatedBy, name} = node.constructor
# {name} = encapsulatedBy if encapsulatedBy
node.update $set:
snapshot : snapshot.json
snapshotIds : snapshot.ids
, (err)->
if err
callback err
else
callback null, snapshot.json
# operation = $set: {}
# operation.$set["items.#{index}"] = snapshot.json
# SnapshotFeed.update {name}, operation, {
# upsert: yes
# }, (err)->
# if err
# callback err
# else
# callback null, snapshot.json
collectIds =(teaser, ids=[])->
new Traverse(teaser).map (node)->
if @key is '_id' then ids.push node
return ids
constructor:(@teaser)->
@json = JSON.stringify teaser
@ids = collectIds(teaser)
toString:-> @json