UNPKG

4.2 kBPlain TextView Raw
1N = require '../..'
2ChangeWatcher = require './ChangeWatcher'
3Q = require 'q'
4async = require 'async'
5
6polyparams = require \polyparams
7cache = require \./Cache
8
9watchers = []
10
11global import require \prelude-ls
12
13class Wrappers
14
15 @_FindDone = -> it |> find-index is-type \Function
16
17 @_WrapFlipDone = (cb) ->
18 if not N.config.flipDone
19 return cb
20
21 resource = @
22
23 (...args) ->
24
25 doneIdx = resource._FindDone args
26 if not doneIdx?
27 return cb.apply @, args
28
29 oldDone = args[doneIdx]
30
31 args[doneIdx] = (err, data) ->
32 if err?
33 resource.error err
34 return oldDone data, err
35
36 resource.error null, false
37
38 oldDone data, err
39
40 cb.apply @, args
41
42 @_WrapPromise = (cb) ->
43 d = null
44
45 _FindDone = @_FindDone
46
47 resource = @
48
49 (...args) ->
50 idx = _FindDone args
51
52 if not idx?
53 d = Q.defer()
54
55 args.push (err, data) ->
56 return d.reject err if err?
57
58 d.resolve data
59
60 ret = cb.apply @, args
61
62 # console.log 'Wrap Promise', d, @
63 if d? and @Init?
64 @Init!
65 new @ d
66 else if d? #and @_promise?
67 @_promise = d.promise
68 @
69 else if not d? and ret?.state? and @_type?
70 @_promise = ret
71 @
72 else
73 ret
74
75 @_WrapResolvePromise = (cb) ->
76 findDone = @_FindDone
77 (...args) ->
78 doneIdx = findDone args
79 oldDone = args[doneIdx]
80 if @_promise?
81 # console.log 'WrapResolvePromise', @
82 @
83 .Then ~> cb.apply it, args
84 .Catch oldDone
85 else
86 cb.apply @, args
87
88 @_WrapResolveArgPromise = (cb) ->
89 findDone = @_FindDone
90 (...args) ->
91 doneIdx = findDone args
92 oldDone = args[doneIdx]
93 async.map args, (arg, done) ->
94 if arg?._promise?
95 arg
96 .Then -> done null it
97 .Catch done
98 else
99 done null arg
100 , (err, results) ~>
101 return oldDone err if oldDone? and err?
102
103 cb.apply @, results
104 @
105
106 @_WrapWatchArgs = (cb) ->
107 (...args) ->
108
109 if not N.Watch.active
110 return cb.apply @, args
111
112 if not ChangeWatcher.Watch cb, args, @
113 return cb.apply @, args
114
115 @_WrapDebugError = (debug, cb) ->
116
117 resource = @
118
119 (...args) ->
120
121 doneIdx = resource._FindDone args
122 if not doneIdx?
123 return cb.apply @, args
124
125 oldDone = args[doneIdx]
126
127 args[doneIdx] = (err, data) ->
128 if err?
129 debug JSON.stringify err
130 # Debug.UnDepth!
131 return oldDone err, data
132
133 oldDone err, data
134
135 cb.apply @, args
136
137 @_WrapParams = (...types) ->
138
139 (...args) ->
140 _cb = polyparams.apply @, types
141 _cb.apply @, args
142
143 @_WrapCache = (name, cb) ->
144
145 if not N.config?.cache
146 return (...args) ->
147 cb.apply @, args
148
149 fullName = name
150 (...args) ->
151 Resource = @
152 name = @lname + fullName
153 doneIdx = @_FindDone args
154 _oldDone = args[doneIdx]
155 first = true
156 oldDone = (err, res) ->
157 if first
158 first := false
159 _oldDone err, res
160 else
161 0
162
163 if is-type \Array args[0] or is-type \Object args[0]
164 name += JSON.stringify args[0]
165 else if is-type \Number args[0]
166 name += args[0]
167 else
168 name += '{}'
169
170 cache.Get name, (err, cached) ~>
171
172 if not err? and cached?
173 cached = JSON.parse cached
174 if is-type \Array cached
175 cached = cached |> map -> Resource.Hydrate it
176 else
177 cached = Resource.Hydrate cached
178 return oldDone null, cached
179
180 return oldDone err if err?
181
182 args[doneIdx] = (err, res) ~>
183 if err?
184 return oldDone err
185
186 if is-type \Array res
187 toStore = res
188 else
189 toStore = obj-to-pairs res |> filter (.0.0 isnt \_) |> pairs-to-obj
190 cache.Set name, JSON.stringify(toStore), (err, status) ~>
191 return oldDone err if err?
192
193 oldDone null, res
194
195 watchers.push N.Watch ~>
196 cb.apply @, args
197
198 @Reset = ->
199 watchers |> each (.Stop!)
200 watchers := []
201
202module.exports = Wrappers