shelving 1.98.0 → 1.98.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/Resource.d.ts +2 -2
- package/api/Resource.js +6 -5
- package/db/ItemReference.d.ts +2 -2
- package/db/ItemState.js +2 -2
- package/db/QueryReference.d.ts +2 -2
- package/db/QueryState.js +3 -3
- package/package.json +1 -1
- package/provider/ValidationProvider.js +2 -2
- package/schema/Schema.d.ts +2 -2
- package/sequence/DeferredSequence.d.ts +3 -3
- package/state/ArrayState.js +3 -3
- package/state/BooleanState.js +1 -1
- package/state/DataState.js +5 -5
- package/state/DictionaryState.js +5 -5
- package/state/State.d.ts +6 -6
- package/state/State.js +17 -15
- package/util/async.d.ts +3 -3
- package/util/callback.d.ts +9 -5
- package/util/index.d.ts +1 -0
- package/util/index.js +1 -0
- package/util/sequence.d.ts +3 -3
- package/util/validate.d.ts +1 -7
- package/util/validate.js +6 -10
package/api/Resource.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Validator } from "../util/validate.js";
|
|
2
2
|
/**
|
|
3
3
|
* An abstract API resource definition, used to specify types for e.g. serverless functions..
|
|
4
4
|
*
|
|
5
5
|
* @param payload The `Validator` the payload must conform to (defaults to `undefined` if not specified).
|
|
6
6
|
* @param returns The `Validator` the function's returned value must conform to (defaults to `undefined` if not specified).
|
|
7
7
|
*/
|
|
8
|
-
export declare class Resource<P = unknown, R = void> implements
|
|
8
|
+
export declare class Resource<P = unknown, R = void> implements Validator<R> {
|
|
9
9
|
/** Payload validator. */
|
|
10
10
|
readonly payload: Validator<P>;
|
|
11
11
|
/** Result validator. */
|
package/api/Resource.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ValidationError } from "../error/ValidationError.js";
|
|
2
2
|
import { Feedback } from "../feedback/Feedback.js";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
// Undefined validator.
|
|
4
|
+
const UNDEFINED = { validate: () => undefined };
|
|
5
5
|
/**
|
|
6
6
|
* An abstract API resource definition, used to specify types for e.g. serverless functions..
|
|
7
7
|
*
|
|
@@ -20,7 +20,8 @@ export class Resource {
|
|
|
20
20
|
* @throws Feedback if the payload could not be validated.
|
|
21
21
|
*/
|
|
22
22
|
prepare(unsafePayload) {
|
|
23
|
-
|
|
23
|
+
var _a;
|
|
24
|
+
return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.validate(unsafePayload);
|
|
24
25
|
}
|
|
25
26
|
/**
|
|
26
27
|
* Validate a result for this resource.
|
|
@@ -30,7 +31,7 @@ export class Resource {
|
|
|
30
31
|
*/
|
|
31
32
|
validate(unsafeResult) {
|
|
32
33
|
try {
|
|
33
|
-
return validate(unsafeResult
|
|
34
|
+
return this.result.validate(unsafeResult);
|
|
34
35
|
}
|
|
35
36
|
catch (thrown) {
|
|
36
37
|
if (thrown instanceof Feedback)
|
|
@@ -39,6 +40,6 @@ export class Resource {
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
|
-
export function RESOURCE(payload =
|
|
43
|
+
export function RESOURCE(payload = UNDEFINED, result = UNDEFINED) {
|
|
43
44
|
return new Resource(payload, result);
|
|
44
45
|
}
|
package/db/ItemReference.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { DeleteChange, SetChange, UpdateChange } from "./Change.js";
|
|
2
2
|
import type { AsyncProvider, Provider } from "../provider/Provider.js";
|
|
3
3
|
import type { ImmutableArray } from "../util/array.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { ErrorCallback, StopCallback, ValueCallback } from "../util/callback.js";
|
|
5
5
|
import type { Data } from "../util/data.js";
|
|
6
6
|
import type { Query } from "../util/query.js";
|
|
7
7
|
import type { Updates } from "../util/update.js";
|
|
@@ -61,7 +61,7 @@ declare abstract class AbstractItemReference<T extends Data = Data> implements A
|
|
|
61
61
|
getDelete(): DeleteChange;
|
|
62
62
|
toString(): string;
|
|
63
63
|
/** Subscribe to this item. */
|
|
64
|
-
subscribe(onNext?:
|
|
64
|
+
subscribe(onNext?: ValueCallback<ItemValue<T>>, onError?: ErrorCallback): StopCallback;
|
|
65
65
|
[Symbol.asyncIterator](): AsyncIterator<ItemValue<T>>;
|
|
66
66
|
}
|
|
67
67
|
/** Reference to an item in a synchronous database. */
|
package/db/ItemState.js
CHANGED
|
@@ -34,12 +34,12 @@ export class ItemState extends State {
|
|
|
34
34
|
}
|
|
35
35
|
async _refresh() {
|
|
36
36
|
this.busy.set(true);
|
|
37
|
-
this.
|
|
37
|
+
this.reason = undefined; // Optimistically clear the error.
|
|
38
38
|
try {
|
|
39
39
|
this.set(await this.ref.value);
|
|
40
40
|
}
|
|
41
41
|
catch (thrown) {
|
|
42
|
-
this.
|
|
42
|
+
this.reason = thrown;
|
|
43
43
|
}
|
|
44
44
|
finally {
|
|
45
45
|
this.busy.set(false);
|
package/db/QueryReference.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ItemArray, ItemData, ItemQuery, ItemValue } from "./ItemReference.js";
|
|
2
2
|
import type { AsyncProvider, Provider } from "../provider/Provider.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { ErrorCallback, StopCallback, ValueCallback } from "../util/callback.js";
|
|
4
4
|
import type { Data } from "../util/data.js";
|
|
5
5
|
import type { Updates } from "../util/update.js";
|
|
6
6
|
/** Reference to a set of items in a sync or async provider. */
|
|
@@ -62,7 +62,7 @@ declare abstract class AbstractQueryReference<T extends Data = Data> implements
|
|
|
62
62
|
abstract delete(): number | PromiseLike<number>;
|
|
63
63
|
toString(): string;
|
|
64
64
|
/** Subscribe to this item. */
|
|
65
|
-
subscribe(onNext?:
|
|
65
|
+
subscribe(onNext?: ValueCallback<ItemArray<T>>, onError?: ErrorCallback): StopCallback;
|
|
66
66
|
[Symbol.asyncIterator](): AsyncIterator<ItemArray<T>>;
|
|
67
67
|
}
|
|
68
68
|
/** Reference to a set of items in a provider. */
|
package/db/QueryState.js
CHANGED
|
@@ -66,7 +66,7 @@ export class QueryState extends State {
|
|
|
66
66
|
}
|
|
67
67
|
async _refresh() {
|
|
68
68
|
this.busy.set(true);
|
|
69
|
-
this.
|
|
69
|
+
this.reason = undefined; // Optimistically clear the error.
|
|
70
70
|
try {
|
|
71
71
|
const items = await this.ref.items;
|
|
72
72
|
this._hasMore = items.length >= this.limit; // If the query returned {limit} or more items, we can assume there are more items waiting to be queried.
|
|
@@ -90,7 +90,7 @@ export class QueryState extends State {
|
|
|
90
90
|
}
|
|
91
91
|
async _loadMore() {
|
|
92
92
|
this.busy.set(true);
|
|
93
|
-
this.
|
|
93
|
+
this.reason = undefined; // Optimistically clear the error.
|
|
94
94
|
try {
|
|
95
95
|
const last = this.last;
|
|
96
96
|
const ref = last ? this.ref.with(getAfterQuery(this.ref.query, last)) : this.ref;
|
|
@@ -99,7 +99,7 @@ export class QueryState extends State {
|
|
|
99
99
|
this._hasMore = items.length >= this.limit; // If the query returned {limit} or more items, we can assume there are more items waiting to be queried.
|
|
100
100
|
}
|
|
101
101
|
catch (thrown) {
|
|
102
|
-
this.
|
|
102
|
+
this.reason = thrown;
|
|
103
103
|
}
|
|
104
104
|
finally {
|
|
105
105
|
this.busy.set(false);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ValidationError } from "../error/ValidationError.js";
|
|
2
2
|
import { Feedback } from "../feedback/Feedback.js";
|
|
3
3
|
import { updateData } from "../util/update.js";
|
|
4
|
-
import {
|
|
4
|
+
import { validateWithContext } from "../util/validate.js";
|
|
5
5
|
// Constants.
|
|
6
6
|
const VALIDATION_CONTEXT_GET = { action: "get", id: true };
|
|
7
7
|
const VALIDATION_CONTEXT_ADD = { action: "add" };
|
|
@@ -52,7 +52,7 @@ export class ValidationProvider extends BaseValidationProvider {
|
|
|
52
52
|
return _validateItemArray(collection, this.source.getQuery(collection, constraints), this.getSchema(collection));
|
|
53
53
|
}
|
|
54
54
|
setQuery(collection, constraints, value) {
|
|
55
|
-
return this.source.setQuery(collection, constraints,
|
|
55
|
+
return this.source.setQuery(collection, constraints, this.getSchema(collection).validate(value));
|
|
56
56
|
}
|
|
57
57
|
updateQuery(collection, constraints, updates) {
|
|
58
58
|
validateWithContext(updateData({}, updates), this.getSchema(collection), VALIDATION_CONTEXT_UPDATE);
|
package/schema/Schema.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Validator } from "../util/validate.js";
|
|
2
2
|
/** Options allowed by a `Schema` instance. */
|
|
3
3
|
export type SchemaOptions = {
|
|
4
4
|
/** Title of the schema, e.g. for using as the title of a corresponding field. */
|
|
@@ -15,7 +15,7 @@ export type SchemaOptions = {
|
|
|
15
15
|
* - Type `T` represents the type of value `validate()` returns.
|
|
16
16
|
* - `validate()` returns `Invalid` if value was not valid.
|
|
17
17
|
*/
|
|
18
|
-
export declare abstract class Schema<T extends unknown = unknown> implements
|
|
18
|
+
export declare abstract class Schema<T extends unknown = unknown> implements Validator<T> {
|
|
19
19
|
/** Title of the schema, e.g. for using as the title of a corresponding field. */
|
|
20
20
|
readonly title: string;
|
|
21
21
|
/** Description of the schema, e.g. for using as a description in a corresponding field. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Deferred } from "../util/async.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ErrorCallback, StopCallback, ValueCallback } from "../util/callback.js";
|
|
3
3
|
import { AbstractSequence } from "./AbstractSequence.js";
|
|
4
4
|
/**
|
|
5
5
|
* Deferred sequence of values that can be async iterated and new values can be published.
|
|
@@ -16,7 +16,7 @@ export declare class DeferredSequence<T = void, R = void> extends AbstractSequen
|
|
|
16
16
|
/** Get the next promise to be deferred/rejected. */
|
|
17
17
|
get promise(): Promise<T>;
|
|
18
18
|
/** Resolve the current deferred in the sequence. */
|
|
19
|
-
readonly resolve:
|
|
19
|
+
readonly resolve: ValueCallback<T>;
|
|
20
20
|
private _nextValue;
|
|
21
21
|
/** Reject the current deferred in the sequence. */
|
|
22
22
|
readonly reject: ErrorCallback;
|
|
@@ -32,5 +32,5 @@ export declare class DeferredSequence<T = void, R = void> extends AbstractSequen
|
|
|
32
32
|
/** Pull values from a source sequence until the returned stop function is called. */
|
|
33
33
|
from(source: AsyncIterable<T>, onError?: ErrorCallback): StopCallback;
|
|
34
34
|
/** Subscrbe to the value of the sequence with a callback until the returned stop function is called. */
|
|
35
|
-
to(onNext:
|
|
35
|
+
to(onNext: ValueCallback<T>, onError?: ErrorCallback): StopCallback;
|
|
36
36
|
}
|
package/state/ArrayState.js
CHANGED
|
@@ -11,15 +11,15 @@ export class ArrayState extends State {
|
|
|
11
11
|
}
|
|
12
12
|
/** Add items to this array. */
|
|
13
13
|
add(...items) {
|
|
14
|
-
this.
|
|
14
|
+
this.value = withArrayItems(this.value, ...items);
|
|
15
15
|
}
|
|
16
16
|
/** Remove items from this array. */
|
|
17
17
|
delete(...items) {
|
|
18
|
-
this.
|
|
18
|
+
this.value = omitArrayItems(this.value, ...items);
|
|
19
19
|
}
|
|
20
20
|
/** Toggle items in this array. */
|
|
21
21
|
toggle(...items) {
|
|
22
|
-
this.
|
|
22
|
+
this.value = toggleArrayItems(this.value, ...items);
|
|
23
23
|
}
|
|
24
24
|
/** Iterate over the items. */
|
|
25
25
|
[Symbol.iterator]() {
|
package/state/BooleanState.js
CHANGED
package/state/DataState.js
CHANGED
|
@@ -10,7 +10,7 @@ export class DataState extends State {
|
|
|
10
10
|
}
|
|
11
11
|
/** Update several props in this data. */
|
|
12
12
|
update(updates) {
|
|
13
|
-
this.
|
|
13
|
+
this.value = updateData(this.data, updates);
|
|
14
14
|
}
|
|
15
15
|
/** Update a single named prop in this data. */
|
|
16
16
|
getProp(name) {
|
|
@@ -18,7 +18,7 @@ export class DataState extends State {
|
|
|
18
18
|
}
|
|
19
19
|
/** Update a single named prop in this data. */
|
|
20
20
|
setProp(name, value) {
|
|
21
|
-
this.
|
|
21
|
+
this.value = withProp(this.data, name, value);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
/** State that stores an optional data object and has additional methods to help with that. */
|
|
@@ -33,7 +33,7 @@ export class OptionalDataState extends State {
|
|
|
33
33
|
}
|
|
34
34
|
/** Update several props in this data. */
|
|
35
35
|
update(updates) {
|
|
36
|
-
this.
|
|
36
|
+
this.value = updateData(this.data, updates);
|
|
37
37
|
}
|
|
38
38
|
/** Update a single named prop in this data. */
|
|
39
39
|
getProp(name) {
|
|
@@ -41,10 +41,10 @@ export class OptionalDataState extends State {
|
|
|
41
41
|
}
|
|
42
42
|
/** Update a single named prop in this data. */
|
|
43
43
|
setProp(name, value) {
|
|
44
|
-
this.
|
|
44
|
+
this.value = withProp(this.data, name, value);
|
|
45
45
|
}
|
|
46
46
|
/** Set the data to `undefined`. */
|
|
47
47
|
unset() {
|
|
48
|
-
this.
|
|
48
|
+
this.value = undefined;
|
|
49
49
|
}
|
|
50
50
|
}
|
package/state/DictionaryState.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { omitDictionaryItems } from "../util/dictionary.js";
|
|
1
|
+
import { getDictionaryItems, omitDictionaryItems } from "../util/dictionary.js";
|
|
2
2
|
import { withProp } from "../util/object.js";
|
|
3
3
|
import { updateData } from "../util/update.js";
|
|
4
4
|
import { State } from "./State.js";
|
|
@@ -13,11 +13,11 @@ export class DictionaryState extends State {
|
|
|
13
13
|
}
|
|
14
14
|
/** Set a named entry in this object with a different value. */
|
|
15
15
|
update(updates) {
|
|
16
|
-
this.
|
|
16
|
+
this.value = updateData(this.value, updates);
|
|
17
17
|
}
|
|
18
18
|
/** Remove a named entry from this object. */
|
|
19
19
|
delete(...keys) {
|
|
20
|
-
this.
|
|
20
|
+
this.value = omitDictionaryItems(this.value, ...keys);
|
|
21
21
|
}
|
|
22
22
|
/** Get an item in this dictionary. */
|
|
23
23
|
getItem(name) {
|
|
@@ -25,10 +25,10 @@ export class DictionaryState extends State {
|
|
|
25
25
|
}
|
|
26
26
|
/** Set an item in this dictionary. */
|
|
27
27
|
setItem(name, value) {
|
|
28
|
-
this.
|
|
28
|
+
this.value = withProp(this.value, name, value);
|
|
29
29
|
}
|
|
30
30
|
/** Iterate over the entries of the object. */
|
|
31
31
|
[Symbol.iterator]() {
|
|
32
|
-
return
|
|
32
|
+
return getDictionaryItems(this.value)[Symbol.iterator]();
|
|
33
33
|
}
|
|
34
34
|
}
|
package/state/State.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { ErrorCallback, StopCallback, ValueCallback } from "../util/callback.js";
|
|
2
|
+
import type { Validator } from "../util/validate.js";
|
|
3
3
|
import { DeferredSequence } from "../sequence/DeferredSequence.js";
|
|
4
4
|
/** Any `State` instance. */
|
|
5
5
|
export type AnyState = State<any>;
|
|
@@ -19,11 +19,12 @@ export type StateOptions<T> = {
|
|
|
19
19
|
* - To set the state to be loading, use the `NONE` constant or a `Promise` value.
|
|
20
20
|
* - To set the state to an explicit value, use that value or another `State` instance with a value.
|
|
21
21
|
* */
|
|
22
|
-
export declare class State<T> implements AsyncIterable<T>,
|
|
22
|
+
export declare class State<T> implements AsyncIterable<T>, Validator<T> {
|
|
23
23
|
/** Deferred sequence this state uses to issue values as they change. */
|
|
24
24
|
readonly next: DeferredSequence<T>;
|
|
25
25
|
/** Current value of the state (or throw a promise that resolves when this state receives its next value or error). */
|
|
26
26
|
get value(): T;
|
|
27
|
+
set value(next: T);
|
|
27
28
|
private _value;
|
|
28
29
|
/** Time this state was last updated with a new value. */
|
|
29
30
|
get time(): number | undefined;
|
|
@@ -34,19 +35,18 @@ export declare class State<T> implements AsyncIterable<T>, Validatable<T> {
|
|
|
34
35
|
get age(): number;
|
|
35
36
|
/** Current error of this state (or `undefined` if there is no reason). */
|
|
36
37
|
get reason(): unknown;
|
|
38
|
+
set reason(reason: Error | unknown);
|
|
37
39
|
private _reason;
|
|
38
40
|
/** State is initiated with an initial state. */
|
|
39
41
|
constructor(options?: StateOptions<T>);
|
|
40
42
|
/** Set the value of the state. */
|
|
41
43
|
set(next: T): void;
|
|
42
|
-
/** Set the error reason of the state (will be */
|
|
43
|
-
error(reason: unknown): void;
|
|
44
44
|
/** Set the value of the state as values are pulled from a sequence. */
|
|
45
45
|
through(sequence: AsyncIterable<T>): AsyncIterable<T>;
|
|
46
46
|
/** Pull values from a source sequence until the returned stop function is called. */
|
|
47
47
|
from(source: AsyncIterable<T>, onError?: ErrorCallback): StopCallback;
|
|
48
48
|
/** Push values to another state or callback to this state until the returned stop function is called. */
|
|
49
|
-
to(target:
|
|
49
|
+
to(target: ValueCallback<T>, onError?: ErrorCallback): StopCallback;
|
|
50
50
|
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
51
51
|
/** Validate data set on this state. */
|
|
52
52
|
validate(value: T): T;
|
package/state/State.js
CHANGED
|
@@ -20,6 +20,15 @@ export class State {
|
|
|
20
20
|
throw this.next;
|
|
21
21
|
return this._value;
|
|
22
22
|
}
|
|
23
|
+
set value(next) {
|
|
24
|
+
const value = this.validate(next);
|
|
25
|
+
if (value !== this._value || this._reason !== undefined) {
|
|
26
|
+
this._reason = undefined;
|
|
27
|
+
this._value = value;
|
|
28
|
+
this._time = Date.now();
|
|
29
|
+
this.next.resolve(value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
23
32
|
/** Time this state was last updated with a new value. */
|
|
24
33
|
get time() {
|
|
25
34
|
return this._time;
|
|
@@ -37,6 +46,12 @@ export class State {
|
|
|
37
46
|
get reason() {
|
|
38
47
|
return this._reason;
|
|
39
48
|
}
|
|
49
|
+
set reason(reason) {
|
|
50
|
+
this._reason = reason;
|
|
51
|
+
if (reason !== undefined) {
|
|
52
|
+
this.next.reject(reason);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
40
55
|
/** State is initiated with an initial state. */
|
|
41
56
|
constructor(options = {}) {
|
|
42
57
|
this._value = NONE;
|
|
@@ -50,25 +65,12 @@ export class State {
|
|
|
50
65
|
}
|
|
51
66
|
/** Set the value of the state. */
|
|
52
67
|
set(next) {
|
|
53
|
-
|
|
54
|
-
if (value !== this._value || this._reason !== undefined) {
|
|
55
|
-
this._reason = undefined;
|
|
56
|
-
this._value = value;
|
|
57
|
-
this._time = Date.now();
|
|
58
|
-
this.next.resolve(value);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
/** Set the error reason of the state (will be */
|
|
62
|
-
error(reason) {
|
|
63
|
-
this._reason = reason;
|
|
64
|
-
if (reason !== undefined) {
|
|
65
|
-
this.next.reject(reason);
|
|
66
|
-
}
|
|
68
|
+
this.value = next;
|
|
67
69
|
}
|
|
68
70
|
/** Set the value of the state as values are pulled from a sequence. */
|
|
69
71
|
async *through(sequence) {
|
|
70
72
|
for await (const value of sequence) {
|
|
71
|
-
this.
|
|
73
|
+
this.value = value;
|
|
72
74
|
yield value;
|
|
73
75
|
}
|
|
74
76
|
}
|
package/util/async.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ErrorCallback, ValueCallback } from "./callback.js";
|
|
2
2
|
/** Is a value an asynchronous value implementing a `then()` function. */
|
|
3
3
|
export declare const isAsync: <T>(value: T | PromiseLike<T>) => value is PromiseLike<T>;
|
|
4
4
|
/** Is a value a synchronous value. */
|
|
@@ -21,7 +21,7 @@ export declare function runMicrotasks(): Promise<void>;
|
|
|
21
21
|
export declare abstract class AbstractPromise<T> extends Promise<T> {
|
|
22
22
|
static get [Symbol.species](): PromiseConstructor;
|
|
23
23
|
/** Resolve this promise with a value. */
|
|
24
|
-
protected readonly _resolve:
|
|
24
|
+
protected readonly _resolve: ValueCallback<T>;
|
|
25
25
|
/** Reject this promise with a reason. */
|
|
26
26
|
protected readonly _reject: ErrorCallback;
|
|
27
27
|
constructor();
|
|
@@ -29,7 +29,7 @@ export declare abstract class AbstractPromise<T> extends Promise<T> {
|
|
|
29
29
|
/** Deferred allows you to access the internal resolve/reject callbacks of a `Promise` */
|
|
30
30
|
export type Deferred<T> = {
|
|
31
31
|
promise: Promise<T>;
|
|
32
|
-
resolve:
|
|
32
|
+
resolve: ValueCallback<T>;
|
|
33
33
|
reject: ErrorCallback;
|
|
34
34
|
};
|
|
35
35
|
/**
|
package/util/callback.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
/** Callback function
|
|
2
|
-
export type Callback
|
|
3
|
-
/** Callback function
|
|
4
|
-
export type AsyncCallback
|
|
1
|
+
/** Callback function with no value. */
|
|
2
|
+
export type Callback = () => void;
|
|
3
|
+
/** Callback function with no value. */
|
|
4
|
+
export type AsyncCallback = () => void | PromiseLike<void>;
|
|
5
|
+
/** Callback function that receives a value. */
|
|
6
|
+
export type ValueCallback<T> = (value: T) => void;
|
|
7
|
+
/** Callback function that receives a value. */
|
|
8
|
+
export type AsyncValueCallback<T = void> = (value: T) => void | PromiseLike<void>;
|
|
5
9
|
/** Callback function that handles an error. */
|
|
6
10
|
export type ErrorCallback = (reason: Error | unknown) => void;
|
|
7
11
|
/** Callback function that starts something (and returns an optional stop callback). */
|
|
@@ -12,7 +16,7 @@ export type StopCallback = () => void;
|
|
|
12
16
|
export declare function call(callback: () => void | PromiseLike<void>): void;
|
|
13
17
|
export declare function call<T>(callback: (value: T) => void | PromiseLike<void>, value: T): void;
|
|
14
18
|
/** Return a callback function that safely calls a callback function (possibly with a value). */
|
|
15
|
-
export declare function called<T>(dispatcher:
|
|
19
|
+
export declare function called<T>(dispatcher: AsyncValueCallback<T>): ValueCallback<T>;
|
|
16
20
|
/** Safely call a callback method (possibly wth a value). */
|
|
17
21
|
export declare function callMethod<M extends string | symbol>(obj: {
|
|
18
22
|
[K in M]: () => void | PromiseLike<void>;
|
package/util/index.d.ts
CHANGED
package/util/index.js
CHANGED
package/util/sequence.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AsyncValueCallback, ErrorCallback, StopCallback, ValueCallback } from "./callback.js";
|
|
2
2
|
import { STOP } from "./constants.js";
|
|
3
3
|
/**
|
|
4
4
|
* Is a value an async iterable object?
|
|
@@ -11,8 +11,8 @@ export declare function repeatUntil<T>(source: AsyncIterable<T>, ...signals: [Pr
|
|
|
11
11
|
/** Infinite sequence that yields every X milliseconds (yields a count of the number of iterations). */
|
|
12
12
|
export declare function repeatDelay(ms: number): AsyncIterable<number>;
|
|
13
13
|
/** Dispatch items in a sequence to a (possibly async) callback. */
|
|
14
|
-
export declare function dispatchSequence<T>(sequence: AsyncIterable<T>, onNext:
|
|
14
|
+
export declare function dispatchSequence<T>(sequence: AsyncIterable<T>, onNext: AsyncValueCallback<T>): AsyncIterable<T>;
|
|
15
15
|
/** Get the first value from an async iterator. **/
|
|
16
16
|
export declare function getNextValue<T>(sequence: AsyncIterable<T>): Promise<T>;
|
|
17
17
|
/** Pull values from a sequence until the returned function is called. */
|
|
18
|
-
export declare function runSequence<T>(sequence: AsyncIterable<T>, onNext?:
|
|
18
|
+
export declare function runSequence<T>(sequence: AsyncIterable<T>, onNext?: ValueCallback<T>, onError?: ErrorCallback): StopCallback;
|
package/util/validate.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ImmutableArray, PossibleArray } from "./array.js";
|
|
|
2
2
|
import type { Data } from "./data.js";
|
|
3
3
|
import type { ImmutableDictionary, PossibleDictionary } from "./dictionary.js";
|
|
4
4
|
/** Object that can validate an unknown value with its `validate()` method. */
|
|
5
|
-
export interface
|
|
5
|
+
export interface Validator<T> {
|
|
6
6
|
/**
|
|
7
7
|
* `validate()` method accepts an unsafe value and returns a valid value.
|
|
8
8
|
*
|
|
@@ -15,10 +15,6 @@ export interface Validatable<T> {
|
|
|
15
15
|
*/
|
|
16
16
|
validate(unsafeValue: unknown): T;
|
|
17
17
|
}
|
|
18
|
-
/** Function that can validate a value. */
|
|
19
|
-
export type Validate<T> = (unsafeValue: unknown) => T;
|
|
20
|
-
/** Something that can validate a value. */
|
|
21
|
-
export type Validator<T = unknown> = Validatable<T> | Validate<T>;
|
|
22
18
|
/** Extract the type from a validator. */
|
|
23
19
|
export type ValidatorType<X> = X extends Validator<infer Y> ? Y : never;
|
|
24
20
|
/** A set of named validators in `{ name: Validator }` format. */
|
|
@@ -29,8 +25,6 @@ export type Validators<T extends Data = Data> = {
|
|
|
29
25
|
export type ValidatorsType<T> = {
|
|
30
26
|
readonly [K in keyof T]: ValidatorType<T[K]>;
|
|
31
27
|
};
|
|
32
|
-
/** Validate an unknown value with a validator. */
|
|
33
|
-
export declare function validate<T>(unsafeValue: unknown, validator: Validator<T>): T;
|
|
34
28
|
/** Get value that validates against a given `Validator`, or throw `ValidationError` */
|
|
35
29
|
export declare function getValid<T>(unsafeValue: unknown, validator: Validator<T>): T;
|
|
36
30
|
/** Assert that a value validates against a given `Validator`, or throw `ValidationError` */
|
package/util/validate.js
CHANGED
|
@@ -5,14 +5,10 @@ import { getLastItem, isArray } from "./array.js";
|
|
|
5
5
|
import { getDataProps } from "./data.js";
|
|
6
6
|
import { getDictionaryItems } from "./dictionary.js";
|
|
7
7
|
import { isIterable } from "./iterate.js";
|
|
8
|
-
/** Validate an unknown value with a validator. */
|
|
9
|
-
export function validate(unsafeValue, validator) {
|
|
10
|
-
return typeof validator === "function" ? validator(unsafeValue) : validator.validate(unsafeValue);
|
|
11
|
-
}
|
|
12
8
|
/** Get value that validates against a given `Validator`, or throw `ValidationError` */
|
|
13
9
|
export function getValid(unsafeValue, validator) {
|
|
14
10
|
try {
|
|
15
|
-
return validate(unsafeValue
|
|
11
|
+
return validator.validate(unsafeValue);
|
|
16
12
|
}
|
|
17
13
|
catch (thrown) {
|
|
18
14
|
if (thrown instanceof Feedback)
|
|
@@ -37,7 +33,7 @@ export function* validateItems(unsafeItems, validator) {
|
|
|
37
33
|
const feedbacks = {};
|
|
38
34
|
for (const unsafeItem of unsafeItems) {
|
|
39
35
|
try {
|
|
40
|
-
yield validate(unsafeItem
|
|
36
|
+
yield validator.validate(unsafeItem);
|
|
41
37
|
}
|
|
42
38
|
catch (thrown) {
|
|
43
39
|
if (!(thrown instanceof Feedback))
|
|
@@ -65,7 +61,7 @@ export function validateArray(unsafeArray, validator) {
|
|
|
65
61
|
const feedbacks = {};
|
|
66
62
|
for (const unsafeItem of unsafeArray) {
|
|
67
63
|
try {
|
|
68
|
-
const safeItem = validate(unsafeItem
|
|
64
|
+
const safeItem = validator.validate(unsafeItem);
|
|
69
65
|
safeArray.push(safeItem);
|
|
70
66
|
if (!changed && safeItem !== unsafeItem)
|
|
71
67
|
changed = true;
|
|
@@ -95,7 +91,7 @@ export function validateDictionary(unsafeDictionary, validator) {
|
|
|
95
91
|
const feedbacks = {};
|
|
96
92
|
for (const [key, unsafeValue] of getDictionaryItems(unsafeDictionary)) {
|
|
97
93
|
try {
|
|
98
|
-
const safeValue = validate(unsafeValue
|
|
94
|
+
const safeValue = validator.validate(unsafeValue);
|
|
99
95
|
safeDictionary[key] = safeValue;
|
|
100
96
|
if (!changed && safeValue !== unsafeValue)
|
|
101
97
|
changed = true;
|
|
@@ -131,7 +127,7 @@ export function validateData(unsafeData, validators) {
|
|
|
131
127
|
if (unsafeValue === undefined && partial)
|
|
132
128
|
continue; // Silently skip `undefined` props if in partial mode.
|
|
133
129
|
try {
|
|
134
|
-
const safeValue = validate(unsafeValue
|
|
130
|
+
const safeValue = validator.validate(unsafeValue);
|
|
135
131
|
safeData[key] = safeValue;
|
|
136
132
|
if (!changed && safeValue !== unsafeValue)
|
|
137
133
|
changed = true;
|
|
@@ -154,7 +150,7 @@ export const getValidationContext = () => getLastItem(CONTEXTS);
|
|
|
154
150
|
/** Validate a unknown value with a validator, and supply a context that can be read during the validation process. */
|
|
155
151
|
export function validateWithContext(unsafeValue, validator, context) {
|
|
156
152
|
CONTEXTS.push(context);
|
|
157
|
-
const validValue = validate(unsafeValue
|
|
153
|
+
const validValue = validator.validate(unsafeValue);
|
|
158
154
|
CONTEXTS.pop();
|
|
159
155
|
return validValue;
|
|
160
156
|
}
|