Repository URL to install this package:
|
Version:
1.2.18 ▾
|
"use strict";
/**
* @file side effect file, has no exports
*/
// const { types, process } = require('mobx-state-tree')
const _require = require('xmobx/mobx-state-tree'),
types = _require.types,
process = _require.process;
const _require2 = require('chain-able-boost'),
uniq = _require2.uniq;
const _require3 = require("./index"),
selfless = _require3.selfless,
thisless = _require3.thisless,
isUndefined = _require3.isUndefined,
setDescriptorMiddleware = _require3.setDescriptorMiddleware;
const isGenerator = x => Object.prototype.toString.call(x).includes('Generator');
/**
* @see https://github.com/mobxjs/mobx-state-tree/blob/9bb7295c7be320cbc56f74901754674a4eff18c2/src/core/process.ts
* @see https://github.com/mobxjs/mobx-state-tree/#asynchronous-actions
* @param {PropertyDescriptor} desc
* @return {PropertyDescriptor}
*/
const processGeneratorDescriptors = desc => {
if (isGenerator(desc.value)) {
desc.value = process(desc.value);
}
return desc;
};
setDescriptorMiddleware(processGeneratorDescriptors);
/**
* @desc get the ObjectType prototype,
* change the methods to allow using classes
*
* @modifies
* ObjectType.prototype.actions
* ObjectType.prototype.views
* ObjectType.prototype.createStore
*
* @see thisless, selfless
* @param {MobxStateTree.ObjectType} x internal state tree class
* @return {void}
*/
function hackObjectTypePrototype(x) {
// only run this once, could be done many ways
if ('createStore' in x) {
return;
}
const ObjectTypeProto = Object.getPrototypeOf(x);
const _ref = [ObjectTypeProto.actions, ObjectTypeProto.views, ObjectTypeProto.create],
actions = _ref[0],
views = _ref[1],
create = _ref[2];
ObjectTypeProto.actions = function (actionArgs) {
const selflessActions = selfless(actionArgs);
return actions.call(this, selflessActions);
};
ObjectTypeProto.views = function (viewArgs) {
const selflessViews = selfless(viewArgs);
return views.call(this, selflessViews);
};
/**
* @param {Object|undefined} [data=undefined]
* @return {Object} { model, state, store }
*/
ObjectTypeProto.createStore = function (data = undefined) {
// eslint-disable-next-line
const model = this;
const state = isUndefined(data) ? this.create() : this.create(data); // // @TODO @HACK @JAMES - DISABLE IN PRODUCTION
// let tapped
// try {
// tapped = tapWrap()
// } catch (error) {
// const lines = error.message.split('\n')
// lines.forEach(line => console.log(line))
// throw error
// }
// @NOTE can debug here if needed
// console.log(JSON.stringify(model, null, 2))
// @NOTE as redux store
// const store = asReduxStore(state)
// connectReduxDevtools(remoteDev, state)
const store = state;
return {
model,
state,
store
};
}; // @note @perf
// ObjectTypeProto.create = function() {
// try {
// const created = create.apply(this, arguments)
// return created
// } catch (error) {
// // @TODO diff match-patch
// error.message.split('\n').forEach(originalLine => {
// const obj = {}
// const messageLines = originalLine
// .split(/[\,\{\}\]\[]/gim)
// .filter(line => line)
// .filter(uniq)
// .map(line => line.replace(/\"/gim, ''))
// messageLines.forEach(line => {
// const [property, value] = line.split(':')
// obj[property] = value
// })
// })
// throw error
// }
// }
}
/**
* @see https://github.com/mobxjs/mobx-state-tree/blob/master/src/types/complex-types/object.ts
*/
hackObjectTypePrototype(types.model({
fake: types.number
}));