Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Rewrite in JS to use JSDoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
robotlolita committed May 14, 2014
1 parent 7927575 commit d961465
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 203 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
*.orig
node_modules
lib
dist
npm-debug.log
docs/literate
docs
3 changes: 1 addition & 2 deletions .hgignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
syntax: glob
*.orig
node_modules
lib
dist
npm-debug.log
docs/literate
docs
84 changes: 50 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,52 +1,69 @@
bin = $(shell npm bin)
lsc = $(bin)/lsc
sjs = $(bin)/sjs
browserify = $(bin)/browserify
groc = $(bin)/groc
jsdoc = $(bin)/jsdoc
uglify = $(bin)/uglifyjs
VERSION = $(shell node -e 'console.log(require("./package.json").version)')

# -- Configuration -----------------------------------------------------
PACKGE = control.async
EXPORTS = folktale.control.async

lib: src/*.ls
$(lsc) -o lib -c src/*.ls
LIB_DIR = lib

TEST_DIR = test/specs-src
TEST_BLD = test/specs
TEST_SRC = $(wildcard $(TEST_DIR)/*.sjs)
TEST_TGT = ${TEST_SRC:$(TEST_DIR)/%.sjs=$(TEST_BLD)/%.js}


# -- Compilation -------------------------------------------------------
dist:
mkdir -p dist
mkdir -p $@

dist/$(PACKAGE).umd.js: $(LIB_DIR)/index.js dist
$(browserify) $< --standalone $(EXPORTS) > $@

dist/control.async.umd.js: compile dist
$(browserify) lib/index.js --standalone Async > $@
dist/$(PACKAGE).umd.min.js: dist/$(PACKAGE).umd.js
$(uglify) --mangle - < $< > $@

dist/control.async.umd.min.js: dist/control.async.umd.js
$(uglify) --mangle - < $^ > $@
$(TEST_BLD)/%.js: $(TEST_DIR)/%.sjs
mkdir -p $(dir $@)
$(sjs) --readable-names \
--module alright/macros \
--output $@ \
$<

# ----------------------------------------------------------------------
bundle: dist/control.async.umd.js

minify: dist/control.async.umd.min.js
# -- Tasks -------------------------------------------------------------
bundle: dist/$(PACKAGE).umd.js

compile: lib
minify: dist/$(PACKAGE).umd.min.js

documentation:
$(groc) --index "README.md" \
--out "docs/literate" \
src/*.ls test/*.ls test/specs/**.ls README.md
$(jsdoc) --configure jsdoc.conf.json
ABSPATH=$(shell cd "$(dirname "$0")"; pwd) $(MAKE) clean-docs

clean-docs:
perl -pi -e "s?$$ABSPATH/??g" ./docs/*.html

clean:
rm -rf dist build lib

test:
$(lsc) test/tap.ls

package: compile documentation bundle minify
mkdir -p dist/control.async-$(VERSION)
cp -r docs/literate dist/control.async-$(VERSION)/docs
cp -r lib dist/control.async-$(VERSION)
cp dist/*.js dist/control.async-$(VERSION)
cp package.json dist/control.async-$(VERSION)
cp README.md dist/control.async-$(VERSION)
cp LICENCE dist/control.async-$(VERSION)
cd dist && tar -czf control.async-$(VERSION).tar.gz control.async-$(VERSION)

publish: clean
rm -rf dist $(TEST_BLD)

test: $(TEST_TGT)
node test/node.js

package: documentation bundle minify
mkdir -p dist/$(PACKAGE)-$(VERSION)
cp -r docs dist/$(PACKAGE)-$(VERSION)
cp -r lib dist/$(PACKAGE)-$(VERSION)
cp dist/*.js dist/$(PACKAGE)-$(VERSION)
cp package.json dist/$(PACKAGE)-$(VERSION)
cp README.md dist/$(PACKAGE)-$(VERSION)
cp LICENCE dist/$(PACKAGE)-$(VERSION)
cd dist && tar -czf $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE)-$(VERSION)

publish: clean $(TGT)
npm install
npm publish

Expand All @@ -59,5 +76,4 @@ bump-feature:
bump-major:
VERSION_BUMP=MAJOR $(MAKE) bump


.PHONY: test
.PHONY: test bump bump-feature bump-major publish package clean documentation
23 changes: 23 additions & 0 deletions jsdoc.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"source": {
"include": [ "lib", "README.md" ]
},
"plugins": [ "plugins/markdown" ],
"markdown": {
"parser": "gfm"
},
"opts": {
"destination": "./docs/",
"template": "./node_modules/ink-docstrap/template"
},
"templates": {
"systemName": "control.async",
"theme": "cerulean",
"linenums": true,
"navType": "vertical",
"copyright": "© 2014 Quildreen Motta",
"default": {
"outputSourceFiles": true
}
}
}
90 changes: 90 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2014 Quildreen Motta <[email protected]>
//
// 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.

/**
* Core operations for asynchronous control flow.
*
* @module async/core
*/

// -- Dependencies -----------------------------------------------------
var Future = require('data.future')


// -- Implementation ---------------------------------------------------

/**
* Returns an action that always fails with the given data.
*
* @method
* @summary α → Future[α, β]
*/
exports.fail = fail
function fail(a) {
return new Future(function(reject, resolve) { reject(a) })
}


/**
* Resolves all futures in parallel, and collects all of their values.
*
* @method
* @summary Array[Future[α, β]] → Future[α, Array[β]]
*/
exports.parallel = parallel
function parallel(xs) { return new Future(function(reject, resolve) {
var len = xs.length
var result = new Array(len)
var resolved = false

xs.forEach(runComputation)

function runComputation(x, i) {
return x.fork( function(e) {
if (resolved) return
resolved = true
reject(e) }

, function(v) {
if (resolved) return
result[i] = v
len = len - 1
if (len === 0) { resolved = true
resolve(result) }})}
})}


/**
* Returns the value of the first resolved or rejected future.
*
* @method
* @summary Array[Future[α, β]] → Future[α, β]
*/
exports.nondeterministicChoice = nondeterministicChoice
function nondeterministicChoice(xs){ return new Future(function(reject, resolve) {
var resolved = false
xs.forEach(function(x){ x.fork( function(e){ transition(reject, e) }
, function(v){ transition(resolve, v) }) })

function transition(f, a) {
if (!resolved) { resolved = true
f(a) }}
})}
29 changes: 29 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2014 Quildreen Motta <[email protected]>
//
// 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.

/**
* @module async
*/
var extend = require('xtend')

module.exports = extend( require('./core')
, require('./time')
)
76 changes: 76 additions & 0 deletions lib/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2014 Quildreen Motta <[email protected]>
//
// 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.

/**
* Asynchronous actions related to time.
*
* @module async/time
*/

// -- Dependencies -----------------------------------------------------
var Future = require('data.future')
var flaw = require('flaw')


// -- Helpers ----------------------------------------------------------

/**
* Timeout, in ms.
*
* @private
* @summary Number → Error
*/
function TimeoutError(n) {
return flaw( 'TimeoutError'
, 'Timeoutted after ' + n + ' milliseconds.')
}

// -- Implementation ---------------------------------------------------

/**
* Returns a future that gets resolved after N milliseconds.
*
* The value of the future will be the delta of the time from its execution to
* the resolution.
*
* @method
* @summary Number → Future[α, Number]
*/
exports.delay = delay
function delay(n) { return new Future(function(reject, resolve) {
var s = new Date
setTimeout( function(){ resolve(new Date - s) }
, n)
})}


/**
* Returns a future that fails after N milliseconds.
*
* @method
* @summary Number → Future[TimeoutError, α]
*/
exports.timeout = timeout
function timeout(n) { return new Future(function(reject, resolve) {
var s = new Date
setTimeout( function(){ reject(TimeoutError(n)) }
, n)
})}
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
"description": "Operations for asynchronous control flow.",
"main": "lib/index.js",
"scripts": {
"test": "make test",
"prepublish": "make compile"
"test": "make test"
},
"repository": {
"type": "git",
"url": "https://github.com/folktale/control.async"
},
"keywords": [
"fantasy-land",
"concurrency",
"parallelism",
"async",
"folktale"
],
"author": "Quildreen Motta",
Expand All @@ -22,16 +24,17 @@
},
"dependencies": {
"flaw": "~0.1.0",
"data.future": "^2.0.0"
"data.future": "^2.0.0",
"xtend": "^3.0.0",
"jsdoc": "^3.3.0-alpha5",
"browserify": "^4.1.2"
},
"devDependencies": {
"browserify": "git://github.com/robotlolita/node-browserify",
"groc": "git://github.com/robotlolita/groc",
"LiveScript": "~1.2.0",
"hifive-tap": "~0.1.0",
"hifive": "~0.1.0",
"uglify-js": "~2.4.3",
"laws": "~0.2.0",
"claire": "~0.4.1"
"hifive-spec": "^0.1.1",
"sweet.js": "^0.6.0",
"alright": "^1.0.0-alpha2",
"ink-docstrap": "git://github.com/robotlolita/docstrap"
}
}
Loading

0 comments on commit d961465

Please sign in to comment.