Skip to content

Commit

Permalink
Merge ad6faf6 into 98a49ef
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Nov 15, 2017
2 parents 98a49ef + ad6faf6 commit 2020ef2
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 482 deletions.
25 changes: 3 additions & 22 deletions derivable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ declare module derivable {
export interface Derivable<T> {

derive<E>(f: (value: T) => E): Derivable<E>;
derive<A, E>(f: (value: T, a: A) => E, a: (A | Derivable<A>)): Derivable<E>;
derive<A, B, E>(f: (value: T, a: A, b: B) => E, a: (A | Derivable<A>), b: (B | Derivable<B>)): Derivable<E>;
derive<E>(f: (value: T, ...args: any[]) => E, ...args: any[]): Derivable<E>;

mDerive<E>(f: (value: T) => E): Derivable<E>;
mDerive<A, E>(f: (value: T, a: A) => E, a: (A | Derivable<A>)): Derivable<E>;
mDerive<A, B, E>(f: (value: T, a: A, b: B) => E, a: (A | Derivable<A>), b: (B | Derivable<B>)): Derivable<E>;
mDerive<E>(f: (value: T, ...args: any[]) => E, ...args: any[]): Derivable<E>;
maybeDerive<E>(f: (value: T) => E): Derivable<E>;

maybeDefault<E>(Derivable<E> | E): Derivable<T | E>

react(f: (value: T) => void, options?: Lifecycle<T>): void;

Expand Down Expand Up @@ -71,21 +67,6 @@ declare module derivable {

function derive<T>(f: () => T): Derivable<T>;

function derive<T, A>(f: (a: A) => T, a: A | Derivable<A>): Derivable<T>;

function derive<T, A, B>(f: (a: A, b: B) => T, a: A | Derivable<A>, b: B | Derivable<B>): Derivable<T>;

function derive<T, A, B, C>(f: (a: A, b: B, c: C) => T, a: A | Derivable<A>,
b: B | Derivable<B>, c: C | Derivable<C>): Derivable<T>;

function derive<T, A, B, C, D>(f: (a: A, b: B, c: C, d: D) => T, a: A | Derivable<A>,
b: B | Derivable<B>, c: C | Derivable<C>, d: D | Derivable<D>): Derivable<T>;

function derive<T>(f: (...args: any[])=> T, ...args: any[]): Derivable<T>

function derive(sections: string[], ...values: any[]): Derivable<string>


function proxy<T>(proxy: CompositeProxy<T>): Atom<T>;

function transact(f: () => void): void;
Expand Down
37 changes: 8 additions & 29 deletions derivable.js.flow
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
// @flow

export interface Derivable<T> {
export type Derivable<T> = {

derive<E>(f: (value: T) => E): Derivable<E>;
derive<A, E>(f: (value: T, a: A) => E, a: (A | Derivable<A>)): Derivable<E>;
derive<A, B, E>(f: (value: T, a: A, b: B) => E, a: (A | Derivable<A>), b: (B | Derivable<B>)): Derivable<E>;
derive<E>(f: (value: T, ...args: Array<mixed>) => E, ...args: Array<mixed>): Derivable<E>;

mDerive<E>(f: $NonMaybeType<T> => E): Derivable<E>;
maybeDerive<E>(f: $NonMaybeType<T> => E): Derivable<E>;

maybeDefault<E>(value: $NonMaybeType<E>): Derivable<$NonMaybeType<T> | E>;

react(f: (value: T) => void, options?: Lifecycle<T>): void;

mReact(f: (value: $NonMaybeType<T>) => void, options?: Lifecycle<T>): void;

get(): T;

map<E>(f: T => E): Derivable<E>;

mMap<E>(f: $NonMaybeType<T> => E): Derivable<E>;

is(other: mixed): Derivable<boolean>;

withEquality(equals: (a: T, b: T) => *): Derivable<T>;
};

export interface Atom<T> extends Derivable<T> {
export type Atom<T> = Derivable<T> & {

set(value: T): void;

Expand All @@ -33,21 +28,21 @@ export interface Atom<T> extends Derivable<T> {
proxy<E>(proxy: Proxy<T, E>): Atom<E>;
};

export interface Proxy<ParentType, ChildType> {
export type Proxy<ParentType, ChildType> = {

get(source: ParentType): ChildType;

set(source: ParentType, value: ChildType): ParentType;
};

export interface CompositeProxy<T> {
export type CompositeProxy<T> = {

get(): T;

set(value: T): void;
};

export interface Lifecycle<T> {
export type Lifecycle<T> = {

+from?: (((d: Derivable<T>) => boolean) | Derivable<boolean>);

Expand All @@ -64,20 +59,6 @@ declare export function atom<T>(value: T): Atom<T>;

declare export function derive<T>(f: () => T): Derivable<T>;

declare export function derive<T, A>(f: (a: A) => T, a: A | Derivable<A>): Derivable<T>;

declare export function derive<T, A, B>(f: (a: A, b: B) => T, a: A | Derivable<A>, b: B | Derivable<B>): Derivable<T>;

declare export function derive<T, A, B, C>(f: (a: A, b: B, c: C) => T, a: A | Derivable<A>,
b: B | Derivable<B>, c: C | Derivable<C>): Derivable<T>;

declare export function derive<T, A, B, C, D>(f: (a: A, b: B, c: C, d: D) => T, a: A | Derivable<A>,
b: B | Derivable<B>, c: C | Derivable<C>, d: D | Derivable<D>): Derivable<T>;

declare export function derive<T>(f: (...args: Array<mixed>)=> T, ...args: Array<mixed>): Derivable<T>

declare export function proxy<T>(proxy: CompositeProxy<T>): Atom<T>;

declare export function transact(f: () => void): void;

declare export function transaction<F: Function>(f: F): F;
Expand All @@ -98,8 +79,6 @@ declare export function isDerivation(obj: mixed): boolean;

declare export function isProxy(obj: mixed): boolean;

declare export function derive(strings: Array<string>, ...things: Array<mixed>): Derivable<string>;

declare export function wrapPreviousState<A, B>(fn: (currentState: A, previousState: A) => B, init?: A): (currentState: A) => B;

declare export function captureDereferences(fn: () => void): Array<Derivable<mixed>>;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "rollup -c && cross-env MINIFY=true rollup -c && cp derivable.d.ts derivable.js.flow dist/",
"lint": "eslint src test",
"test": "npm run build && jest && npm run lint",
"test-types": "cd test_flow && yarn && npm test",
"test-types": "cd test_flow && yarn && yarn test",
"bench": "node scripts/bench.js",
"coverage": "npm run build && jest --coverage",
"report-coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls",
Expand Down
20 changes: 0 additions & 20 deletions src/combinators.js

This file was deleted.

47 changes: 23 additions & 24 deletions src/derivable.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
import * as util from './util';
import {makeReactor} from './reactors';
import * as types from './types';
import {derive as _derive} from './derivation.js';
import {derive} from './derivation.js';
import {unpack} from './unpack';
import {map, mMap} from './combinators.js';

export var derivablePrototype = {
/**
* Creates a derived value whose state will always be f applied to this
* value
*/
derive: function (f, ...args) {
if (typeof f === 'function') {
return _derive(f, this, ...args);
} else {
throw Error('type error');
derive(f) {
if (typeof f !== 'function') {
throw Error('derive requires function');
}
return derive(() => f(this.get()));
},

map(f) {
return map(f, this);
maybeDerive(f) {
if (typeof f !== 'function') {
throw Error('maybeDerive requires function');
}
return derive(() => {
const arg = this.get();
return util.some(arg) ? f(arg) : null;
});
},

mMap(f) {
return mMap(f, this);
maybeDefault(def) {
if (!util.some(def)) {
throw Error('maybeDefault requires non-null value');
}
return this.derive(value => util.some(value) ? value : def);
},

react: function (f, opts) {
makeReactor(this, f, opts);
},

mReact: function (f, opts) {
var mWhen = _derive(() => Boolean(this.get()));
var mWhen = this.derive(Boolean);
if (opts && 'when' in opts && opts.when !== true) {
var when = opts.when;
if (typeof when === 'function' || when === false) {
when = _derive(when);
when = derive(when);
} else if (!types.isDerivable(when)) {
throw new Error('when condition must be bool, function, or derivable');
}
mWhen = mWhen.map(d => d && when.get());
mWhen = mWhen.derive(d => d && when.get());
}
return this.react(f, util.assign({}, opts, {when: mWhen}));
makeReactor(this, f, util.assign({}, opts, {when: mWhen}));
},

is: function (other) {
var x = this;
return _derive(function () {
return derive(function () {
return x.__equals(x.get(), unpack(other));
});
},

mDerive: function (...args) {
return _derive(() => util.some(this.get()) ? this.derive(...args).get() : null);
},

withEquality: function (equals) {
if (equals) {
if (typeof equals !== 'function') {
Expand Down
54 changes: 4 additions & 50 deletions src/derivation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as util from './util';
import * as parents from './parents';
import * as types from './types';
import {unpack} from './unpack';
import {CHANGED, UNCHANGED, UNKNOWN, DISCONNECTED} from './states';

export function Derivation (deriver) {
Expand Down Expand Up @@ -118,54 +117,9 @@ export function detach (parent, child) {
}
}

export function deriveFactory(f) {
return new Derivation(f);
}

const warnDeriveFn = (() => {
let called = false;
return () => {
if (!called) {
called = true;
console.warn('derive with arguments injection (derive((a) => {}, da) is deprecated. Use map method instead');
}
};
})();

export function derive (f, a, b, c, d) {
switch (arguments.length) {
case 0:
throw new Error('derive takes at least one argument');
case 1:
return deriveFactory(f);
case 2:
warnDeriveFn();
return deriveFactory(function () {
return f(unpack(a));
});
case 3:
warnDeriveFn();
return deriveFactory(function () {
return f(unpack(a), unpack(b));
});
case 4:
warnDeriveFn();
return deriveFactory(function () {
return f(unpack(a), unpack(b), unpack(c));
});
case 5:
warnDeriveFn();
return deriveFactory(function () {
return f(unpack(a),
unpack(b),
unpack(c),
unpack(d));
});
default:
warnDeriveFn();
var args = util.slice(arguments, 1);
return deriveFactory(function () {
return f.apply(null, args.map(unpack));
});
export function derive(f) {
if (typeof f !== 'function') {
throw Error('derive requires function');
}
return new Derivation(f);
}
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as parents from './parents';
import {deepUnpack, unpack} from './unpack';

export {isDerivable, isAtom, isProxy, isDerivation} from './types';
export {map, mMap} from './combinators.js';
export {transact, transaction, ticker, atomic, atomically} from './transactions';
export {Reactor as __Reactor} from './reactors';

Expand Down
2 changes: 1 addition & 1 deletion test/atom_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('the concurrent modification of _reactors bug', () => {
A_success = true;
}, { from: $A });

const $C = $A.map(a => a && $B.get());
const $C = $A.derive(a => a && $B.get());

$C.react(() => {
C_success = true;
Expand Down
Loading

0 comments on commit 2020ef2

Please sign in to comment.