Skip to content

Commit

Permalink
add m.prop.mixin().
Browse files Browse the repository at this point in the history
  • Loading branch information
pygy committed Aug 1, 2016
1 parent 608a494 commit b8479f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ m.prop = Stream.stream
m.prop.combine = Stream.combine
m.prop.reject = Stream.reject
m.prop.merge = Stream.merge
m.prop.mixin = Stream.mixin
m.prop.HALT = Stream.HALT
m.render = renderService.render
m.redraw = redrawService.publish
Expand Down
17 changes: 15 additions & 2 deletions util/stream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict"

var guid = 0, noop = function() {}, HALT = {}
var guid = 0, noop = function() {}, HALT = {}, methodNames = [], methods = []
function createStream() {
function stream() {
if (arguments.length > 0) updateStream(stream, arguments[0], undefined)
Expand All @@ -18,6 +18,7 @@ function initStream(stream, args) {
stream.map = map, stream.ap = ap, stream.of = createStream
stream.valueOf = valueOf, stream.toJSON = toJSON, stream.toString = valueOf
stream.run = run, stream.catch = doCatch
for (var i = 0; i < methods.length; methods++) stream[methodNames[i]] = methods[i]

Object.defineProperties(stream, {
error: {get: function() {
Expand Down Expand Up @@ -174,6 +175,18 @@ function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [this,
function valueOf() {return this._state.value}
function toJSON() {return JSON.stringify(this._state.value)}

function mixin (extension) {
for (var name in extension) {
var i = methodNames.indexOf(name)
if (i === -1) {
methodNames.push(name)
methods.push(extension[name])
} else {
methods[i] = extension[name]
}
}
}

function active(stream) {return stream._state.state === 1}
function changed(stream) {return stream._state.changed}
function notEnded(stream) {return stream._state.state !== 2}
Expand All @@ -191,4 +204,4 @@ function merge(streams) {
}, streams)
}

module.exports = {stream: createStream, merge: merge, combine: combine, reject: reject, HALT: HALT}
module.exports = {stream: createStream, merge: merge, combine: combine, reject: reject, mixin: mixin, HALT: HALT}
7 changes: 7 additions & 0 deletions util/tests/test-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,13 @@ o.spec("stream", function() {
o(Stream.stream().toJSON()).equals(undefined)
})
})
o.spec("mixin", function() {
o("adds a method", function() {
Stream.mixin({self: function(){return this}})
var s = Stream.stream()
o(s.self()).equals(s)
})
})
o.spec("map", function() {
o("works", function() {
var stream = Stream.stream()
Expand Down

0 comments on commit b8479f4

Please sign in to comment.