@leansdk/leanrc
Version:
LeanRC is a MVC framework for creating graceful applications
200 lines (173 loc) • 5.84 kB
text/coffeescript
# This file is part of LeanRC.
#
# LeanRC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LeanRC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with LeanRC. If not, see <https://www.gnu.org/licenses/>.
# должен имплементировать интерфейс CursorInterface
# является оберткой над обычным массивом
# TODO: от Игоря предложение, сделать свойство isClosed
module.exports = (Module)->
{
AnyT, PointerT
FuncG, MaybeG
CollectionInterface, CursorInterface
CoreObject
Utils: { _ }
} = Module::
class Cursor extends CoreObject
CursorInterface
Module
ipnCurrentIndex = PointerT currentIndex: Number,
default: 0
iplArray = PointerT array: AnyT
ipoCollection = PointerT collection: MaybeG CollectionInterface
isClosed: Boolean,
default: false
setCollection: FuncG(CollectionInterface, CursorInterface),
default: (aoCollection)->
@[ipoCollection] = aoCollection
return @
setIterable: FuncG(AnyT, CursorInterface),
default: (alArray)->
@[iplArray] = alArray
return @
toArray: FuncG([], Array),
default: ->
while yield
yield
next: FuncG([], MaybeG AnyT),
default: ->
data = yield Module::Promise.resolve @[iplArray][@[ipnCurrentIndex]]
@[ipnCurrentIndex]++
switch
when not data?
yield return data
when @[ipoCollection]?
return yield @[ipoCollection].normalize data
else
yield return data
hasNext: FuncG([], Boolean),
default: ->
yield Module::Promise.resolve not _.isNil @[iplArray][@[ipnCurrentIndex]]
close: Function,
default: ->
for item, i in @[iplArray]
delete @[iplArray][i]
delete @[iplArray]
yield return
count: FuncG([], Number),
default: ->
array = @[iplArray] ? []
yield Module::Promise.resolve array.length?() ? array.length
forEach: FuncG(Function),
default: (lambda)->
index = 0
try
while yield
yield lambda (yield ), index++
return
catch err
yield
throw err
map: FuncG(Function, Array),
default: (lambda)->
index = 0
try
while yield
yield lambda (yield ), index++
catch err
yield
throw err
filter: FuncG(Function, Array),
default: (lambda)->
index = 0
records = []
try
while yield
record = yield
if yield lambda record, index++
records.push record
records
catch err
yield
throw err
find: FuncG(Function, MaybeG AnyT),
default: (lambda)->
index = 0
_record = null
try
while yield
record = yield
if yield lambda record, index++
_record = record
break
_record
catch err
yield
throw err
compact: FuncG([], Array),
default: ->
results = []
try
while @[ipnCurrentIndex] < yield
rawResult = @[iplArray][@[ipnCurrentIndex]]
++@[ipnCurrentIndex]
unless _.isEmpty rawResult
result = switch
when @[ipoCollection]?
yield @[ipoCollection].normalize rawResult
else
rawResult
results.push result
yield return results
catch err
yield
throw err
reduce: FuncG([Function, AnyT], AnyT),
default: (lambda, initialValue)->
try
index = 0
_initialValue = initialValue
while yield
_initialValue = yield lambda _initialValue, (yield ), index++
_initialValue
catch err
yield
throw err
first: FuncG([], MaybeG AnyT),
default: ->
try
result = if yield
yield
else
null
yield
yield return result
catch err
yield
throw err
restoreObject: Function,
default: ->
throw new Error "restoreObject method not supported for #{@name}"
yield return
replicateObject: Function,
default: ->
throw new Error "replicateObject method not supported for #{@name}"
yield return
init: FuncG([MaybeG(CollectionInterface), MaybeG Array]),
default: (aoCollection = null, alArray = null)->
arguments...
@[ipoCollection] = aoCollection if aoCollection?
@[iplArray] = alArray ? []
return