cyclejs-group
Advanced tools
Comparing version 0.3.6 to 0.3.7
{ | ||
"name": "cyclejs-group", | ||
"version": "0.3.6", | ||
"version": "0.3.7", | ||
"author": "Eryk Napierala", | ||
@@ -18,3 +18,3 @@ "description": "Utility for CycleJS framework for reducing boilerplate when creating groups of streams", | ||
], | ||
"main": "dist/create-streams-group.js", | ||
"main": "dist/create-group.js", | ||
"dependencies": { | ||
@@ -49,4 +49,4 @@ "core-js": "^0.9.6", | ||
"test": "npm run jshint && npm run mocha", | ||
"browserify": "browserify src/create-streams-group.js -t babelify --standalone createStreamsGroup --outfile dist/cyclejs-group.js", | ||
"uglify": "uglifyjs dist/cycle.js -o dist/cycle.min.js", | ||
"browserify": "browserify src/create-group.js -t babelify --standalone createGroup --outfile dist/cyclejs-group.js", | ||
"uglify": "uglifyjs dist/cyclejs-group.js -o dist/cyclejs-group.min.js", | ||
"dist": "npm run browserify && npm run uglify", | ||
@@ -53,0 +53,0 @@ "precompile": "rm -rf dist/ && mkdir -p dist", |
169
README.md
@@ -5,7 +5,7 @@ # cyclejs-group | ||
## Why may I need it? | ||
Usually ina Cycle.js application or component you want to create more than one stream, especially for intent and model parts. It's 100% possible to do it with pure JS, but it requires a lot of boilerplate code. This utility covers common case and lets creating complicated programs easily. | ||
Usually in a Cycle.js application or component you want to create more than one stream, especially for intent and model parts. It's 100% possible to do it with pure JS, but it requires a lot of boilerplate code. This utility covers common case and makes creating complicated programs easier. | ||
## Example usage | ||
Let's say, you want to create simple application, that allows you to add two numbers. With pure JS and Cycle.js you can do it like this: | ||
Let's say, you want to create simple application, that allows you to add two numbers. With pure Cycle.js you can do it like this: | ||
@@ -16,53 +16,52 @@ ```javascript | ||
let a$ = createStream((changeA$) => changeA$ | ||
.map(value => parseInt(value, 10)) | ||
.filter(value => !isNaN(value)) | ||
.startWith(1) | ||
.distinctUntilChanged() | ||
.map(value => parseInt(value, 10)) | ||
.filter(value => !isNaN(value)) | ||
.startWith(1) | ||
.distinctUntilChanged() | ||
); | ||
let b$ = createStream((changeB$) => changeB$ | ||
.map(value => parseInt(value, 10)) | ||
.filter(value => !isNaN(value)) | ||
.startWith(1) | ||
.distinctUntilChanged() | ||
.map(value => parseInt(value, 10)) | ||
.filter(value => !isNaN(value)) | ||
.startWith(1) | ||
.distinctUntilChanged() | ||
); | ||
let c = createStream((a$, b$) => Rx.Observable.combineLatest( | ||
a$, | ||
b$, | ||
(a, b) => a + b | ||
a$, b$, | ||
(a, b) => a + b | ||
)); | ||
let vtree$ = createStream((a$, b$, c$) => | ||
Rx.Observable.combineLatest(a$, b$, c$, (a, b, c) => | ||
h('form', | ||
h('fieldset', [ | ||
h('legend', 'Add two numbers'), | ||
h('input#a', { | ||
type: 'number', | ||
value: a, | ||
}), | ||
h('input#b', { | ||
type: 'number', | ||
value: b, | ||
}), | ||
h('output', { | ||
value: c, | ||
htmlFor: 'a,b' | ||
}) | ||
]) | ||
Rx.Observable.combineLatest(a$, b$, c$, (a, b, c) => | ||
h('form', | ||
h('fieldset', [ | ||
h('legend', 'Add two numbers'), | ||
h('input#a', { | ||
type: 'number', | ||
value: a, | ||
}), | ||
h('input#b', { | ||
type: 'number', | ||
value: b, | ||
}), | ||
h('output', { | ||
value: c, | ||
htmlFor: 'a,b' | ||
}) | ||
]) | ||
) | ||
) | ||
) | ||
); | ||
let changeA$ = createStream((interaction$) => | ||
interaction$ | ||
.choose('#a', 'input') | ||
.map(({ target }) => target.value) | ||
interaction$ | ||
.choose('#a', 'input') | ||
.map(({ target }) => target.value) | ||
); | ||
let changeA$ = createStream((interaction$) => | ||
interaction$ | ||
.choose('#b', 'input') | ||
.map(({ target }) => target.value) | ||
interaction$ | ||
.choose('#b', 'input') | ||
.map(({ target }) => target.value) | ||
); | ||
@@ -81,64 +80,64 @@ | ||
Seems easy for now, but when streams number grows, amount of boilerplate will grow proportionally. With `createStreamsGroup` you can achieve the same effect in more compact way and create batch of streams from plain functions. Each stream in the group will be injected input streams given by the `inject` method of the group, where this connection is detected based on names of function parameters and keys of the group object. | ||
Seems easy for now, but when streams number grows, amount of boilerplate will grow proportionally. With `createGroup` you can achieve the same effect in more compact way and create batch of streams from plain functions. Thanks to `inject` method of the group, you can make streams form one group available for streams from another one. Connection is detected based on names of function parameters and keys of the group object. | ||
```javascript | ||
import { createStream, render, h, Rx } from 'cyclejs'; | ||
import createStreamsGroup from 'cyclejs-create-streams-group'; | ||
import createGroup from 'cyclejs-group'; | ||
let model = createStreamsGroup({ | ||
a$: (changeA$) => changeA$ | ||
.map(value => parseInt(value, 10)) | ||
.filter(value => !isNaN(value)) | ||
.startWith(1) | ||
.distinctUntilChanged() | ||
b$: (changeB$) => changeB$ | ||
.map(value => parseInt(value, 10)) | ||
.filter(value => !isNaN(value)) | ||
.startWith(1) | ||
.distinctUntilChanged(), | ||
c$: (a$, b$) => Rx.Observable.combineLatest( | ||
a$, | ||
b$, | ||
(a, b) => a + b | ||
let model = createGroup({ | ||
a$: (changeA$) => changeA$ | ||
.map(value => parseInt(value, 10)) | ||
.filter(value => !isNaN(value)) | ||
.startWith(1) | ||
.distinctUntilChanged() | ||
b$: (changeB$) => changeB$ | ||
.map(value => parseInt(value, 10)) | ||
.filter(value => !isNaN(value)) | ||
.startWith(1) | ||
.distinctUntilChanged(), | ||
c$: (a$, b$) => Rx.Observable.combineLatest( | ||
a$, b$, | ||
(a, b) => a + b | ||
) | ||
}); | ||
let intent = createStreamsGroup({ | ||
changeA$: (interaction$) => interaction$ | ||
.choose('#a', 'input') | ||
.map(({ target }) => target.value), | ||
changeB$: (interaction$) => interaction$ | ||
.choose('#b', 'input') | ||
.map(({ target }) => target.value) | ||
let intent = createGroup({ | ||
changeA$: (interaction$) => interaction$ | ||
.choose('#a', 'input') | ||
.map(({ target }) => target.value), | ||
changeB$: (interaction$) => interaction$ | ||
.choose('#b', 'input') | ||
.map(({ target }) => target.value) | ||
}); | ||
let view = createStreamsGroup({ | ||
vtree$: (a$, b$, c$) => Rx.Observable.combineLatest( | ||
a$, b$, c$, | ||
(a, b, c) => | ||
h('form', | ||
h('fieldset', [ | ||
h('legend', 'Add two numbers'), | ||
h('input#a', { | ||
type: 'number', | ||
value: a, | ||
}), | ||
h('input#b', { | ||
type: 'number', | ||
value: b, | ||
}), | ||
h('output', { | ||
value: c, | ||
htmlFor: 'a,b' | ||
}) | ||
]) | ||
let view = createGroup({ | ||
vtree$: (a$, b$, c$) => Rx.Observable.combineLatest( | ||
a$, b$, c$, | ||
(a, b, c) => | ||
h('form', | ||
h('fieldset', [ | ||
h('legend', 'Add two numbers'), | ||
h('input#a', { | ||
type: 'number', | ||
value: a, | ||
}), | ||
h('input#b', { | ||
type: 'number', | ||
value: b, | ||
}), | ||
h('output', { | ||
value: c, | ||
htmlFor: 'a,b' | ||
}) | ||
] | ||
) | ||
) | ||
) | ||
) | ||
}); | ||
let user = createStreamsGroup({ | ||
interaction$: (vtree$) => render(vtree$, document.body).interaction$ | ||
let user = createGroup({ | ||
interaction$: (vtree$) => render(vtree$, document.body).interaction$ | ||
}); | ||
model.inject(intent); | ||
model.inject(intent, model); // self-injection to make a$ and b$ available for c$ | ||
view.inject(model); | ||
@@ -145,0 +144,0 @@ user.inject(view); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24353
144
1