Skip to content

Commit

Permalink
refactor: catch work
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jul 14, 2024
1 parent 7b93249 commit d0f7a35
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
19 changes: 12 additions & 7 deletions demo/index-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,18 @@ const schemaInstanceJSON = {
returns: {
type: 'never'
}
},

{
type: 'catch',
name: 'abc',
innerType: {
type: 'string'
}
}
// Todo: add Promise, function and symbol to regular demo

// Todo: add these above and in schema.js, uncomment nativeEnum there
// {
// type: 'catch'
// },
// {
// type: 'nativeEnum'
// }

Expand All @@ -287,11 +291,12 @@ const schemaInstanceJSON = {
// Sparse arrays
// 'effect'

// Todo: allow function/promise/symbol to be cloneable albeit not through
// structured cloneable; note that typeson has an issue for
// symbol-iterating keys
// Todo: Test/Fix functionality for `toValue`, `getInput`, `setValue`,
// `getValue`, `viewUI`

// Todo: allow function/promise/symbol to be cloneable albeit not through
// structured cloneable; note that typeson has an issue for
// symbol-iterating keys; then add to regular demo and test
]
};

Expand Down
5 changes: 3 additions & 2 deletions src/formats/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ const schema = {
// Todo: Fix `iterate` for schemas (e.g., inject a value method in demo)

/** @type {Map<AvailableZodexType, import('../types.js').AvailableType>} */
// @ts-expect-error Remove after Zodex update allows `catch`, etc.
const zodexToStructuredCloningTypeMap = new Map([
['boolean', 'boolean'],
['number', 'number'],
Expand Down Expand Up @@ -382,10 +383,10 @@ const schema = {
// Todo: Allow non-cloning version to return these too, but filter out
// otherwise
['function', 'function'],
['promise', 'promise']
['promise', 'promise'],

// Todo: Wait until added to Zodex
// ['catch', 'catch'],
['catch', 'catch']
// ['nativeEnum', 'nativeEnum']
]);

Expand Down
72 changes: 72 additions & 0 deletions src/fundamentalTypes/catchType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {$e} from '../utils/templateUtils.js';

/**
* @type {import('../types.js').TypeObject}
*/
const catchType = {
option: ['Catch'],
stringRegex: /^catch\((.*)\)$/u,
// Todo: Fix all the following methods up to `editUI` to work with children
toValue (s) {
return {value: s.slice(6, -1)};
},
getInput ({root}) {
return /** @type {HTMLTextAreaElement} */ ($e(root, 'input'));
},
setValue ({root, value}) {
this.getInput({root}).value = value;
},
getValue ({root}) {
return this.getInput({root}).value;
},
viewUI ({value}) {
return ['span', {dataset: {type: 'catch'}}, [value]];
},
editUI ({
format, type, buildTypeChoices, specificSchemaObject,
topRoot, schemaContent, typeNamespace
}) {
const schemaName =
// @ts-expect-error Waiting for Zodex update
/** @type {import('zodex').SzCatch} */ (
specificSchemaObject
)?.name;
return ['div', {dataset: {type: 'catch'}}, [
['label', [
['b', ['Name']],
' ',
['input', {
disabled: Boolean(schemaName),
value: schemaName ?? ''
}]
]],
['br'],
['label', [
['b', ['Value']],
' ',
...(/** @type {import('../typeChoices.js').BuildTypeChoices} */ (
buildTypeChoices
)({
// resultType,
// eslint-disable-next-line object-shorthand -- TS
topRoot: /** @type {HTMLDivElement} */ (topRoot),
// eslint-disable-next-line object-shorthand -- TS
format: /** @type {import('../formats.js').AvailableFormat} */ (
format
),
schemaOriginal: schemaContent,
// @ts-expect-error Waiting for Zodex update
schemaContent: /** @type {import('zodex').SzCatch} */ (
specificSchemaObject
)?.innerType ?? {type: 'any'},
// schemaState,
state: type,
// itemIndex,
typeNamespace
}).domArray)
]]
]];
}
};

export default catchType;
7 changes: 7 additions & 0 deletions src/fundamentalTypes/functionType.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ const functionType = {
format, type, buildTypeChoices, specificSchemaObject,
topRoot, schemaContent, typeNamespace
}) {
// Todo: Could make a function instance result type which builds a Function
// like `new Function('arg1', 'return !arg1');` this just builds a
// specific function schema instance, and can't have a meaningful
// `getValue`, etc.; add to demo with tests; could also add to
// use with `Promise` so that could also build a meaningful
// implementation

// We want to allow overriding its descriptions
const specificSchemaObj = copyObject(specificSchemaObject);
const argsTuple = /** @type {import('zodex').SzFunction<any, any>} */ (
Expand Down
6 changes: 4 additions & 2 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import noneditableType from './fundamentalTypes/noneditableType.js';
import neverType from './fundamentalTypes/neverType.js';
import promiseType from './fundamentalTypes/promiseType.js';
import functionType from './fundamentalTypes/functionType.js';
import catchType from './fundamentalTypes/catchType.js';

/**
* Utility to retrieve the property value given a legend element.
Expand Down Expand Up @@ -413,11 +414,11 @@ export const getPropertyValueFromLegend = (legend) => {
* "arrayNonindexKeys"|"error"|"errors"|"blob"|"domexception"|"domrect"|
* "dompoint"|"dommatrix"|"resurrectable"|"boolean"|"nan"|"tuple"|
* "record"|"void"|"enum"|"literal"|"symbol"|"never"|"promise"|
* "function"
* "function"|"catch"
* } AvailableType
*/
// Todo: When done with function/promise/symbol, move off here
// Todo: Add when Zodex ready: "catch"|"nativeEnum"
// Todo: Add when Zodex ready: "nativeEnum"

/**
* @typedef {TypeObject & {
Expand Down Expand Up @@ -501,6 +502,7 @@ class Types {
never: neverType,
promise: promiseType,
function: functionType,
catch: catchType,

buffersource: buffersourceType,
dataview: {
Expand Down

0 comments on commit d0f7a35

Please sign in to comment.