Repository URL to install this package:
|
Version:
1.2.20 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.shapeToMobx = exports.toStateTree = exports.toMobx = exports.OptionalArray = void 0;
var _mobxStateTree = require("xmobx/mobx-state-tree");
var _chainAbleBoost = require("chain-able-boost");
var _exotic = require("exotic");
require("../thisless/statetree");
var _UniqueDictionary = _interopRequireDefault(require("./UniqueDictionary"));
var _empty = require("./deps/empty");
var _stateTreeTypes = require("./state-tree-types");
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
} // const spread = Typed.spread(Object, Array, Function, Number, String, Boolean)
// const {
// isObj,
// isArray,
// isFunction,
// isNumber,
// isBoolean,
// isString,
// toBoolean,
// toString,
// toNumber,
// toArray,
// toObject,
// } = spread
const fromNativeToMobx = (x, name) => _toMobx((0, _exotic.toEmpty)(x), name);
const stringify = x => JSON.stringify(x, null, 2); // debugging unique dictionary with all types we ever use in all places
const modelDictionary = new _UniqueDictionary.default();
/**
* @private
* @param {*} x any type schema
* @param {string} [modelName=undefined] name of this type as-a-model
* @return {StateTreeModel}
*/
function _toMobx(x, modelName = undefined) {
let typed = -100;
if (x && x.isType) {
return x;
}
if (modelName === undefined) {
console.log('did not pass in a name', x);
modelName = typeof x === 'object' ? Object.keys(x).join('_') : stringify(x);
}
/**
* @param {*} tapped
* @example
* addToDictionary(...)
* //=> 'UserModel [object]': {tapped: MobxType, value: OriginalValue}
*/
const addToDictionary = tapped => {
modelDictionary.add(modelName + ' [' + (0, _exotic.toKindOf)(x) + ']', {
// mob: _toMobx(toEmpty(x), modelName),
tapped,
value: x // kind: x,
// name: modelName,
});
};
/**
* @return {StateTree|undefined} used to use returns for dictionary
*/
const tapWrap = () => {
/**
* @TODO @FIXME @james
* @NOTE this currently will coerce BuiltInNativeFunctions -> mobx
*/
if ((0, _exotic.isFunction)(x)) {
return fromNativeToMobx(x, modelName);
}
if ((0, _exotic.isNumber)(x)) {
return (0, _stateTreeTypes.toMobNumber)(x, modelName);
}
if ((0, _exotic.isString)(x)) {
return (0, _stateTreeTypes.toMobString)(x, modelName);
}
if ((0, _exotic.isBoolean)(x)) {
return (0, _stateTreeTypes.toMobBoolean)(x, modelName);
}
if ((0, _exotic.isObj)(x)) {
if ((0, _exotic.isFrozen)(x)) {
return (0, _stateTreeTypes.toMobFrozen)(x, modelName);
}
if ((0, _exotic.isMap)(x)) {
return (0, _stateTreeTypes.toMobMap)(x, modelName);
}
/**
* @NOTE opinionated
* assumes all values in array are the same
* because this simplifies things
*/
if ((0, _exotic.isArray)(x)) {
const ArrayItemModel = _toMobx(x[0], modelName + 'ArrayItem');
typed = (0, _stateTreeTypes.toMobArray)(ArrayItemModel, modelName);
return undefined;
} else {
const model = {};
(0, _chainAbleBoost.forOwn)(x, (value, key) => {
model[key] = _toMobx(value, key);
});
typed = (0, _stateTreeTypes.toMobModel)(model, modelName);
return undefined;
}
} else {
throw new Error('what else?' + stringify({
x,
modelName
}));
return undefined;
}
};
const tapped = tapWrap();
addToDictionary(tapped);
if (tapped === undefined) {
if (typed === -100) {
const last = modelDictionary.last();
const msg = 'invalid type [' + modelName + '] ' + stringify(last) + stringify(x);
const error = new Error(msg);
throw error;
return _mobxStateTree.types.late(() => {
console.log({
error,
last
});
throw error;
});
}
return typed;
} else {
return tapped;
}
}
/**
* @param {Schema} x ExoticSchema to transform
* @return {Array} MobxModel, EmptyState, SchemaDataTransformer
*
* @TODO
* const Transformer = transformWhereObj(x)
*/
const toMobx = (x, name) => {
const Mobx = _toMobx(x, name);
const Empty = (0, _empty.toEmptyRecursive)(x); // @TODO should move this back with thisless
const creates = Object.getPrototypeOf(Mobx).create; // @TODO need to document exposed dictionary for debug
// console.log(modelDictionary)
return [Mobx, Empty, modelDictionary];
}; // @NOTE currently unused
// @Typed
// class StateTreeType {
// // static coercers = [
// // // recursive
// // [Object, types.model],
// // // [Object.Property, types.model],
// // [Array, types.array],
// // // simple
// // [Function, types.frozen],
// // [Number, types.number],
// // [String, types.string],
// // [Boolean, types.boolean],
// // ]
// }
/**
* @TODO - want to use this to generate a model from fixture json
*/
exports.toStateTree = exports.toMobx = toMobx;
const shapeToMobx = (SchemaShape, name = undefined) => {
const nameFromKeys = name || Object.keys(SchemaShape).join('_');
const _toMobx2 = toMobx(SchemaShape, nameFromKeys),
_toMobx3 = _slicedToArray(_toMobx2, 2),
Model = _toMobx3[0],
Empty = _toMobx3[1]; // console.log({ Model })
return Model;
};
exports.shapeToMobx = shapeToMobx;
const OptionalArray = x => _mobxStateTree.types.maybe(_mobxStateTree.types.array(_mobxStateTree.types.maybe(x))); // --- export default exports
exports.OptionalArray = OptionalArray;