Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 0115dbe

Browse files
committed
Updated how types are handled.
1. Updated version of TypeScript. 2. Autogenerated d.ts file from JSDoc comments. 3. Added src/types.js where all project-wide types are amalgamented.
1 parent 90638e6 commit 0115dbe

16 files changed

+169
-71
lines changed

dist/runtime.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime.mjs.map

+1-1
Large diffs are not rendered by default.

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@composi/runtime",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"description": "A JavaScript library providing state management for DOM renderers.",
55
"main": "src/index.js",
66
"module": "dist/runtime.mjs",
@@ -54,8 +54,9 @@
5454
"rollup-plugin-babel-minify": "^6.0.0",
5555
"rollup-plugin-commonjs": "^8.2.0",
5656
"rollup-plugin-node-resolve": "^3.0.0",
57-
"typescript": "^3.0.3"
57+
"typescript": "^3.7.3"
5858
},
59+
"typings": "types",
5960
"publishConfig": {
6061
"access": "public"
6162
}

src/effects.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* @typedef {import('./runtime').Send} Send
3-
* @typedef {import('./runtime').Message} Message
4-
* @typedef {Object} State
2+
* @typedef {import('./types').Send} Send
3+
* @typedef {import('./types').Message} Message
4+
* @typedef {import('./types').State} State
55
* @typedef {() => State} GetState
66
* @typedef {(send?: Send, getState?: GetState) => any} Effect
77
*/

src/index.d.ts

-7
This file was deleted.

src/index.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,4 @@ export { run } from './runtime'
22
export { union } from './union'
33
export { batchEffects, batch } from './effects'
44

5-
/**
6-
* Make types available to programs that use them.
7-
*/
8-
/**
9-
* A program which is executed by the `run` function.
10-
* @typedef { import('./runtime').Program } Program
11-
*/
12-
/**
13-
* Message dispatched by the `send` function to the program's `update` method.
14-
* @typedef { import('./runtime').Message } Message
15-
*/
16-
/**
17-
* Type of state, which can be of any type.
18-
* @typedef { import('./runtime').State } State
19-
*/
20-
/**
21-
* Function for sending messages to the program's `update` method.
22-
* @typedef { import('./runtime').Send } Send
23-
*/
5+

src/runtime.js

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
1-
/**
2-
* @typedef {Object<string, any>} Message
3-
* @prop {string} type
4-
* @prop {any} [data]
5-
* @typedef {(msg?: Message | Function, data?: any) => Message} Send
6-
* @typedef {() => State} GetState
7-
*/
8-
/**
9-
* @typedef {any} State Simple or complex types for application state.
10-
*/
11-
/**
12-
* @typedef {State | void} InitResult Return result of program init method.
13-
*/
14-
/**
15-
* @typedef {Object<string, any>} Program A program to run.
16-
* @prop {() => InitResult} init Method to set up initial state.
17-
* @prop {(state: State, send?: Send) => void} view Method to present the current application state.
18-
* @prop {(state: State, msg?: Message, send?: Send) => any} update Method to capture messages sent from view or subscriptions. According to the message, an action will transform application state and pass it the the program view method.
19-
* @prop {(send?: Send, getState?: GetState) => void} [subscriptions] Method to run effects when the program starts. These run independently from the rest of the program.
20-
* @prop {(send?: Send, getState?: GetState) => void} [subs] Shortcut for subscriptions.
21-
* @prop {(state: State) => void} [done] Method to do clean up when shutting down a program.
22-
* @prop {Send} [send] A static send function for dispatching message to a program. Used with routers and in composition.
23-
*/
1+
242
/**
253
* The @composi/runtime.
264
* @example
@@ -47,7 +25,7 @@
4725
* // Run the program:
4826
* run(program)
4927
* ```
50-
* @param {Program} program A program to run with five methods: `init`, `view`, `update`, `subscriptions` and `done`.
28+
* @param {import('./types').Program} program A program to run with five methods: `init`, `view`, `update`, `subscriptions` and `done`.
5129
* @return {() => void} Function to terminate runtime.
5230
*/
5331
export function run(program) {
@@ -63,7 +41,7 @@ export function run(program) {
6341

6442
/**
6543
* Send a message.
66-
* @param {Message} message
44+
* @param {import('./types').Message} message
6745
*
6846
*/
6947
function send(message, data) {

src/types.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Dummy export to make this a module with exportable types.
2+
export {}
3+
4+
/**
5+
* @typedef {Object} Tag
6+
* @prop {string} type
7+
* @prop {any} [data]
8+
*/
9+
10+
/**
11+
* @typedef {Object<string, any>} Message
12+
* @prop {string} type
13+
* @prop {any} [data]
14+
* @typedef {(msg?: Message | Function, data?: any) => Message} Send
15+
* @typedef {() => State} GetState
16+
*/
17+
/**
18+
* @typedef {any} State Simple or complex types for application state.
19+
*/
20+
/**
21+
* @typedef {State | void} InitResult Return result of program init method.
22+
*/
23+
/**
24+
* @typedef {Object<string, any>} Program A program to run.
25+
* @prop {() => InitResult} init Method to set up initial state.
26+
* @prop {(state: State, send?: Send) => void} view Method to present the current application state.
27+
* @prop {(state: State, msg?: Message, send?: Send) => any} update Method to capture messages sent from view or subscriptions. According to the message, an action will transform application state and pass it the the program view method.
28+
* @prop {(send?: Send, getState?: GetState) => void} [subscriptions] Method to run effects when the program starts. These run independently from the rest of the program.
29+
* @prop {(send?: Send, getState?: GetState) => void} [subs] Shortcut for subscriptions.
30+
* @prop {(state: State) => void} [done] Method to do clean up when shutting down a program.
31+
* @prop {Send} [send] A static send function for dispatching message to a program. Used with routers and in composition.
32+
*/

src/union.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
*/
44
const hasOwnProperty = Object.prototype.hasOwnProperty
55

6+
67
/**
7-
* @typedef {Object} Tag
8-
* @prop {string} type
9-
* @prop {any} [data]
10-
*/
11-
/**
12-
* @param {Tag} tag
8+
* @param {import('./types').Tag} tag
139
* @param {Object<string, Function>} handlers
1410
* @param {() => void} [catchAll]
1511
*/

tsconfig.json

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
"target": "es6",
44
"allowJs": true,
55
"checkJs": true,
6-
"noEmit": true,
76
"moduleResolution": "node",
7+
"alwaysStrict": true,
8+
"strictNullChecks": false,
9+
"emitDeclarationOnly": true,
10+
"declaration": true,
11+
"outDir": "types",
12+
"removeComments": false
813
},
9-
"include": [
10-
"src/*.js",
14+
"files": [
15+
"src/effects.js",
16+
"src/runtime.js",
17+
"src/union.js",
18+
"src/index.js"
19+
],
20+
"exclude": [
21+
"node_modules",
22+
"types"
1123
]
1224
}

types/effects.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function batchEffects(...effects: ((send?: (msg?: Function | import("./types").Message, data?: any) => import("./types").Message, getState?: () => any) => any)[]): (send?: (msg?: Function | import("./types").Message, data?: any) => import("./types").Message, getState?: () => any) => void;
2+
export function batch(...effects: ((send?: (msg?: Function | import("./types").Message, data?: any) => import("./types").Message, getState?: () => any) => any)[]): (send?: (msg?: Function | import("./types").Message, data?: any) => import("./types").Message, getState?: () => any) => void;
3+
export type Send = (msg?: Function | import("./types").Message, data?: any) => import("./types").Message;
4+
export type Message = {
5+
type: string;
6+
data?: any;
7+
};
8+
export type State = any;
9+
export type GetState = () => any;
10+
export type Effect = (send?: (msg?: Function | import("./types").Message, data?: any) => import("./types").Message, getState?: () => any) => any;

types/index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { run } from "./runtime";
2+
export { union } from "./union";
3+
export { batchEffects, batch } from "./effects";

types/runtime.d.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* The @composi/runtime.
3+
* @example
4+
*
5+
* ```
6+
* // Create a runtime program
7+
* const program = {
8+
* // Define state:
9+
* init() {
10+
* return [{count: 0}]
11+
* },
12+
* // Define view to render.
13+
* // Notice event to send message 'incr'.
14+
* view(state, send) {
15+
* return render(<div onclick={send('incr')}>The count is: {state.count}</div>, document.body)
16+
* },
17+
* // Define action to update state:
18+
* update(state, msg) {
19+
* if (msg === 'incr') {
20+
* return [state.count++]
21+
* }
22+
* }
23+
* }
24+
* // Run the program:
25+
* run(program)
26+
* ```
27+
* @param {import('./types').Program} program A program to run with five methods: `init`, `view`, `update`, `subscriptions` and `done`.
28+
* @return {() => void} Function to terminate runtime.
29+
*/
30+
export function run(program: import("./types").Program): () => void;

types/types.d.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export type Tag = {
2+
type: string;
3+
data?: any;
4+
};
5+
export type Message = {
6+
type: string;
7+
data?: any;
8+
};
9+
export type Send = (msg?: Function | Message, data?: any) => Message;
10+
export type GetState = () => any;
11+
/**
12+
* Simple or complex types for application state.
13+
*/
14+
export type State = any;
15+
/**
16+
* Return result of program init method.
17+
*/
18+
export type InitResult = any;
19+
/**
20+
* A program to run.
21+
*/
22+
export type Program = {
23+
/**
24+
* Method to set up initial state.
25+
*/
26+
init: () => any;
27+
/**
28+
* Method to present the current application state.
29+
*/
30+
view: (state: any, send?: (msg?: Function | Message, data?: any) => Message) => void;
31+
/**
32+
* Method to capture messages sent from view or subscriptions. According to the message, an action will transform application state and pass it the the program view method.
33+
*/
34+
update: (state: any, msg?: Message, send?: (msg?: Function | Message, data?: any) => Message) => any;
35+
/**
36+
* Method to run effects when the program starts. These run independently from the rest of the program.
37+
*/
38+
subscriptions?: (send?: (msg?: Function | Message, data?: any) => Message, getState?: () => any) => void;
39+
/**
40+
* Shortcut for subscriptions.
41+
*/
42+
subs?: (send?: (msg?: Function | Message, data?: any) => Message, getState?: () => any) => void;
43+
/**
44+
* Method to do clean up when shutting down a program.
45+
*/
46+
done?: (state: any) => void;
47+
/**
48+
* A static send function for dispatching message to a program. Used with routers and in composition.
49+
*/
50+
send?: (msg?: Function | Message, data?: any) => Message;
51+
};

types/union.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @typedef {Object} MessageUnion
3+
*/
4+
/**
5+
* Create a union of types for matching up with functions. This is used to define actions for the `update` method of a runtime program.
6+
* @param {...string} types
7+
* @returns {MessageUnion} MessageUnion
8+
*/
9+
export function union(...types: string[]): any;
10+
export type MessageUnion = any;

0 commit comments

Comments
 (0)