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

Accurate argument and return types on all functions (issue #512) #513

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6a6d4d3
(optional) configure vscode formatter to match existing style as clos…
qpwo Oct 31, 2021
e6afc4a
add types.ts stub
qpwo Oct 31, 2021
466b053
create sbaobab.d.ts and sbaobab.js for strict versions, instead of ch…
qpwo Oct 31, 2021
c993cef
add util.ts (Immutable, DeepPath, DeepIndex)
qpwo Oct 31, 2021
a72a262
delete unchanged exports in sbaobab.d.ts;
qpwo Oct 31, 2021
ceb08f9
throw a ton of examples in types.ts
qpwo Oct 31, 2021
edc2788
add a few more missing examples to types.ts
qpwo Oct 31, 2021
d3c2a6b
replace var with const
qpwo Oct 31, 2021
e07ae8e
make linter and tester pass
qpwo Oct 31, 2021
53ff0b4
bunch of shit almost there with get()
qpwo Nov 1, 2021
e9c675b
write better PathsOf
qpwo Nov 1, 2021
d88f8a4
I think FullPathsOf and FullDeepIndex might finally be correct
qpwo Nov 1, 2021
5f26657
typed out most of SCommonBaobabMethods
qpwo Nov 1, 2021
ec0f030
couple touch-ups
qpwo Nov 1, 2021
3b82721
improve test cases in types.ts
qpwo Nov 1, 2021
8b45c1e
map() type for cursor
qpwo Nov 1, 2021
084590f
track path instead of subtype for root() to work;
qpwo Nov 1, 2021
7251b3e
improve watcher types; add watcher-types.patch
qpwo Nov 1, 2021
58dfb78
got the imports clean on user-package side. might be an easier way on…
qpwo Nov 2, 2021
0a93d55
empty select from root; .on('...',...) methods semi-typed
qpwo Nov 2, 2021
8b7c2b1
REMOVEME publish dist files
qpwo Nov 2, 2021
ed29691
change build process slightly (imports/exports)
qpwo Nov 2, 2021
51fb725
update dist files again
qpwo Nov 2, 2021
387b351
add readonly interfaces
qpwo May 10, 2022
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
21 changes: 21 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#211B7F",
"titleBar.activeBackground": "#2F26B2",
"titleBar.activeForeground": "#FAF9FE",
"statusBar.background": "#2F26B2",
"statusBar.foreground": "#FAF9FE"
},
"javascript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"javascript.format.insertSpaceAfterOpeningAndBeforeClosingEmptyBraces": false,
"javascript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"javascript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": false,
"javascript.format.insertSpaceBeforeFunctionParenthesis": false,
"javascript.format.semicolons": "insert",
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingEmptyBraces": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": false,
"typescript.format.insertSpaceBeforeFunctionParenthesis": false,
"typescript.format.semicolons": "insert",
}
236 changes: 118 additions & 118 deletions build/baobab.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/baobab.min.js

Large diffs are not rendered by default.

206 changes: 206 additions & 0 deletions dist/baobab.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import Emitter from 'emmett';
import {SBaobab} from '../dist/sbaobab';

interface PlainObject<T = any> {
[key: string]: T;
}

type Predicate = (data: any) => boolean;
type Constraints = PlainObject;
type PathKey = string | number;
type PathElement = PathKey | Predicate | Constraints;
export type Path = PathElement[] | PathKey;

type Splicer = [number | PlainObject | ((...args: any[]) => any), ...any[]];

/**
* This class is empty purposely. Baobab must be able to identify in an initial
* state when it has to deal with Monkeys instanciation, and uses this dummy
* class in that purpose.
*/
export class MonkeyDefinition {
// Empty class intended
}

export class Monkey {
// TODO
}

export interface BaobabOptions {
autoCommit: boolean;
asynchronous: boolean;
immutable: boolean;
lazyMonkeys: boolean;
monkeyBusiness: boolean;
persistent: boolean;
pure: boolean;
validate: null | ((previousData: any, data: any, affectedPaths?: Path[]) => (Error | undefined));
validationBehavior: string;
}

export interface MonkeyOptions {
immutable: boolean;
}

/**
* This class only exists to group methods that are common to the Baobab and
* Cursor classes. Since `Baobab.root` is a property while `Cursor#root` is a
* method, Baobab cannot extend Cursor.
*/
export abstract class CommonBaobabMethods extends Emitter {
apply(path: Path, value: (state: any) => any): any;
apply(value: (state: any) => any): any;

clone(...args: PathElement[]): any;
clone(path?: Path): any;

concat(path: Path, value: any[]): any;
concat(value: any[]): any;

deepClone(...args: PathElement[]): any;
deepClone(path?: Path): any;

deepMerge(path: Path, value: PlainObject): any;
deepMerge(value: PlainObject): any;

exists(...args: PathElement[]): boolean;
exists(path?: Path): boolean;

get(...args: PathElement[]): any;
get(path: Path): any;

merge(path: Path, value: PlainObject): any;
merge(value: PlainObject): any;

pop(path?: Path): any;

project(projection: (Path)[]): any[];
project(projection: PlainObject<Path>): PlainObject;

push(path: Path, value: any): any;
push(value: any): any;

release(): void;

select(...args: PathElement[]): Cursor;
select(path: Path): Cursor;

serialize(...args: PathElement[]): any;
serialize(path: Path): any;

set(path: Path, value: any): any;
set(value: any): any;

shift(path?: Path): any;

splice(path: Path, value: Splicer): any;
splice(value: Splicer): any;

unset(path?: Path): any;

unshift(path: Path, value: any): any;
unshift(value: any): any;
}

export class Watcher extends Emitter {
constructor(tree: Baobab, mapping: PlainObject<Path | Cursor>);

get(): PlainObject;
getWatchedPaths(): Path[];
getCursors(): PlainObject<Cursor>;
refresh(mappings: PlainObject<Path | Cursor>): void;
release(): void;
}

export class Cursor extends CommonBaobabMethods implements Iterable<any> {
path?: Path;
solvedPath?: PathKey[];
state: {
killed: boolean;
recording: boolean;
undoing: boolean;
};

[Symbol.iterator](): IterableIterator<any>;

// Navigation:
up(): Cursor | null;
down(): Cursor;
left(): Cursor | null;
right(): Cursor | null;
leftmost(): Cursor | null;
rightmost(): Cursor | null;
root(): Cursor;

// Predicates:
isLeaf(): boolean;
isRoot(): boolean;
isBranch(): boolean;

// History:
hasHistory(): boolean;
getHistory(): any[];
clearHistory(): this;
startRecording(maxRecords?: number): this;
stopRecording(): this;
undo(steps?: number): this;

// Others:
toJSON(): string;
toString(): string;

map(fn: (v: any, index?: number) => any, scope?: any): any[];
}

export class Baobab extends CommonBaobabMethods {
constructor(initialState?: PlainObject, options?: Partial<BaobabOptions>);

root: Cursor;
options: BaobabOptions;

update(
path: Path,
operation: {
type: string,
value: any,
options?: {
mutableLeaf?: boolean;
};
}
): this;

commit(): this;

getMonkey(path: Path): Monkey;

watch(mappings: PlainObject<Path | Cursor>): Watcher;

static monkey(definition: {cursors?: PlainObject<Path>; get(data: PlainObject): any; options?: MonkeyOptions;}): MonkeyDefinition;

/* tslint:disable:unified-signatures */
// Polymorphisms for:
// `.monkey(...paths: Path[], get: (v1: any) => any)`
static monkey(path1: Path, get: (value: any) => any, options?: MonkeyOptions): MonkeyDefinition;
static monkey(path1: Path, path2: Path, get: (...values: [any, any]) => any, options?: MonkeyOptions): MonkeyDefinition;
static monkey(path1: Path, path2: Path, path3: Path, get: (...values: [any, any, any]) => any, options?: MonkeyOptions): MonkeyDefinition;
static monkey(path1: Path, path2: Path, path3: Path, path4: Path, get: (...values: [any, any, any, any]) => any, options?: MonkeyOptions): MonkeyDefinition;
static monkey(path1: Path, path2: Path, path3: Path, path4: Path, path5: Path, get: (...values: [any, any, any, any, any]) => any, options?: MonkeyOptions): MonkeyDefinition;
// Fallback:
static monkey(...pathsEndingWithGetAndMaybeOptions: (Path | ((...values: any[]) => any) | MonkeyOptions)[]): MonkeyDefinition;

// Polymorphisms for:
// `.monkey(definition: [...paths: Path[], get: (v1: any) => any])`
static monkey(args: [Path, (value: any) => any], options?: MonkeyOptions): MonkeyDefinition;
static monkey(args: [Path, Path, (...values: [any, any]) => any], options?: MonkeyOptions): MonkeyDefinition;
static monkey(args: [Path, Path, Path, (...values: [any, any, any]) => any], options?: MonkeyOptions): MonkeyDefinition;
static monkey(args: [Path, Path, Path, Path, (...values: [any, any, any, any]) => any], options?: MonkeyOptions): MonkeyDefinition;
static monkey(args: [Path, Path, Path, Path, Path, (...values: [any, any, any, any, any]) => any], options?: MonkeyOptions): MonkeyDefinition;
// Fallback:
static monkey(pathsEndingWithGet: (Path | ((...values: any[]) => any) | MonkeyOptions)[]): MonkeyDefinition;
/* tslint:enable:unified-signatures */

static dynamicNode: typeof Baobab.monkey;
}

export default Baobab;
// export * from './sbaobab';
Loading