Skip to content

Commit bc1835b

Browse files
Merge pull request #14 from umbrellio/feature/jsdoc-type-coverage
Types for better intellisense
2 parents ceedf4b + 9f73b6f commit bc1835b

10 files changed

+154
-4
lines changed

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@umbrellio/observable",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"description": "Observable library",
55
"repository": "git@github.com:umbrellio/observable.git",
66
"author": "Aleksei Bespalov <nulldefiner@gmail.com>",
@@ -9,6 +9,7 @@
99
"main": "dist/observable.cjs.js",
1010
"module": "dist/observable.es.js",
1111
"unpkg": "dist/observable.iife.js",
12+
"types": "dist/observable.d.ts",
1213
"scripts": {
1314
"build": "rollup -c",
1415
"lint": "eslint .",
@@ -41,7 +42,9 @@
4142
"jest-environment-jsdom": "^28.1.1",
4243
"react": "^17.0.2",
4344
"react-dom": "^17.0.2",
44-
"rollup": "^2.75.6"
45+
"rollup": "^2.75.6",
46+
"rollup-plugin-dts": "^4.2.3",
47+
"typescript": "^4.8.4"
4548
},
4649
"peerDependencies": {
4750
"react": "^16.13.1 || ^17"

rollup.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { babel } from "@rollup/plugin-babel"
22
import { nodeResolve } from "@rollup/plugin-node-resolve"
3+
import dts from "rollup-plugin-dts"
34

45
const shared = {
56
input: "src/index.js",
@@ -46,6 +47,14 @@ const config = [
4647
name: "T",
4748
},
4849
} }),
50+
{
51+
input: "src/index.js",
52+
plugins: [dts()],
53+
output: {
54+
file: "dist/observable.d.ts",
55+
format: "es",
56+
},
57+
},
4958
]
5059

5160
export default config

src/common.d.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Component, PureComponent, FC } from "react"
2+
3+
export declare type SubscribeFn<T> = (data: T) => void
4+
5+
export declare type UnsubscribeFn = () => void
6+
7+
export declare type MapFn<T> = (data: T) => T | any
8+
9+
export declare type ReactComponent<Props, State, SnapShot> = (
10+
Component<Props, State, SnapShot> | PureComponent<Props, State, SnapShot> | FC<Props>
11+
)
12+
13+
export declare type ComponentWrapper<Props, State, SnapShot> = (
14+
(component: ReactComponent<Props, State, SnapShot>) => Component<Props, State, SnapShot>
15+
)
16+
17+
export declare interface Store<State> {
18+
getState(): State
19+
set(data: Partial<State>): void
20+
observer<ComponentState, ComponentSnapShot>
21+
(options: ObserverOptions<State>): Observer<State, ComponentState, ComponentSnapShot>
22+
subscribe(sub: SubscribeFn<State>): UnsubscribeFn
23+
reset(): void
24+
}
25+
26+
export declare interface ObserverOptions<T> {
27+
key: string
28+
map: MapFn<T>
29+
}
30+
31+
export declare interface Observer<State, ComponentState, ComponentSnapShot> {
32+
(store: Store<State>, options?: ObserverOptions<State>):
33+
ComponentWrapper<State, ComponentState, ComponentSnapShot>
34+
}

src/multipleObserver.d.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Component } from "react"
2+
3+
import { Store, ReactComponent, MapFn } from "./common"
4+
5+
declare interface StoreConfig<T> {
6+
store: Store<T>
7+
key: string
8+
map?: MapFn<T>
9+
}
10+
11+
declare type Wrapper<Props, State, SnapShot> = (
12+
(component: ReactComponent<Props, State, SnapShot>) => Component<Props, State, SnapShot>
13+
)
14+
15+
declare function multipleObserver<State, ComponentState, ComponentSnapShot>
16+
(stores: [StoreConfig<State>]): Wrapper<State, ComponentState, ComponentSnapShot>
17+
18+
declare function multipleObserver<T1, T2, S, SS>
19+
(stores: [StoreConfig<T1>, StoreConfig<T2>]): Wrapper<T1 & T2, S, SS>
20+
21+
declare function multipleObserver<T1, T2, T3, S, SS>
22+
(stores: [StoreConfig<T1>, StoreConfig<T2>, StoreConfig<T3>]): Wrapper<T1 & T2 & T3, S, SS>
23+
24+
declare function multipleObserver<T1, T2, T3, T4, S, SS>
25+
(stores: [StoreConfig<T1>, StoreConfig<T2>, StoreConfig<T3>, StoreConfig<T4>]): Wrapper<T1 & T2 & T3 & T4, S, SS>
26+
27+
declare function multipleObserver<T1, T2, T3, T4, T5, S, SS>
28+
(stores: [StoreConfig<T1>, StoreConfig<T2>, StoreConfig<T3>, StoreConfig<T4>, StoreConfig<T5>]): Wrapper<T1 & T2 & T3 & T4 & T5, S, SS>
29+
30+
declare function multipleObserver<T1, T2, T3, T4, T5, T6, S, SS>
31+
(stores: [StoreConfig<T1>, StoreConfig<T2>, StoreConfig<T3>, StoreConfig<T4>, StoreConfig<T5>, StoreConfig<T6>]): Wrapper<T1 & T2 & T3 & T4 & T5 & T6, S, SS>
32+
33+
declare function multipleObserver<T1, T2, T3, T4, T5, T6, T7, S, SS>
34+
(stores: [StoreConfig<T1>, StoreConfig<T2>, StoreConfig<T3>, StoreConfig<T4>, StoreConfig<T5>, StoreConfig<T6>, StoreConfig<T7>]): Wrapper<T1 & T2 & T3 & T4 & T5 & T6 & T7, S, SS>
35+
36+
export default multipleObserver

src/multipleObserver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const multipleObserver = stores => WrappedComponent => {
3333
}
3434
}, {})
3535

36-
return <WrappedComponent {...this.props} {...state} />
36+
return WrappedComponent({ ...this.props, ...state })
3737
}
3838
}
3939
}

src/observable.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Store } from "./common"
2+
3+
declare function observable<T> (initialData: T): Store<T>
4+
5+
export default observable

src/observer.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ComponentWrapper, ObserverOptions, Store } from "./common"
2+
3+
declare function observer<State, ComponentState, ComponentSnapShot>
4+
(store: Store<State>, options?: ObserverOptions<State>):
5+
ComponentWrapper<State, ComponentState, ComponentSnapShot>
6+
7+
export default observer

src/observer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const observer = (store, { key, map }) => WrappedComponent => {
2323

2424
render () {
2525
const state = { [key]: map ? map(this.state) : this.state }
26-
return <WrappedComponent {...this.props} {...state} />
26+
return WrappedComponent({ ...this.props, ...state })
2727
}
2828
}
2929
}

src/useStore.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Store, MapFn } from "./common"
2+
3+
declare interface Options<T> {
4+
map: MapFn<T>
5+
}
6+
7+
declare function useStore<T> (store: Store<T>, options?: Options<T>): T
8+
9+
export default useStore

yarn.lock

+47
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747
dependencies:
4848
"@babel/highlight" "^7.16.7"
4949

50+
"@babel/code-frame@^7.18.6":
51+
version "7.18.6"
52+
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.18.6.tgz#3b25d38c89600baa2dcc219edfa88a74eb2c427a"
53+
integrity sha512-TDCmlK5eOvH+eH7cdAFlNXeVJqWIQ7gW9tY1GJIpUtFb6CmjVyq2VM3u71bOyR8CRihcCgMUYoDNyLXao3+70Q==
54+
dependencies:
55+
"@babel/highlight" "^7.18.6"
56+
5057
"@babel/compat-data@^7.13.11":
5158
version "7.14.0"
5259
resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.14.0.tgz#a901128bce2ad02565df95e6ecbf195cf9465919"
@@ -397,6 +404,11 @@
397404
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.16.7.tgz#e8c602438c4a8195751243da9031d1607d247cad"
398405
integrity sha512-hsEnFemeiW4D08A5gUAZxLBTXpZ39P+a+DGDsHw1yxqyQ/jzFEnxf5uTEGp+3bzAbNOxU1paTgYS4ECU/IgfDw==
399406

407+
"@babel/helper-validator-identifier@^7.18.6":
408+
version "7.19.1"
409+
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.19.1.tgz#7eea834cf32901ffdc1a7ee555e2f9c27e249ca2"
410+
integrity sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==
411+
400412
"@babel/helper-validator-option@^7.12.17":
401413
version "7.12.17"
402414
resolved "https://registry.yarnpkg.com/@babel/helper-validator-option/-/helper-validator-option-7.12.17.tgz#d1fbf012e1a79b7eebbfdc6d270baaf8d9eb9831"
@@ -453,6 +465,15 @@
453465
chalk "^2.0.0"
454466
js-tokens "^4.0.0"
455467

468+
"@babel/highlight@^7.18.6":
469+
version "7.18.6"
470+
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.18.6.tgz#81158601e93e2563795adcbfbdf5d64be3f2ecdf"
471+
integrity sha512-u7stbOuYjaPezCuLj29hNW1v64M2Md2qupEKP1fHc7WdOA3DgLh37suiSrZYY7haUB7iBeQZ9P1uiRF359do3g==
472+
dependencies:
473+
"@babel/helper-validator-identifier" "^7.18.6"
474+
chalk "^2.0.0"
475+
js-tokens "^4.0.0"
476+
456477
"@babel/parser@^7.1.0", "@babel/parser@^7.12.13", "@babel/parser@^7.13.13":
457478
version "7.13.13"
458479
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.13.13.tgz#42f03862f4aed50461e543270916b47dd501f0df"
@@ -4396,6 +4417,13 @@ lru-cache@^6.0.0:
43964417
dependencies:
43974418
yallist "^4.0.0"
43984419

4420+
magic-string@^0.26.6:
4421+
version "0.26.7"
4422+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.26.7.tgz#caf7daf61b34e9982f8228c4527474dac8981d6f"
4423+
integrity sha512-hX9XH3ziStPoPhJxLq1syWuZMxbDvGNbVchfrdCtanC7D13888bMFow61x8axrx+GfHLtVeAx2kxL7tTGRl+Ow==
4424+
dependencies:
4425+
sourcemap-codec "^1.4.8"
4426+
43994427
make-dir@^2.1.0:
44004428
version "2.1.0"
44014429
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"
@@ -5172,6 +5200,15 @@ rimraf@^3.0.0, rimraf@^3.0.2:
51725200
dependencies:
51735201
glob "^7.1.3"
51745202

5203+
rollup-plugin-dts@^4.2.3:
5204+
version "4.2.3"
5205+
resolved "https://registry.yarnpkg.com/rollup-plugin-dts/-/rollup-plugin-dts-4.2.3.tgz#04c3615df1ffab4228aa9d540697eaca61e01f47"
5206+
integrity sha512-jlcpItqM2efqfIiKzDB/IKOS9E9fDvbkJSGw5GtK/PqPGS9eC3R3JKyw2VvpTktZA+TNgJRMu1NTv244aTUzzQ==
5207+
dependencies:
5208+
magic-string "^0.26.6"
5209+
optionalDependencies:
5210+
"@babel/code-frame" "^7.18.6"
5211+
51755212
rollup@^2.75.6:
51765213
version "2.75.6"
51775214
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.75.6.tgz#ac4dc8600f95942a0180f61c7c9d6200e374b439"
@@ -5310,6 +5347,11 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
53105347
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
53115348
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
53125349

5350+
sourcemap-codec@^1.4.8:
5351+
version "1.4.8"
5352+
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
5353+
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==
5354+
53135355
sprintf-js@~1.0.2:
53145356
version "1.0.3"
53155357
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
@@ -5624,6 +5666,11 @@ type-fest@^0.21.3:
56245666
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
56255667
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==
56265668

5669+
typescript@^4.8.4:
5670+
version "4.8.4"
5671+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6"
5672+
integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==
5673+
56275674
unbox-primitive@^1.0.0:
56285675
version "1.0.1"
56295676
resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.1.tgz#085e215625ec3162574dc8859abee78a59b14471"

0 commit comments

Comments
 (0)