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

build: migrate to typescript #296

Merged
merged 36 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d521db8
build(prelude): migrate to typescript
raveclassic Aug 5, 2019
4e86056
build(disposable): migrate to typescript
raveclassic Aug 5, 2019
5665024
git
raveclassic Aug 6, 2019
c461f3c
build(core): migrate to typescript
raveclassic Aug 7, 2019
34c68f8
build(core): migrate to typescript // fix build
raveclassic Aug 8, 2019
8b07756
build(core): migrate to typescript // fix build
raveclassic Aug 8, 2019
017855d
build(core): migrate to typescript // fix eslint
raveclassic Aug 8, 2019
b34be98
postreview
raveclassic Aug 13, 2019
f16295b
postreview
raveclassic Aug 13, 2019
334d68c
remove vscode settings
raveclassic Aug 14, 2019
b8357ec
replace standard with eslint
raveclassic Aug 14, 2019
03440dc
update combine to ToStreamsArray
raveclassic Aug 14, 2019
1701b85
postreview
raveclassic Aug 19, 2019
6b27f04
postreview
raveclassic Aug 19, 2019
40695e3
postreview
raveclassic Aug 19, 2019
70a8b56
postreview
raveclassic Aug 19, 2019
61e5c54
fix curry3 typings
raveclassic Aug 19, 2019
1b24b9c
replace any with unknown
raveclassic Aug 21, 2019
7e516b1
replace bangs with casts and add todo comments
raveclassic Aug 21, 2019
7900b66
replace any with unknown
raveclassic Aug 21, 2019
32ad781
make Pipe abstract
raveclassic Aug 23, 2019
bbca980
fix some PropagateTask types
raveclassic Aug 23, 2019
9fb8503
simplify PropagateTask
raveclassic Aug 28, 2019
a4a65d2
remove DeferredSink
raveclassic Aug 28, 2019
a4b2b50
make IndexSink a named export
raveclassic Aug 28, 2019
cddc32c
remove Readonly
raveclassic Aug 28, 2019
23a9a0b
make active private
raveclassic Aug 30, 2019
e903032
style: space-before-function-paren
raveclassic Sep 2, 2019
1f2fc25
refactor: extract Sentinel interface
raveclassic Sep 2, 2019
4e29bea
refactor: hoist TestMulticastSource class
raveclassic Sep 2, 2019
808c7aa
style: add comment about cast
raveclassic Sep 2, 2019
8ef111a
style: postreview
raveclassic Sep 17, 2019
253909b
style: remove extra space
raveclassic Sep 17, 2019
6429390
feat: arrayLike
raveclassic Aug 19, 2019
5d638a3
test: associativity for concat
raveclassic Sep 20, 2019
5a3b2e3
Merge branch 'master' into typescript
raveclassic Nov 5, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ docs/_static
docs/_templates
lerna-debug.log
npm-debug.log
.rpt2_cache/
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
raveclassic marked this conversation as resolved.
Show resolved Hide resolved
"typescript.tsdk": "node_modules/typescript/lib"
}
1,642 changes: 1,251 additions & 391 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions packages/core/.babelrc

This file was deleted.

14 changes: 0 additions & 14 deletions packages/core/.babelrc.js

This file was deleted.

42 changes: 29 additions & 13 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@most/core",
"version": "1.5.0",
"description": "Reactive programming with lean, functions-only, curried, tree-shakeable API",
"typings": "type-definitions/most.d.ts",
"typings": "dist/most.d.ts",
"main": "dist/index.js",
"module": "dist/index.es.js",
"files": [
Expand All @@ -11,8 +11,8 @@
],
"scripts": {
"test": "npm run test:lint && npm run test:unit && npm run test:flow",
"test:unit": "cross-env NODE_ENV=test nyc --reporter=text-summary mocha -r babel-register --reporter dot --recursive 'test/**/*-test.js'",
"test:lint": "standard --fix 'src/**/*.js' 'test/**/*.js' --verbose | snazzy",
"test:unit": "cross-env NODE_ENV=test nyc mocha",
"test:lint": "standard --fix 'src/**/*.ts' 'test/**/*.ts' --verbose | snazzy",
"test:flow": "flow check",
"build": "npm run build:dist && npm run build:flow",
"build:dist": "rollup -c",
Expand All @@ -39,22 +39,23 @@
"license": "MIT",
"devDependencies": {
"@briancavalier/assert": "^3.2.0",
"babel-core": "^6.26.3",
"babel-plugin-annotate-pure-calls": "^0.2.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0",
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"@types/mocha": "^5.2.7",
"@typescript-eslint/eslint-plugin": "^1.11.0",
"@typescript-eslint/parser": "^1.11.0",
"cpy-cli": "^2.0.0",
"cross-env": "^5.2.0",
"flow-bin": "^0.101.0",
"mocha": "^5.2.0",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"rollup": "^0.50.0",
"rollup-plugin-babel": "^3.0.2",
"rollup-plugin-local-resolve": "^1.0.7",
"rollup": "^1.6.0",
"rollup-plugin-local-resolve": "~1.0.7",
"rollup-plugin-typescript2": "^0.21.2",
"sinon": "^1.17.7",
"snazzy": "^8.0.0",
"standard": "^12.0.1"
"standard": "^12.0.1",
"ts-node": "^8.3.0",
"typescript": "^3.5.2"
},
"dependencies": {
"@most/disposable": "^1.2.2",
Expand All @@ -66,6 +67,21 @@
"ignore": [
"test/perf",
"test/flow"
],
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint/eslint-plugin"
]
},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
"all": true,
"reporter": [
"text-summary"
],
"exclude": [
"dist/**",
"coverage/**"
]
}
}
10 changes: 5 additions & 5 deletions packages/core/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import babel from 'rollup-plugin-babel'
import typescript from 'rollup-plugin-typescript2'
import localResolve from 'rollup-plugin-local-resolve'
import pkg from './package.json'

export default {
input: 'src/index.js',
input: 'src/index.ts',
plugins: [
localResolve(),
babel({
exclude: 'node_modules/**',
plugins: ['external-helpers']
typescript({
tsconfig: './src/tsconfig.json',
typescript: require('typescript'),
})
],
external: [
Expand Down
26 changes: 18 additions & 8 deletions packages/core/src/Queue.js → packages/core/src/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ const HEAD_MAX_SHRINK = 2
// its internal array
const TAIL_MIN_SHRINK = 10000

export default class Queue {
export default class Queue<A> {
private _head: number;
private _tail: number;
private _capacityMask: number;
private _list: Array<A | undefined>;

constructor () {
this._head = 0
this._tail = 0
this._capacityMask = 0x3
this._list = new Array(4)
}

push (x) {
push (x: A): number {
const tail = this._tail
this._list[tail] = x
this._tail = (tail + 1) & this._capacityMask
Expand All @@ -37,7 +42,7 @@ export default class Queue {
}
}

shift () {
shift (): A | undefined {
const head = this._head
if (head === this._tail) {
return undefined
Expand All @@ -55,11 +60,11 @@ export default class Queue {
return x
}

isEmpty () {
isEmpty (): boolean {
return this._head === this._tail
}

length () {
length (): number {
if (this._head === this._tail) {
return 0
} else if (this._head < this._tail) {
Expand All @@ -69,7 +74,7 @@ export default class Queue {
}
}

_growArray () {
_growArray (): void {
if (this._head) {
// copy existing data, head to end, then beginning to tail.
this._list = this._copyArray()
Expand All @@ -83,12 +88,12 @@ export default class Queue {
this._capacityMask = (this._capacityMask << 1) | 1
}

_shrinkArray () {
_shrinkArray (): void {
this._list.length >>>= 1
this._capacityMask >>>= 1
}

_copyArray () {
_copyArray (): Array<A | undefined> {
Frikki marked this conversation as resolved.
Show resolved Hide resolved
const newArray = []
const list = this._list
const len = list.length
Expand All @@ -104,3 +109,8 @@ export default class Queue {
return newArray
}
}

export interface NonEmptyQueue<A> extends Queue<A> {
isEmpty(): false
Frikki marked this conversation as resolved.
Show resolved Hide resolved
shift(): A
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { combine } from './combine'
import { apply } from '@most/prelude'
import { Stream } from '@most/types' // eslint-disable-line no-unused-vars

/**
* Assume fs is a stream containing functions, and apply the latest function
Expand All @@ -15,6 +16,6 @@ import { apply } from '@most/prelude'
* @param {Stream} xs stream of values to which to apply all the latest f
* @returns {Stream} stream containing all the applications of fs to xs
*/
export function ap (fs, xs) {
export function ap <A, B> (fs: Stream<(a: A) => B>, xs: Stream<A>): Stream<B> {
return combine(apply, fs, xs)
}
22 changes: 0 additions & 22 deletions packages/core/src/combinator/chain.js

This file was deleted.

23 changes: 23 additions & 0 deletions packages/core/src/combinator/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/** @author Brian Cavalier */
/** @author John Hann */

import { mergeConcurrently, mergeMapConcurrently } from './mergeConcurrently'
import { Stream } from '@most/types' // eslint-disable-line no-unused-vars

/**
* Map each value in the stream to a new stream, and merge it into the
* returned outer stream. Event arrival times are preserved.
* @param f chaining function, must return a Stream
* @param stream
* @returns new stream containing all events from each stream returned by f
*/
export const chain = <A, B>(f: (value: A) => Stream<B>, stream: Stream<A>): Stream<B> => mergeMapConcurrently(f, Infinity, stream)

/**
* Monadic join. Flatten a Stream<Stream<X>> to Stream<X> by merging inner
* streams to the outer. Event arrival times are preserved.
* @param stream stream of streams
* @returns new stream containing all events of all inner streams
*/
export const join = <A>(stream: Stream<Stream<A>>): Stream<A> => mergeConcurrently(Infinity, stream)
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,48 @@
import { map } from './transform'
import { empty, containsCanonicalEmpty } from '../source/empty'
import Pipe from '../sink/Pipe'
import IndexSink from '../sink/IndexSink'
import IndexSink, { IndexedValue } from '../sink/IndexSink' // eslint-disable-line no-unused-vars
raveclassic marked this conversation as resolved.
Show resolved Hide resolved
import { disposeAll, tryDispose } from '@most/disposable'
import invoke from '../invoke'
import { Stream, Sink, Scheduler, Disposable, Time } from '@most/types' // eslint-disable-line no-unused-vars
import { ToStreamsArray } from './variadic' // eslint-disable-line no-unused-vars

/**
* Combine latest events from two streams
* @param {function(...events):*} f function to combine most recent events
* @returns {Stream} stream containing the result of applying f to the most recent
* @param f function to combine most recent events
* @returns stream containing the result of applying f to the most recent
* event of each input stream, whenever a new event arrives on any stream.
*/
export const combine = (f, stream1, stream2) =>
export const combine = <A, B, C>(f: (a: A, b: B) => C, stream1: Stream<A>, stream2: Stream<B>): Stream<C> =>
combineArray(f, [stream1, stream2])

/**
* Combine latest events from all input streams
* @param {function(...events):*} f function to combine most recent events
* @param {[Stream]} streams most recent events
* @returns {Stream} stream containing the result of applying f to the most recent
* @param f function to combine most recent events
* @param streams most recent events
* @returns stream containing the result of applying f to the most recent
* event of each input stream, whenever a new event arrives on any stream.
*/
export const combineArray = (f, streams) =>
export const combineArray = <Args extends any[], R>(f: (...args: Args) => R, streams: ToStreamsArray<Args>): Stream<R> =>
streams.length === 0 || containsCanonicalEmpty(streams) ? empty()
: streams.length === 1 ? map(f, streams[0])
: new Combine(f, streams)
: streams.length === 1 ? map(f as any, streams[0])
raveclassic marked this conversation as resolved.
Show resolved Hide resolved
: new Combine(f as any, streams)

class Combine {
constructor (f, sources) {
class Combine<A, B> {
raveclassic marked this conversation as resolved.
Show resolved Hide resolved
private readonly f: (...args: A[]) => B
private readonly sources: Stream<A>[];
raveclassic marked this conversation as resolved.
Show resolved Hide resolved

constructor (f: (...args: A[]) => B, sources: Stream<A>[]) {
this.f = f
this.sources = sources
}

run (sink, scheduler) {
run (sink: Sink<B>, scheduler: Scheduler) {
const l = this.sources.length
const disposables = new Array(l)
const sinks = new Array(l)

const mergeSink = new CombineSink(disposables, sinks, sink, this.f)
const mergeSink = new CombineSink(disposables, sinks.length, sink, this.f)

for (let indexSink, i = 0; i < l; ++i) {
indexSink = sinks[i] = new IndexSink(i, mergeSink)
Expand All @@ -50,21 +55,27 @@ class Combine {
}
}

class CombineSink extends Pipe {
constructor (disposables, sinks, sink, f) {
class CombineSink<A, B> extends Pipe<B | IndexedValue<A>> {
private readonly disposables: Disposable[]
private readonly f: (...args: A[]) => B
private awaiting: number
private hasValue: boolean[]
private activeCount: number
private readonly values: A[]

constructor (disposables: Disposable[], length: number, sink: Sink<B>, f: (...args: A[]) => B) {
super(sink)
this.disposables = disposables
this.sinks = sinks
this.f = f

const l = sinks.length
const l = length
raveclassic marked this conversation as resolved.
Show resolved Hide resolved
this.awaiting = l
this.values = new Array(l)
this.hasValue = new Array(l).fill(false)
this.activeCount = sinks.length
this.activeCount = length
}

event (t, indexedValue) {
event (t: Time, indexedValue: IndexedValue<A>) {
raveclassic marked this conversation as resolved.
Show resolved Hide resolved
if (!indexedValue.active) {
this._dispose(t, indexedValue.index)
return
Expand All @@ -79,7 +90,7 @@ class CombineSink extends Pipe {
}
}

_updateReady (index) {
_updateReady (index: number) {
if (this.awaiting > 0) {
if (!this.hasValue[index]) {
this.hasValue[index] = true
Expand All @@ -89,7 +100,7 @@ class CombineSink extends Pipe {
return this.awaiting
}

_dispose (t, index) {
_dispose (t: Time, index: number) {
tryDispose(t, this.disposables[index], this.sink)
if (--this.activeCount === 0) {
this.sink.end(t)
Expand Down
Loading