Skip to content

Commit 6a10572

Browse files
committed
ts specific deployment
1 parent 799bd94 commit 6a10572

File tree

14 files changed

+252
-249
lines changed

14 files changed

+252
-249
lines changed

index.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
coveragePathIgnorePatterns: [
44
'<rootDir>/src/\\$global.ts',
55
'<rootDir>/src/test-artifacts',
6-
'<rootDir>/src/types.ts',
6+
'<rootDir>/src/index.ts'
77
],
88
detectOpenHandles: true,
99
globals: {},

src/connection.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import AccessorCache from './model/accessor-cache';
2-
import { Immutable } from '.';
2+
import { Immutable } from './main';
33
import { deps, Connection } from './connection';
44

55
describe( 'Connection class', () => {
66
const setup = () => {
77
const cache = new AccessorCache({});
8-
const imDeps = require( '.' ).deps;
8+
const imDeps = require( './main' ).deps;
99
const assignCacheOrig = imDeps.assignCache;
1010
imDeps.assignCache = () => cache;
1111
const expectedId = 'TEST ID';

src/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import type {
1010
Value,
1111
ValueObject,
1212
ValueObjectCloneable
13-
} from './types';
13+
} from '.';
1414

1515
import setValue from './set';
1616

17-
import { Immutable } from '.';
17+
import { Immutable } from './main';
1818

1919
import AccessorCache from './model/accessor-cache';
2020

src/index.ts

Lines changed: 124 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,135 @@
1-
type Fn = (...args: any[]) => any;
2-
type Constants = typeof constants;
3-
type TagKeys = {[K in keyof Constants]: K extends `${ string }_TAG` ? K : never}[ keyof Constants ];
4-
type TagNameMap = {[K in TagKeys]: K extends `${ infer P }_TAG` ? P : never}
5-
type TagType = {[K in TagKeys as TagNameMap[ K ]]: Constants[ K ] };
1+
import type { Closable, TagType } from './main';
62

7-
import type { Value } from './types';
3+
import {
4+
CLEAR_TAG,
5+
DELETE_TAG,
6+
GLOBAL_SELECTOR,
7+
MOVE_TAG,
8+
NULL_SELECTOR,
9+
PUSH_TAG,
10+
REPLACE_TAG,
11+
SET_TAG,
12+
SPLICE_TAG
13+
} from './constants';
814

9-
import * as _constants from './constants';
15+
import { Immutable, Tag } from './main';
1016

11-
import { clonedeep } from './utils';
17+
/** @see {@link https://auto-immutable.js.org/api/set/method/tags/clear-usage} */
18+
export type ClearTag = typeof CLEAR_TAG;
1219

13-
import AccessorCache from './model/accessor-cache';
20+
/** @see {@link https://auto-immutable.js.org/api/set/method/tags/delete-usage} */
21+
export type DeleteTag = typeof DELETE_TAG;
1422

15-
import { Connection } from './connection';
23+
export type GlobalSelector = typeof GLOBAL_SELECTOR;
1624

17-
export const constants = _constants;
25+
/** @see {@link https://auto-immutable.js.org/api/set/method/tags/move-usage} */
26+
export type MoveTag = typeof MOVE_TAG;
1827

19-
export const Tag = {} as Readonly<TagType>;
20-
for( let k in constants ) {
21-
if( !k.endsWith( '-TAG' ) ) { continue }
22-
// istanbul ignore next
23-
Tag[ k.slice( 0, -4 ) ] = constants[ k ];
28+
export type NullSelector = typeof NULL_SELECTOR;
29+
30+
/** @see {@link https://auto-immutable.js.org/api/set/method/tags/push-usage} */
31+
export type PushTag = typeof PUSH_TAG;
32+
33+
/** @see {@link https://auto-immutable.js.org/api/set/method/tags/replace-usage} */
34+
export type ReplaceTag = typeof REPLACE_TAG;
35+
36+
/** @see {@link https://auto-immutable.js.org/api/set/method/tags/set-usage} */
37+
export type SetTag = typeof SET_TAG;
38+
39+
/** @see {@link https://auto-immutable.js.org/api/set/method/tags/splice-usage} */
40+
export type SpliceTag = typeof SPLICE_TAG;
41+
42+
export type KeyType = number | string | symbol;
43+
44+
export type ScalarType = boolean | KeyType;
45+
46+
export type Cloneable<T extends object> = T & {
47+
clone?: ( ...args : Array<any> ) => T;
48+
cloneNode?: ( deep : true, ...args : Array<any> ) => T;
2449
};
25-
Object.freeze( Tag );
2650

27-
export const deps = {
28-
assignCache: <T extends Value>( initValue : T ) => new AccessorCache( clonedeep( initValue ) ),
29-
numCreated: 0
51+
export type ValueObject = {[x: KeyType]: BaseType | Function};
52+
export type ValueObjectCloneable = Cloneable<ValueObject>;
53+
export type Value = ValueObject | ValueObjectCloneable;
54+
55+
export interface UpdateStats { hasChanges: boolean };
56+
57+
export type BaseType = Array<any> | ScalarType | Value | {} | object;
58+
59+
/** As in {"@@CLEAR":*} is a parameterless command. Parameters have not effect */
60+
export type ClearCommand = {[CLEAR_TAG]: any};
61+
62+
/** As in {"@@DELETE": [property keys to delete]} */
63+
export type DeleteCommand<T> = {[DELETE_TAG]: Array<keyof T>}
64+
65+
/** As in {"@@MOVE": [-/+fromIndex, -/+toIndex, +numItems? ]}. numItems = 1 by default. */
66+
export type MoveCommand = {[MOVE_TAG]: [number, number, number?]}
67+
68+
/** As in {"@@PUSH": [new items]} */
69+
export type PushCommand = {[PUSH_TAG]: Array<any>}
70+
71+
/** As in {"@@REPLACE": Replacement value} */
72+
export type ReplaceCommand = {[REPLACE_TAG]: BaseType}
73+
74+
/** As in {"@@SET": Replacement value} */
75+
export type SetCommand = {[SET_TAG]: BaseType | (<V>(currentValue: V) => any)}
76+
77+
/** As in {"@@SPLICE": [-/+fromIndex, +deleteCount <n >= 0>, ...newItems? ]}. numItems = undefined by default. */
78+
export type SpliceCommand = {[SPLICE_TAG]: [number, number, ...Array<any>]}
79+
80+
export type TagCommand<T extends TagType, P extends Value|Array<any> = Value> =
81+
T extends ClearTag ? ClearCommand :
82+
T extends DeleteTag ? DeleteCommand<P> :
83+
T extends MoveTag ? MoveCommand :
84+
T extends PushTag ? PushCommand :
85+
T extends ReplaceTag ? ReplaceCommand :
86+
T extends SetTag ? SetCommand :
87+
T extends SpliceTag ? SpliceCommand : never;
88+
89+
export interface AccessorPayload {[ propertyPath: string ]: Atom};
90+
91+
export interface AccessorResponse {[ propertyPath: string ]: Atom["value"]}; // [Readonly<any>};
92+
93+
export type Changes<T extends Value> = UpdatePayload<T> | UpdatePayloadArray<T>;
94+
95+
export type Listener = <T extends Value>(changes : Changes<T>) => void;
96+
97+
export type UpdatePayloadCore<T extends Array<any> | Value> =
98+
| ClearTag
99+
| TagCommand<TagType, T>
100+
| Value
101+
| T extends {}
102+
? T | Partial<{
103+
[K in keyof T]: T[K] extends Array<any>|Value
104+
? UpdatePayload<T[K]>
105+
: UpdatePayload<Value>
106+
}>
107+
: T;
108+
export type UpdatePayloadCoreCloneable<T extends Array<any> | Value> = Cloneable<UpdatePayloadCore<T>>
109+
export type UpdatePayload<T extends Array<any> | Value> = UpdatePayloadCore<T> | UpdatePayloadCoreCloneable<T>;
110+
111+
export type UpdatePayloadArrayCore<T extends Array<any>|Value> = Array<UpdatePayload<T>>;
112+
export type UpdatePayloadArrayCoreCloneable<T extends Array<any>|Value> = Cloneable<UpdatePayloadArrayCore<T>>;
113+
export type UpdatePayloadArray<T extends Array<any> | Value> = UpdatePayloadArrayCore<T>|UpdatePayloadArrayCoreCloneable<T>;
114+
115+
import type Atom from './model/atom';
116+
117+
export type { Connection } from './connection';
118+
119+
export type { Closable, TagType };
120+
121+
export { Immutable, Tag };
122+
123+
export {
124+
CLEAR_TAG,
125+
DELETE_TAG,
126+
GLOBAL_SELECTOR,
127+
MOVE_TAG,
128+
NULL_SELECTOR,
129+
PUSH_TAG,
130+
REPLACE_TAG,
131+
SET_TAG,
132+
SPLICE_TAG
30133
};
31134

32-
export class Closable {
33-
34-
#closed = false;
35-
#listeners = new Set<Fn>();
36-
37-
get closed() { return this.#closed }
38-
39-
@invoke
40-
close() {
41-
this.#listeners.forEach( f => f() )
42-
this.#closed = true;
43-
}
44-
45-
@invoke
46-
onClose( fn : Fn ) {
47-
const _fn = () => {
48-
fn();
49-
this.#offClose( _fn );
50-
}
51-
this.#listeners.add( _fn );
52-
return () => this.#offClose( _fn );
53-
}
54-
55-
@invoke
56-
#offClose( fn : Fn ) { this.#listeners.delete( fn ) }
57-
58-
}
59-
60-
export class Immutable<T extends Value = Value> extends Closable {
61-
62-
static #cacheMap = new WeakMap<Immutable<Value>, AccessorCache<Value>>();
63-
64-
#numConnectionsCreated = 0;
65-
66-
constructor( initValue : T ) {
67-
super();
68-
Immutable.#cacheMap.set( this, deps.assignCache( initValue ) );
69-
deps.numCreated++;
70-
}
71-
72-
close() {
73-
super.close();
74-
Immutable.#cacheMap.delete( this );
75-
}
76-
77-
@invoke
78-
connect() {
79-
return new Connection(
80-
`${ deps.numCreated }:${ ++this.#numConnectionsCreated }`, {
81-
key: this,
82-
map: Immutable.#cacheMap
83-
}
84-
);
85-
}
86-
}
87-
88-
function invoke<C>( method: Function, context: C ) {
89-
return function (
90-
this: Closable,
91-
...args: Array<any>
92-
) {
93-
if( this.closed ) { return }
94-
return method.apply( this, args ) };
95-
}
135+
export default Immutable;

src/index.test.ts renamed to src/main.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Connection } from './connection';
2-
import { Closable, Immutable } from '.';
2+
import { Closable, Immutable } from './main';
33

44
describe( 'Immutable class', () => {
55
describe( 'identity', () => {

src/main.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
type Fn = (...args: any[]) => any;
2+
type Constants = typeof constants;
3+
type TagKeys = {[K in keyof Constants]: K extends `${ string }_TAG` ? K : never}[ keyof Constants ];
4+
type TagNameMap = {[K in TagKeys]: K extends `${ infer P }_TAG` ? P : never}
5+
type TagTypeMap = {[K in TagKeys as TagNameMap[ K ]]: Constants[ K ] };
6+
export type TagType = keyof {[K in keyof TagTypeMap as TagTypeMap[ K ]]: never };
7+
8+
import type { Value } from '.';
9+
10+
import * as _constants from './constants';
11+
12+
import { clonedeep } from './utils';
13+
14+
import AccessorCache from './model/accessor-cache';
15+
16+
import { Connection } from './connection';
17+
18+
export const constants = _constants;
19+
20+
export const Tag = {} as Readonly<TagTypeMap>;
21+
for( let k in constants ) {
22+
if( !k.endsWith( '-TAG' ) ) { continue }
23+
// istanbul ignore next
24+
Tag[ k.slice( 0, -4 ) ] = constants[ k ];
25+
};
26+
Object.freeze( Tag );
27+
28+
export const deps = {
29+
assignCache: <T extends Value>( initValue : T ) => new AccessorCache( clonedeep( initValue ) ),
30+
numCreated: 0
31+
};
32+
33+
export class Closable {
34+
35+
#closed = false;
36+
#listeners = new Set<Fn>();
37+
38+
get closed() { return this.#closed }
39+
40+
@invoke
41+
close() {
42+
this.#listeners.forEach( f => f() )
43+
this.#closed = true;
44+
}
45+
46+
@invoke
47+
onClose( fn : Fn ) {
48+
const _fn = () => {
49+
fn();
50+
this.#offClose( _fn );
51+
}
52+
this.#listeners.add( _fn );
53+
return () => this.#offClose( _fn );
54+
}
55+
56+
@invoke
57+
#offClose( fn : Fn ) { this.#listeners.delete( fn ) }
58+
59+
}
60+
61+
export class Immutable<T extends Value = Value> extends Closable {
62+
63+
static #cacheMap = new WeakMap<Immutable<Value>, AccessorCache<Value>>();
64+
65+
#numConnectionsCreated = 0;
66+
67+
constructor( initValue : T ) {
68+
super();
69+
Immutable.#cacheMap.set( this, deps.assignCache( initValue ) );
70+
deps.numCreated++;
71+
}
72+
73+
close() {
74+
super.close();
75+
Immutable.#cacheMap.delete( this );
76+
}
77+
78+
@invoke
79+
connect() {
80+
return new Connection(
81+
`${ deps.numCreated }:${ ++this.#numConnectionsCreated }`, {
82+
key: this,
83+
map: Immutable.#cacheMap
84+
}
85+
);
86+
}
87+
}
88+
89+
function invoke<C>( method: Function, context: C ) {
90+
return function (
91+
this: Closable,
92+
...args: Array<any>
93+
) {
94+
if( this.closed ) { return }
95+
return method.apply( this, args ) };
96+
}

src/model/accessor-cache/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
UpdatePayloadCore,
1010
UpdatePayloadCoreCloneable,
1111
Value
12-
} from '../../types';
12+
} from '../..';
1313

1414
interface PropertyOriginInfo {
1515
exists: boolean,

src/model/accessor/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { AccessorPayload, AccessorResponse } from '../../types';
1+
import type {
2+
AccessorPayload,
3+
AccessorResponse
4+
} from '../..';
25

36
import type Atom from '../atom';
47

0 commit comments

Comments
 (0)