-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb52502
commit 526c521
Showing
2 changed files
with
8,263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/*can-stream-kefir@1.1.0#can-stream-kefir*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-kefir', | ||
'can-compute', | ||
'can-stream', | ||
'can-symbol', | ||
'can-namespace' | ||
], function (require, exports, module) { | ||
'use strict'; | ||
var Kefir = require('can-kefir'); | ||
var compute = require('can-compute'); | ||
var canStream = require('can-stream'); | ||
var canSymbol = require('can-symbol'); | ||
var namespace = require('can-namespace'); | ||
var getValueDependenciesSymbol = canSymbol.for('can.getValueDependencies'); | ||
var getKeyDependenciesSymbol = canSymbol.for('can.getKeyDependencies'); | ||
var canStreamKefir = {}; | ||
canStreamKefir.toStream = function (compute) { | ||
var stream = Kefir.stream(function (emitter) { | ||
var changeHandler = function (ev, newVal) { | ||
emitter.emit(newVal); | ||
}; | ||
compute.on('change', changeHandler); | ||
var currentValue = compute(); | ||
if (currentValue !== undefined) { | ||
emitter.emit(currentValue); | ||
} | ||
return function () { | ||
compute.off('change', changeHandler); | ||
}; | ||
}); | ||
stream[getValueDependenciesSymbol] = function getValueDependencies() { | ||
return { valueDependencies: new Set([compute]) }; | ||
}; | ||
return stream; | ||
}; | ||
canStreamKefir.toCompute = function (makeStream, context) { | ||
var emitter, lastValue, streamHandler, lastSetValue; | ||
var setterStream = Kefir.stream(function (e) { | ||
emitter = e; | ||
if (lastSetValue !== undefined) { | ||
emitter.emit(lastSetValue); | ||
} | ||
}); | ||
var valueStream = makeStream.call(context, setterStream); | ||
var streamCompute = compute(undefined, { | ||
get: function () { | ||
return lastValue; | ||
}, | ||
set: function (val) { | ||
if (emitter) { | ||
emitter.emit(val); | ||
} else { | ||
lastSetValue = val; | ||
} | ||
return val; | ||
}, | ||
on: function (updated) { | ||
streamHandler = function (val) { | ||
lastValue = val; | ||
updated(); | ||
}; | ||
valueStream.onValue(streamHandler); | ||
}, | ||
off: function () { | ||
valueStream.offValue(streamHandler); | ||
} | ||
}); | ||
var _compute = streamCompute.computeInstance; | ||
_compute[getKeyDependenciesSymbol] = function getKeyDependencies(key) { | ||
if (key === 'change') { | ||
return { valueDependencies: new Set([valueStream]) }; | ||
} | ||
}; | ||
return streamCompute; | ||
}; | ||
if (!namespace.streamKefir) { | ||
module.exports = namespace.streamKefir = canStream(canStreamKefir); | ||
} | ||
}); |
Oops, something went wrong.