Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to xstream #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions example/basic.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { run } from '@cycle/core';
import { run } from '@cycle/xstream-run';
import blessed from 'blessed';
import { makeTermDriver, box } from '../src';
import { Observable as $ } from 'rx';
import $ from 'xstream';

let screen = blessed.screen({ smartCSR: true, useBCE: true, title: 'Hello, World!' });

let BlueBox = text => box({ border: { type: 'line', fg: 'blue' } }, text);

run(({ term }) => ({
term: $.just(BlueBox('Hello, World!')),
term: $.of(BlueBox('Hello, World!')),
exit: term.on('key C-c')
}), {
term: makeTermDriver(screen),
exit: exit$ => exit$.forEach(::process.exit)
exit: exit$ => exit$.addListener({
next: ::process.exit,
error: ::process.exit,
complete: ::process.exit
})
});
8 changes: 6 additions & 2 deletions example/click.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { run } from '@cycle/core';
import { run } from '@cycle/xstream-run';
import blessed from 'blessed';
import { makeTermDriver, button } from '../src';
import { id, key, constant } from '../src/transform';
Expand Down Expand Up @@ -27,5 +27,9 @@ run(({ term: { on } }) => {
};
}, {
term: makeTermDriver(screen),
exit: exit$ => exit$.forEach(::process.exit)
exit: exit$ => exit$.addListener({
next: ::process.exit,
error: ::process.exit,
complete: ::process.exit
})
});
14 changes: 9 additions & 5 deletions example/input.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { run } from '@cycle/core';
import { run } from '@cycle/xstream-run';
import blessed from 'blessed';
import { makeTermDriver, form, textarea, text, button } from '../src';
import { id, view, key } from '../src/transform';
import { Observable as $ } from 'rx';
import $ from 'xstream';

// for the unfocus bug, see `src/index.js` for more info

Expand Down Expand Up @@ -62,13 +62,17 @@ run(({ term: { on } }) => {

let submit$ = on('*press', id('Submit'));

let result$ = text$.sample(submit$).startWith('');
let result$ = text$.map(::submit$.mapTo).flatten().startWith('');

return {
term: $.combineLatest(text$, result$, Form),
term: $.combine(Form, text$, result$),
exit: on('*keypress', key('C-c'))
};
}, {
term: makeTermDriver(screen),
exit: exit$ => exit$.forEach(::process.exit)
exit: exit$ => exit$.addListener({
next: ::process.exit,
error: ::process.exit,
complete: ::process.exit
})
});
12 changes: 8 additions & 4 deletions example/writer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { run } from '@cycle/core';
import { run } from '@cycle/xstream-run';
import blessed from 'blessed';
import { makeTermDriver, box } from '../src';
import { view, key } from '../src/transform';
Expand All @@ -16,15 +16,19 @@ let HelloBox = text => box({
}
});

run(({ term: { on } }) => {
run(({ term: { on } }) => {
let text$ = on('*keypress', view(1))
.startWith('').scan((str, char) => str + char);
.fold((str, char) => str + char, '');

return {
term: text$.map(HelloBox),
exit: on('*keypress', key('C-c'))
}
}, {
term: makeTermDriver(screen),
exit: exit$ => exit$.forEach(::process.exit)
exit: exit$ => exit$.addListener({
next: ::process.exit,
error: ::process.exit,
complete: ::process.exit
})
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"author": "synchronous",
"license": "MIT",
"dependencies": {
"rx": "^4.1.0"
"xstream": "^2.4.2"
},
"peerDependencies": {
"blessed": "^0.1.81"
},
"devDependencies": {
"@cycle/core": "^6.0.3",
"@cycle/xstream-run": "^1.1.0",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2"
},
Expand Down
33 changes: 20 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import blessed from 'blessed';
import { Observable as $ } from 'rx';
import $ from 'xstream';
import { isObject, singleton } from './util';

let nop = () => {};

let makeTermDriver = screen => {
let root = h('element', { keyable: true, clickable: true, children: [] });
screen.append(root);

return vt$ => {
vt$.forEach(vt => {
// TODO implement diffing
// blessed only performs simple diffing
// problem visible in `example/input.js`
// don't use blessed textarea for now;
// implement custom inputs as in `example/writer.js`
root.children = [];
root.append(vt);
screen.render();
vt$.addListener({
next: vt => {
// TODO implement diffing
// blessed only performs simple diffing
// problem visible in `example/input.js`
// don't use blessed textarea for now;
// implement custom inputs as in `example/writer.js`
root.children = [];
root.append(vt);
screen.render();
},
error: nop,
complete: nop
});

let makeEvent = node => {
Expand All @@ -35,9 +41,10 @@ let makeTermDriver = screen => {
// otherwise create a new listener
let stream = listeners[eventName]
? listeners[eventName]
: listeners[eventName] = $.create(o =>
void node.on(eventName, (...args) => o.onNext(args))
);
: listeners[eventName] = $.create({
start: sub => node.on(eventName, (...args) => sub.next(args)),
stop: nop
});

// if event is an object, apply the transforms
// otherwise just return the raw listener
Expand Down
6 changes: 3 additions & 3 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ export let id = i =>
s => s.filter(([el]) => el.options.id === i);

export let view = (...v) =>
s => s.pluck(...v);
s => v.reduce((s, k) => s.map(x => x[k]), s);

export let key = k =>
s => s.filter(([,,ek]) => ek.full === k);

export let constant = c =>
s => s.map(() => c);
s => s.mapTo(c);

export let init = (...i) =>
s => s.startWith(...i);

export let toggle = i =>
s => s.scan(a => !a, !i);
s => s.reduce(a => !a, !i);