Skip to content

Commit

Permalink
Version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Nov 5, 2012
0 parents commit 676bf59
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.4
- 0.5
- 0.6
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changes

## 0.0.1 / 2012-11-03

- Initial release
18 changes: 18 additions & 0 deletions License.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright 2012 Irakli Gozalishvili. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
56 changes: 56 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# signalr

[![Build Status](https://secure.travis-ci.org/Gozala/signalr.png)](http://travis-ci.org/Gozala/signalr)

[Functional Reactive Programming][FRP] (FRP) is a programming paradigm for
working with *time-varying* values, better capturing the temporal aspect of
mutable state. Signal is a data structure representing a time-varying value.
For example, consider the position of the mouse. The signal `mousePosition`
represents current mouse position. When the mouse moves, the value changes
automatically.

Signals can also be transformed and combined without typical hazards of the
stateful programs.

# Example

Signal is very low level construct that can be used to create signals from
scratch:

```js
var signal = require("signalr/signal")
var time = signal(function(next) {
setInterval(function() {
next(Date.now())
}, 1000)
})
```

Signals implement [Watchables][] abstraction which allows one to subscribe
and unsubscribe from signal changes:

```js
var watch = require("watchables/watch")
var unwatch = require("watchables/unwatch")

var end = Date.now() + 1000 * 5
watch(time, function onTimechange(value) {
console.log(value)
if (end - value <= 0) unwatch(time, onTimechange)
})

// => 1352077824718
// => 1352077825719
// => 1352077826720
// => 1352077827721
// => 1352077828722
// => 1352077829723
```


## Install

npm install signalr

[FRP]:http://en.wikipedia.org/wiki/Functional_reactive_programming
[Watchables]:https://github.com/Gozala/watchables
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

exports.signal = require("./signal")
exports.send = require("./send")
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "signalr",
"id": "signalr",
"version": "0.0.1",
"description": "Signals in a functional reactive style",
"keywords": [
"signalr", "FRP", "functional", "reactive", "signal"
],
"author": "Irakli Gozalishvili <rfobic@gmail.com> (http://jeditoolkit.com)",
"homepage": "https://github.com/Gozala/signalr",
"repository": {
"type": "git",
"url": "https://github.com/Gozala/signalr.git",
"web": "https://github.com/Gozala/signalr"
},
"bugs": {
"url": "http://github.com/Gozala/signalr/issues/"
},
"devDependencies": {
"test": "~0.x.0",
"phantomify": "~0.x.0",
"repl-utils": "~1.0.0"
},
"main": "./index.js",
"scripts": {
"repl": "node node_modules/repl-utils",
"test": "npm run test-node && npm run test-browser",
"test-browser": "node ./node_modules/phantomify/bin/cmd.js ./test/index.js",
"test-node": "node ./test/index.js"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/Gozala/signalr/License.md"
}
],
"dependencies": {
"method": "~0.1.1",
"event": "0.0.1",
"watchables": "0.0.3"
}
}
3 changes: 3 additions & 0 deletions send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("event/send")
70 changes: 70 additions & 0 deletions signal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"use strict";

var watch = require("watchables/watch")
var unwatch = require("watchables/unwatch")
var event = require("event/event")
var send = require("event/send")

function Signal() {
/**
Signal is a data structure representing a time-varying value. For example,
consider the position of the mouse. The signal `mousePosition` represents
current mouse position. When the mouse moves, the value changes automatically.
**/
this[source] = event()
}

// Signal type has internal property that represents it's current state also
// `valueOf` method is made to return it.
var state = "state@" + module.id
Object.defineProperty(Signal.prototype, state, {
value: void(0), enumerable: false, configurable: false, writable: true
})
Object.defineProperty(Signal.prototype, "valueOf", {
value: function valueOf() { return this[state] }
})

// Signal type has internal property that is `event` stream of it's values
// changes.
var source = "event@" + module.id
Object.defineProperty(Signal.prototype, source, {
value: void(0), enumerable: false, configurable: false, writable: true
})

// Signal implements `watch` and `unwatch` that can be used to subscribe /
// unsubscribe from value changes.
watch.define(Signal, function watchSignal(signal, watcher) {
return watch(signal[source], watcher)
})
unwatch.define(Signal, function unwatchSignal(signal, watcher) {
return unwatch(signal[source], watcher)
})

function signal(generator) {
/**
Signal takes `generator` function and passes `next` function into it
calling which will emit new values on resulting signal.
## Example
var time = signal(function(next) {
setInterval(function() {
next(Time.now())
), 1000)
})
watch(time, console.log)
**/
var result = new Signal()
var event = result[source]
generator(function next(value) {
if (result[state] !== value) {
result[state] = value
send(event, value)
}
}, result)
return result
}
signal.type = Signal

module.exports = signal
86 changes: 86 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"use strict";

var signal = require("../signal")
var watch = require("watchables/watch")
var unwatch = require("watchables/unwatch")

exports["test signal events"] = function(assert, done) {
var actual = []
var s = signal(function(next) {
setTimeout(function() {
next(1)
next(2)
next(3)
assert.deepEqual(actual, [1, 2, 3], "all values were accumulated")
done()
})
})

watch(s, function(value) {
actual.push(value)
})
}

exports["test signal only changes"] = function(assert, done) {
var actual = []
var s = signal(function(next) {
setTimeout(function() {
next(1)
next(1)
next(2)
next(2)
next(2)
next(3)
next(3)
assert.deepEqual(actual, [1, 2, 3], "all values were accumulated")
done()
})
})

watch(s, function(value) {
actual.push(value)
})
}

exports["test signal valueOf"] = function(assert, done) {
var actual = []
var s = signal(function(next) {
setTimeout(function() {
next(1)
assert.deepEqual(actual, [1], "first value accumulated")
assert.equal(s.valueOf(), 1, "currently value is 1")
next(2)
assert.deepEqual(actual, [1, 2], "second value accumulated")
assert.equal(s.valueOf(), 2, "currently value is 2")
next(3)
assert.deepEqual(actual, [1, 2, 3], "all values were accumulated")
assert.equal(s.valueOf(), 3, "currently value is 3")
done()
})
})

watch(s, function(value) {
actual.push(value)
})
}

exports["test signal self argument"] = function(assert, done) {
var actual = []
signal(function(next, self) {
watch(self, function(value) { actual.push(value) })
setTimeout(function() {
next(1)
assert.deepEqual(actual, [1], "first value accumulated")
assert.equal(self.valueOf(), 1, "currently value is 1")
next(2)
assert.deepEqual(actual, [1, 2], "second value accumulated")
assert.equal(self.valueOf(), 2, "currently value is 2")
next(3)
assert.deepEqual(actual, [1, 2, 3], "all values were accumulated")
assert.equal(self.valueOf(), 3, "currently value is 3")
done()
})
})
}

require("test").run(exports)

0 comments on commit 676bf59

Please sign in to comment.