Skip to content

Commit

Permalink
refactor: update Fn args in various packages
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 7, 2019
1 parent 5a5a2d1 commit e453ac3
Show file tree
Hide file tree
Showing 40 changed files with 194 additions and 132 deletions.
4 changes: 2 additions & 2 deletions packages/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ export type Watch<T> = (id: string, oldState: T, newState: T) => void;
export interface IAssociative<K, V, T> {
assoc(key: K, val: V): T;
assocIn(key: K[], val: V): T;
update(key: K, f: (v: V) => V): T;
updateIn(key: K[], f: (v: V) => V): T;
update(key: K, f: Fn<V, V>): T;
updateIn(key: K[], f: Fn<V, V>): T;
}

/**
Expand Down
7 changes: 2 additions & 5 deletions packages/api/src/mixins/iwatch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { IWatch } from "../api";
import { Fn3, IWatch } from "../api";
import { mixin } from "../mixin";

export const IWatchMixin = mixin(<IWatch<any>>{
addWatch(
id: string,
fn: (id: string, oldState: any, newState: any) => void
) {
addWatch(id: string, fn: Fn3<string, any, any, void>) {
this._watches = this._watches || {};
if (this._watches[id]) {
return false;
Expand Down
21 changes: 12 additions & 9 deletions packages/api/test/mixins.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import * as assert from "assert";

import { Event, INotify, EVENT_ALL } from "../src/api";
import {
Event,
EVENT_ALL,
INotify,
Listener
} from "../src/api";
import { INotifyMixin } from "../src/mixins/inotify";

describe("mixins", () => {

describe("mixins", () => {
it("INotify", () => {

@INotifyMixin
class Foo implements INotify {
addListener(_: string, __: (e: Event) => void, ___?: any): boolean {
addListener(_: string, __: Listener, ___?: any): boolean {
throw new Error();
}
removeListener(_: string, __: (e: Event) => void, ___?: any): boolean {
removeListener(_: string, __: Listener, ___?: any): boolean {
throw new Error();
}
notify(_: Event) {
Expand All @@ -22,8 +25,8 @@ describe("mixins", () => {

const res: any = {};
const foo = new Foo();
const l = (e) => res[e.id] = e.value;
const lall = (e) => res[EVENT_ALL] = e.value;
const l = (e) => (res[e.id] = e.value);
const lall = (e) => (res[EVENT_ALL] = e.value);
assert.doesNotThrow(() => foo.addListener("x", l));
assert.doesNotThrow(() => foo.addListener(EVENT_ALL, lall));
foo.notify({ id: "x", value: 1 });
Expand All @@ -35,4 +38,4 @@ describe("mixins", () => {
foo.notify({ id: "x", value: 3 });
assert.deepEqual(res, { x: 1, [EVENT_ALL]: 2 });
});
});
});
9 changes: 7 additions & 2 deletions packages/associative/src/array-set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Pair, Predicate2, SEMAPHORE } from "@thi.ng/api";
import {
Fn3,
Pair,
Predicate2,
SEMAPHORE
} from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";
import { EquivSetOpts, IEquivSet } from "./api";

Expand Down Expand Up @@ -131,7 +136,7 @@ export class ArraySet<T> extends Set<T> implements IEquivSet<T> {
return true;
}

forEach(fn: (val: T, val2: T, set: Set<T>) => void, thisArg?: any) {
forEach(fn: Fn3<T, T, Set<T>, void>, thisArg?: any) {
const vals = __private.get(this).vals;
for (let i = vals.length - 1; i >= 0; i--) {
const v = vals[i];
Expand Down
3 changes: 2 additions & 1 deletion packages/associative/src/equiv-map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Fn3,
ICopy,
IEmpty,
IEquiv,
Expand Down Expand Up @@ -134,7 +135,7 @@ export class EquivMap<K, V> extends Map<K, V>
return this;
}

forEach(fn: (val: V, key: K, map: Map<K, V>) => void, thisArg?: any) {
forEach(fn: Fn3<V, K, Map<K, V>, void>, thisArg?: any) {
for (let pair of __private.get(this).map) {
fn.call(thisArg, pair[1], pair[0], this);
}
Expand Down
9 changes: 7 additions & 2 deletions packages/associative/src/ll-set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Pair, Predicate2, SEMAPHORE } from "@thi.ng/api";
import {
Fn3,
Pair,
Predicate2,
SEMAPHORE
} from "@thi.ng/api";
import { DCons } from "@thi.ng/dcons";
import { equiv } from "@thi.ng/equiv";
import { EquivSetOpts, IEquivSet } from "./api";
Expand Down Expand Up @@ -139,7 +144,7 @@ export class LLSet<T> extends Set<T> implements IEquivSet<T> {
return true;
}

forEach(fn: (val: T, val2: T, set: Set<T>) => void, thisArg?: any) {
forEach(fn: Fn3<T, T, Set<T>, void>, thisArg?: any) {
let i = __private.get(this).vals.head;
while (i) {
fn.call(thisArg, i.value, i.value, this);
Expand Down
6 changes: 3 additions & 3 deletions packages/associative/src/merge-with.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IObjectOf } from "@thi.ng/api";
import { Fn2, IObjectOf } from "@thi.ng/api";
import { copy } from "./utils";

export const mergeMapWith = <K, V>(
f: (a: V, b: V) => V,
f: Fn2<V, V, V>,
dest: Map<K, V>,
...xs: Map<K, V>[]
) => {
Expand All @@ -20,7 +20,7 @@ export const mergeMapWith = <K, V>(
};

export const mergeObjWith = <T>(
f: (a: T, b: T) => T,
f: Fn2<T, T, T>,
dest: IObjectOf<T>,
...xs: IObjectOf<T>[]
) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/associative/src/sorted-map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Comparator,
Fn3,
ICompare,
ICopy,
IEmpty,
Expand Down Expand Up @@ -179,7 +180,7 @@ export class SortedMap<K, V> extends Map<K, V>
}
}

forEach(fn: (val: V, key: K, map: Map<K, V>) => void, thisArg?: any) {
forEach(fn: Fn3<V, K, Map<K, V>, void>, thisArg?: any) {
for (let p of this) {
fn.call(thisArg, p[1], p[0], this);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/associative/src/sorted-set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICompare, Pair } from "@thi.ng/api";
import { Fn3, ICompare, Pair } from "@thi.ng/api";
import { compare } from "@thi.ng/compare";
import { IReducible, map, ReductionFn } from "@thi.ng/transducers";
import { IEquivSet, SortedSetOpts } from "./api";
Expand Down Expand Up @@ -148,7 +148,7 @@ export class SortedSet<T> extends Set<T>
return this;
}

forEach(fn: (val: T, val2: T, set: Set<T>) => void, thisArg?: any): void {
forEach(fn: Fn3<T, T, Set<T>, void>, thisArg?: any): void {
for (let p of this) {
fn.call(thisArg, p[0], p[0], this);
}
Expand Down
14 changes: 10 additions & 4 deletions packages/atom/src/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { IID, IRelease, Watch } from "@thi.ng/api";
import {
Fn,
Fn2,
IID,
IRelease,
Watch
} from "@thi.ng/api";
import { isArray, isFunction } from "@thi.ng/checks";
import { illegalArgs, illegalArity } from "@thi.ng/errors";
import { getter, Path, setter } from "@thi.ng/paths";
Expand Down Expand Up @@ -43,15 +49,15 @@ export class Cursor<T> implements IAtom<T>, IID<string>, IRelease {
parent: IAtom<any>;

protected local: Atom<T>;
protected lookup: (s: any) => T;
protected lookup: Fn<any, T>;
protected selfUpdate: boolean;

constructor(opts: CursorOpts<T>);
constructor(parent: IAtom<any>, path: Path);
constructor(
parent: IAtom<any>,
lookup: (s: any) => T,
update: (s: any, v: T) => any
lookup: Fn<any, T>,
update: Fn2<any, T, any>
);
constructor(...args: any[]) {
let parent, id, lookup, update, validate, opts: CursorOpts<T>;
Expand Down
5 changes: 3 additions & 2 deletions packages/atom/src/history.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Event,
INotifyMixin,
Listener,
Predicate2,
Watch
} from "@thi.ng/api";
Expand Down Expand Up @@ -256,11 +257,11 @@ export class History<T> implements IHistory<T> {
return true;
}

addListener(_: string, __: (e: Event) => void, ___?: any): boolean {
addListener(_: string, __: Listener, ___?: any): boolean {
return false;
}

removeListener(_: string, __: (e: Event) => void, ___?: any): boolean {
removeListener(_: string, __: Listener, ___?: any): boolean {
return false;
}

Expand Down
13 changes: 8 additions & 5 deletions packages/cache/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {
Fn,
Fn0,
Fn2,
ICopy,
IEmpty,
ILength,
Expand All @@ -16,7 +19,7 @@ export interface ICache<K, V>
has(key: K): boolean;
get(key: K, notFound?: V): V;
set(key: K, val: V): V;
getSet(key: K, fn: () => Promise<V>): Promise<V>;
getSet(key: K, fn: Fn0<Promise<V>>): Promise<V>;
delete(key: K): boolean;

entries(): IterableIterator<Readonly<[K, CacheEntry<K, V>]>>;
Expand All @@ -25,10 +28,10 @@ export interface ICache<K, V>
}

export interface CacheOpts<K, V> {
ksize: (k: K) => number;
vsize: (v: V) => number;
release: (k: K, v: V) => void;
map: () => Map<K, any>;
ksize: Fn<K, number>;
vsize: Fn<V, number>;
release: Fn2<K, V, void>;
map: Fn0<Map<K, any>>;
maxlen: number;
maxsize: number;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/cache/src/lru.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Fn0 } from "@thi.ng/api";
import { ConsCell, DCons } from "@thi.ng/dcons";
import { map } from "@thi.ng/transducers";
import { CacheEntry, CacheOpts, ICache } from "./api";
Expand Down Expand Up @@ -124,7 +125,7 @@ export class LRUCache<K, V> implements ICache<K, V> {
return this;
}

getSet(key: K, retrieve: () => Promise<V>): Promise<V> {
getSet(key: K, retrieve: Fn0<Promise<V>>): Promise<V> {
const e = this.map.get(key);
if (e) {
return Promise.resolve(this.resetEntry(e));
Expand Down
7 changes: 2 additions & 5 deletions packages/cache/src/tlru.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Fn0 } from "@thi.ng/api";
import { ConsCell, DCons } from "@thi.ng/dcons";
import { CacheEntry, CacheOpts } from "./api";
import { LRUCache } from "./lru";
Expand Down Expand Up @@ -76,11 +77,7 @@ export class TLRUCache<K, V> extends LRUCache<K, V> {
return value;
}

getSet(
key: K,
retrieve: () => Promise<V>,
ttl = this.opts.ttl
): Promise<V> {
getSet(key: K, retrieve: Fn0<Promise<V>>, ttl = this.opts.ttl): Promise<V> {
const e = this.get(key);
if (e) {
return Promise.resolve(e);
Expand Down
11 changes: 6 additions & 5 deletions packages/color/src/porter-duff.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Fn2, Fn3 } from "@thi.ng/api";
import { setC4, setN4 } from "@thi.ng/vectors";
import { Color, ReadonlyColor } from "./api";
import { postmultiply, premultiply } from "./premultiply";
Expand All @@ -24,7 +25,7 @@ import { postmultiply, premultiply } from "./premultiply";
* @param z factor of "dest" region
*/
export const porterDuff = (
f: (s: number, d: number) => number,
f: Fn2<number, number, number>,
x: 0 | 1,
y: 0 | 1,
z: 0 | 1
Expand All @@ -36,9 +37,9 @@ export const porterDuff = (
: (s: number, d: number, sda: number, sy: number) =>
f(s, d) * sda + s * sy
: z
? (s: number, d: number, sda: number, _, sz: number) =>
f(s, d) * sda + d * sz
: (s: number, d: number, sda: number) => f(s, d) * sda;
? (s: number, d: number, sda: number, _, sz: number) =>
f(s, d) * sda + d * sz
: (s: number, d: number, sda: number) => f(s, d) * sda;
return (out: Color, src: ReadonlyColor, dest: ReadonlyColor) => {
const sa = src[3];
const da = dest[3];
Expand Down Expand Up @@ -68,7 +69,7 @@ export const porterDuffP = (
out: Color,
src: ReadonlyColor,
dest: ReadonlyColor,
mode: (out: Color, src: ReadonlyColor, dest: ReadonlyColor) => Color
mode: Fn3<Color, ReadonlyColor, ReadonlyColor, Color>
) =>
postmultiply(
null,
Expand Down
8 changes: 4 additions & 4 deletions packages/compose/src/delay.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { IDeref } from "@thi.ng/api";
import { Fn0, IDeref } from "@thi.ng/api";

export const delay = <T>(body: () => T) => new Delay<T>(body);
export const delay = <T>(body: Fn0<T>) => new Delay<T>(body);

export class Delay<T> implements IDeref<T> {
value: T;
protected body: () => T;
protected body: Fn0<T>;
protected realized: boolean;

constructor(body: () => T) {
constructor(body: Fn0<T>) {
this.body = body;
this.realized = false;
}
Expand Down
13 changes: 9 additions & 4 deletions packages/csp/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { IID, ILength, IRelease } from "@thi.ng/api";
import {
Fn,
IID,
ILength,
IRelease
} from "@thi.ng/api";
import { Channel } from "./channel";

export const enum State {
Expand All @@ -13,8 +18,8 @@ export const enum State {
// export const __State = (<any>exports).State;

export interface ChannelItem<T> {
value: () => Promise<T>;
resolve: (success: boolean) => void;
value(): Promise<T>;
resolve(success: boolean): void;
}

export interface IBuffer<T> extends ILength, IRelease {
Expand All @@ -41,6 +46,6 @@ export interface IReadWriteableChannel<T>
extends IReadableChannel<T>,
IWriteableChannel<T> {}

export type TopicFn<T> = (x: T) => string;
export type TopicFn<T> = Fn<T, string>;

export type ErrorHandler = (e: Error, chan: Channel<any>, val?: any) => void;
Loading

0 comments on commit e453ac3

Please sign in to comment.