From 008ffc95870d6b445c672036d94878c52617fdb2 Mon Sep 17 00:00:00 2001 From: Gilbert Date: Tue, 12 Jul 2016 22:57:09 -0500 Subject: [PATCH] Add m.prop.sync --- docs/v1.x-migration.md | 55 +++++++++++++++++++++++++++++++++++++++ index.js | 1 + util/stream.js | 10 ++++++- util/tests/test-stream.js | 25 ++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/docs/v1.x-migration.md b/docs/v1.x-migration.md index c0dd5476..ba4b0c14 100644 --- a/docs/v1.x-migration.md +++ b/docs/v1.x-migration.md @@ -11,6 +11,7 @@ - [Reading/writing the current route](#readingwriting-the-current-route) - [Accessing route params](#accessing-route-params) - [Setting route prefix](#setting-route-prefix) +- [m.request](#mrequest) ## `config` function @@ -270,3 +271,57 @@ m.route.mode = "pathname"; ```js m.route.prefix(""); ``` + +## m.request + +[m.request](request.md) now returns an [m.prop stream](prop.md) instead of a promise. The main difference is you'll have to use `.run` to get similar functionality as a promise's `.then`: + +### `v0.2.x` + +```js +m.request({ method: 'GET', url: 'https://api.github.com/' }) + .then(function (responseBody) { + return m.request({ method: 'GET', url: responseBody.emojis_url }); + }) + .then(function (emojis) { + console.log("+1 url:", emojis['+1']); + }); +``` + +### `v1.x` + +```js +m.request({ method: 'GET', url: 'https://api.github.com/' }) + .run(function (responseBody) { + return m.request({ method: 'GET', url: responseBody.emojis_url }); + }) + .run(function (emojis) { + console.log("+1 url:", emojis['+1']); + }); +``` + +The equivalent of `m.sync` is now `m.prop.sync`: + +### `v0.2.x` + +```js +m.sync([ + m.request({ method: 'GET', url: 'https://api.github.com/users/lhorie' }), + m.request({ method: 'GET', url: 'https://api.github.com/users/isiahmeadows' }), +]) + .then(function (users) { + console.log("Contributors:", users[0].name, "and", users[1].name); + }); +``` + +### `v1.x` + +```js +m.prop.sync([ + m.request({ method: 'GET', url: 'https://api.github.com/users/lhorie' }), + m.request({ method: 'GET', url: 'https://api.github.com/users/isiahmeadows' }), +]) + .run(function (users) { + console.log("Contributors:", users[0].name, "and", users[1].name); + }); +``` diff --git a/index.js b/index.js index 139be0be..151d3d68 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,7 @@ m.route = require("./api/router")(window, renderService, redrawService) m.mount = require("./api/mount")(renderService, redrawService) m.trust = require("./render/trust") m.prop = Stream.stream +m.prop.sync = Stream.sync m.prop.combine = Stream.combine m.prop.reject = Stream.reject m.prop.HALT = Stream.HALT diff --git a/util/stream.js b/util/stream.js index 1897f72b..e7d77c4c 100644 --- a/util/stream.js +++ b/util/stream.js @@ -174,4 +174,12 @@ function reject(e) { return stream } -module.exports = {stream: createStream, combine: combine, reject: reject, HALT: HALT} +function sync (streams) { + return combine(function () { + return Array.prototype.slice + .call(arguments, 0, arguments.length-1) + .map(function (s) { return s() }) + }, streams) +} + +module.exports = {stream: createStream, sync: sync, combine: combine, reject: reject, HALT: HALT} diff --git a/util/tests/test-stream.js b/util/tests/test-stream.js index fabbf771..e7687f85 100644 --- a/util/tests/test-stream.js +++ b/util/tests/test-stream.js @@ -166,6 +166,31 @@ o.spec("stream", function() { o(b()).equals(undefined) }) }) + o.spec("sync", function() { + o("transforms an array of streams to an array of values", function() { + var all = Stream.sync([ + Stream.stream(10), + Stream.stream("20"), + Stream.stream({ value: 30 }), + ]) + + o(all()).deepEquals([10, "20", { value: 30 }]) + }) + o("remains pending until all streams are active", function() { + var straggler = Stream.stream() + + var all = Stream.sync([ + Stream.stream(10), + Stream.stream("20"), + straggler, + ]) + + o(all()).equals(undefined) + + straggler(30) + o(all()).deepEquals([10, "20", 30]) + }) + }) o.spec("end", function() { o("end stream works", function() { var stream = Stream.stream()