modeled-mobx
Advanced tools
Comparing version 0.3.3 to 0.3.5
@@ -1,2 +0,3 @@ | ||
import { ModelSchema } from './types'; | ||
export declare function addModelInterceptors<T extends object>(model: T, schema: ModelSchema<T, keyof T>): void; | ||
import { Model, ModelInstance } from './types'; | ||
export declare function configureHasOne(parent: ModelInstance, property: string, desiredModel: Model): void; | ||
export declare function configureHasMany(parent: ModelInstance, property: string, desiredModel: Model): void; |
@@ -34,13 +34,13 @@ var mobx = require('mobx'); | ||
function decorate(model, propertyKey, options) { | ||
mobx.observable(model, propertyKey); | ||
findOrCreateSchema(model).recordProperty(propertyKey, options); | ||
function decorate(model, property, options) { | ||
mobx.observable(model, property); | ||
findOrCreateSchema(model).recordProperty(property, options); | ||
} | ||
function field(target, propertyKey) { | ||
if (propertyKey) { | ||
decorate(target, propertyKey, field); | ||
function field(target, property) { | ||
if (property) { | ||
decorate(target, property, field); | ||
} else { | ||
return (target, propertyKey) => { | ||
decorate(target, propertyKey, field); | ||
return (target, property) => { | ||
decorate(target, property, field); | ||
}; | ||
@@ -50,4 +50,4 @@ } | ||
function model(model) { | ||
const decorator = (target, propertyKey) => { | ||
decorate(target, propertyKey, decorator); | ||
const decorator = (target, property) => { | ||
decorate(target, property, decorator); | ||
}; | ||
@@ -59,3 +59,3 @@ | ||
function getSchema(model) { | ||
return model[$schema] || false; | ||
return model[$schema]; | ||
} | ||
@@ -78,31 +78,2 @@ function findOrCreateSchema(model) { | ||
function modelize(model, properties, options) { | ||
const decoratedSchema = getSchema(model); | ||
if (!properties) { | ||
mobx.makeObservable(model); | ||
if (!decoratedSchema) { | ||
return; // no decorators have created schema | ||
} | ||
return; | ||
} | ||
const schema = decoratedSchema || findOrCreateSchema(model); | ||
const mobxAnnotations = {}; | ||
Object.keys(properties).forEach(property => { | ||
if (!schema.recordProperty(property, properties[property])) { | ||
mobxAnnotations[property] = properties[property]; | ||
} | ||
}); | ||
schema.properties.forEach((options, key) => { | ||
if (!options.annotated) { | ||
mobxAnnotations[key] = mobx.observable; | ||
options.annotated = true; | ||
} | ||
}); | ||
mobx.makeObservable(model, mobxAnnotations, options); | ||
} | ||
const Parents = new WeakMap(); | ||
@@ -192,2 +163,85 @@ function recordParentOf(child, parent) { | ||
function configureHasOne(parent, property, desiredModel) { | ||
mobx.intercept(parent, property, change => { | ||
if (change.newValue) { | ||
if (!(change.newValue instanceof desiredModel)) { | ||
change.newValue = hydrate(desiredModel, change.newValue); | ||
} | ||
recordParentOf(change.newValue, parent); | ||
} | ||
return change; | ||
}); | ||
} | ||
function configureHasMany(parent, property, desiredModel) { | ||
const value = mobx.observable.array(); | ||
parent[property] = value; | ||
const toModel = attrs => { | ||
let child; | ||
if (attrs instanceof desiredModel) { | ||
child = attrs; | ||
} else { | ||
child = hydrate(desiredModel, attrs); | ||
} | ||
recordParentOf(child, parent); | ||
return child; | ||
}; | ||
mobx.intercept(value, change => { | ||
if (change.type == 'splice') { | ||
change.added = change.added.map(toModel); | ||
} | ||
return change; | ||
}); | ||
mobx.intercept(parent, property, change => { | ||
parent[property].replace(change.newValue.map(toModel)); | ||
return null; | ||
}); | ||
} | ||
function modelize(model, properties, options) { | ||
const decoratedSchema = getSchema(model); | ||
if (!properties) { | ||
mobx.makeObservable(model); | ||
if (!decoratedSchema) { | ||
return; // no decorators have created schema | ||
} | ||
return; | ||
} | ||
const schema = decoratedSchema || findOrCreateSchema(model); | ||
const mobxAnnotations = {}; | ||
Object.keys(properties).forEach(property => { | ||
if (!schema.recordProperty(property, properties[property])) { | ||
mobxAnnotations[property] = properties[property]; | ||
} | ||
}); | ||
schema.properties.forEach((options, key) => { | ||
if (!options.annotated) { | ||
mobxAnnotations[key] = mobx.observable; | ||
options.annotated = true; | ||
} | ||
}); | ||
mobx.makeObservable(model, mobxAnnotations, options); | ||
Object.keys(mobxAnnotations).forEach(property => { | ||
const options = schema.properties.get(property); | ||
if (options && options.type == 'model') { | ||
if (Array.isArray(model[property])) { | ||
configureHasMany(model, property, options.model); | ||
} else { | ||
configureHasOne(model, property, options.model); | ||
} | ||
} | ||
}); | ||
} | ||
exports.field = field; | ||
@@ -194,0 +248,0 @@ exports.getParentOf = getParentOf; |
@@ -1,2 +0,2 @@ | ||
import { observable, makeObservable } from 'mobx'; | ||
import { observable, intercept, makeObservable } from 'mobx'; | ||
@@ -34,13 +34,13 @@ const $schema = Symbol('modeled mobx schema'); | ||
function decorate(model, propertyKey, options) { | ||
observable(model, propertyKey); | ||
findOrCreateSchema(model).recordProperty(propertyKey, options); | ||
function decorate(model, property, options) { | ||
observable(model, property); | ||
findOrCreateSchema(model).recordProperty(property, options); | ||
} | ||
function field(target, propertyKey) { | ||
if (propertyKey) { | ||
decorate(target, propertyKey, field); | ||
function field(target, property) { | ||
if (property) { | ||
decorate(target, property, field); | ||
} else { | ||
return (target, propertyKey) => { | ||
decorate(target, propertyKey, field); | ||
return (target, property) => { | ||
decorate(target, property, field); | ||
}; | ||
@@ -50,4 +50,4 @@ } | ||
function model(model) { | ||
const decorator = (target, propertyKey) => { | ||
decorate(target, propertyKey, decorator); | ||
const decorator = (target, property) => { | ||
decorate(target, property, decorator); | ||
}; | ||
@@ -59,3 +59,3 @@ | ||
function getSchema(model) { | ||
return model[$schema] || false; | ||
return model[$schema]; | ||
} | ||
@@ -78,31 +78,2 @@ function findOrCreateSchema(model) { | ||
function modelize(model, properties, options) { | ||
const decoratedSchema = getSchema(model); | ||
if (!properties) { | ||
makeObservable(model); | ||
if (!decoratedSchema) { | ||
return; // no decorators have created schema | ||
} | ||
return; | ||
} | ||
const schema = decoratedSchema || findOrCreateSchema(model); | ||
const mobxAnnotations = {}; | ||
Object.keys(properties).forEach(property => { | ||
if (!schema.recordProperty(property, properties[property])) { | ||
mobxAnnotations[property] = properties[property]; | ||
} | ||
}); | ||
schema.properties.forEach((options, key) => { | ||
if (!options.annotated) { | ||
mobxAnnotations[key] = observable; | ||
options.annotated = true; | ||
} | ||
}); | ||
makeObservable(model, mobxAnnotations, options); | ||
} | ||
const Parents = new WeakMap(); | ||
@@ -192,3 +163,86 @@ function recordParentOf(child, parent) { | ||
function configureHasOne(parent, property, desiredModel) { | ||
intercept(parent, property, change => { | ||
if (change.newValue) { | ||
if (!(change.newValue instanceof desiredModel)) { | ||
change.newValue = hydrate(desiredModel, change.newValue); | ||
} | ||
recordParentOf(change.newValue, parent); | ||
} | ||
return change; | ||
}); | ||
} | ||
function configureHasMany(parent, property, desiredModel) { | ||
const value = observable.array(); | ||
parent[property] = value; | ||
const toModel = attrs => { | ||
let child; | ||
if (attrs instanceof desiredModel) { | ||
child = attrs; | ||
} else { | ||
child = hydrate(desiredModel, attrs); | ||
} | ||
recordParentOf(child, parent); | ||
return child; | ||
}; | ||
intercept(value, change => { | ||
if (change.type == 'splice') { | ||
change.added = change.added.map(toModel); | ||
} | ||
return change; | ||
}); | ||
intercept(parent, property, change => { | ||
parent[property].replace(change.newValue.map(toModel)); | ||
return null; | ||
}); | ||
} | ||
function modelize(model, properties, options) { | ||
const decoratedSchema = getSchema(model); | ||
if (!properties) { | ||
makeObservable(model); | ||
if (!decoratedSchema) { | ||
return; // no decorators have created schema | ||
} | ||
return; | ||
} | ||
const schema = decoratedSchema || findOrCreateSchema(model); | ||
const mobxAnnotations = {}; | ||
Object.keys(properties).forEach(property => { | ||
if (!schema.recordProperty(property, properties[property])) { | ||
mobxAnnotations[property] = properties[property]; | ||
} | ||
}); | ||
schema.properties.forEach((options, key) => { | ||
if (!options.annotated) { | ||
mobxAnnotations[key] = observable; | ||
options.annotated = true; | ||
} | ||
}); | ||
makeObservable(model, mobxAnnotations, options); | ||
Object.keys(mobxAnnotations).forEach(property => { | ||
const options = schema.properties.get(property); | ||
if (options && options.type == 'model') { | ||
if (Array.isArray(model[property])) { | ||
configureHasMany(model, property, options.model); | ||
} else { | ||
configureHasOne(model, property, options.model); | ||
} | ||
} | ||
}); | ||
} | ||
export { field, getParentOf, hydrate, model, modelize, serialize }; | ||
//# sourceMappingURL=modeled-mobx.modern.js.map |
@@ -1,2 +0,2 @@ | ||
import { observable, makeObservable } from 'mobx'; | ||
import { observable, intercept, makeObservable } from 'mobx'; | ||
@@ -34,13 +34,13 @@ const $schema = Symbol('modeled mobx schema'); | ||
function decorate(model, propertyKey, options) { | ||
observable(model, propertyKey); | ||
findOrCreateSchema(model).recordProperty(propertyKey, options); | ||
function decorate(model, property, options) { | ||
observable(model, property); | ||
findOrCreateSchema(model).recordProperty(property, options); | ||
} | ||
function field(target, propertyKey) { | ||
if (propertyKey) { | ||
decorate(target, propertyKey, field); | ||
function field(target, property) { | ||
if (property) { | ||
decorate(target, property, field); | ||
} else { | ||
return (target, propertyKey) => { | ||
decorate(target, propertyKey, field); | ||
return (target, property) => { | ||
decorate(target, property, field); | ||
}; | ||
@@ -50,4 +50,4 @@ } | ||
function model(model) { | ||
const decorator = (target, propertyKey) => { | ||
decorate(target, propertyKey, decorator); | ||
const decorator = (target, property) => { | ||
decorate(target, property, decorator); | ||
}; | ||
@@ -59,3 +59,3 @@ | ||
function getSchema(model) { | ||
return model[$schema] || false; | ||
return model[$schema]; | ||
} | ||
@@ -78,31 +78,2 @@ function findOrCreateSchema(model) { | ||
function modelize(model, properties, options) { | ||
const decoratedSchema = getSchema(model); | ||
if (!properties) { | ||
makeObservable(model); | ||
if (!decoratedSchema) { | ||
return; // no decorators have created schema | ||
} | ||
return; | ||
} | ||
const schema = decoratedSchema || findOrCreateSchema(model); | ||
const mobxAnnotations = {}; | ||
Object.keys(properties).forEach(property => { | ||
if (!schema.recordProperty(property, properties[property])) { | ||
mobxAnnotations[property] = properties[property]; | ||
} | ||
}); | ||
schema.properties.forEach((options, key) => { | ||
if (!options.annotated) { | ||
mobxAnnotations[key] = observable; | ||
options.annotated = true; | ||
} | ||
}); | ||
makeObservable(model, mobxAnnotations, options); | ||
} | ||
const Parents = new WeakMap(); | ||
@@ -192,3 +163,86 @@ function recordParentOf(child, parent) { | ||
function configureHasOne(parent, property, desiredModel) { | ||
intercept(parent, property, change => { | ||
if (change.newValue) { | ||
if (!(change.newValue instanceof desiredModel)) { | ||
change.newValue = hydrate(desiredModel, change.newValue); | ||
} | ||
recordParentOf(change.newValue, parent); | ||
} | ||
return change; | ||
}); | ||
} | ||
function configureHasMany(parent, property, desiredModel) { | ||
const value = observable.array(); | ||
parent[property] = value; | ||
const toModel = attrs => { | ||
let child; | ||
if (attrs instanceof desiredModel) { | ||
child = attrs; | ||
} else { | ||
child = hydrate(desiredModel, attrs); | ||
} | ||
recordParentOf(child, parent); | ||
return child; | ||
}; | ||
intercept(value, change => { | ||
if (change.type == 'splice') { | ||
change.added = change.added.map(toModel); | ||
} | ||
return change; | ||
}); | ||
intercept(parent, property, change => { | ||
parent[property].replace(change.newValue.map(toModel)); | ||
return null; | ||
}); | ||
} | ||
function modelize(model, properties, options) { | ||
const decoratedSchema = getSchema(model); | ||
if (!properties) { | ||
makeObservable(model); | ||
if (!decoratedSchema) { | ||
return; // no decorators have created schema | ||
} | ||
return; | ||
} | ||
const schema = decoratedSchema || findOrCreateSchema(model); | ||
const mobxAnnotations = {}; | ||
Object.keys(properties).forEach(property => { | ||
if (!schema.recordProperty(property, properties[property])) { | ||
mobxAnnotations[property] = properties[property]; | ||
} | ||
}); | ||
schema.properties.forEach((options, key) => { | ||
if (!options.annotated) { | ||
mobxAnnotations[key] = observable; | ||
options.annotated = true; | ||
} | ||
}); | ||
makeObservable(model, mobxAnnotations, options); | ||
Object.keys(mobxAnnotations).forEach(property => { | ||
const options = schema.properties.get(property); | ||
if (options && options.type == 'model') { | ||
if (Array.isArray(model[property])) { | ||
configureHasMany(model, property, options.model); | ||
} else { | ||
configureHasOne(model, property, options.model); | ||
} | ||
} | ||
}); | ||
} | ||
export { field, getParentOf, hydrate, model, modelize, serialize }; | ||
//# sourceMappingURL=modeled-mobx.module.js.map |
@@ -37,13 +37,13 @@ (function (global, factory) { | ||
function decorate(model, propertyKey, options) { | ||
mobx.observable(model, propertyKey); | ||
findOrCreateSchema(model).recordProperty(propertyKey, options); | ||
function decorate(model, property, options) { | ||
mobx.observable(model, property); | ||
findOrCreateSchema(model).recordProperty(property, options); | ||
} | ||
function field(target, propertyKey) { | ||
if (propertyKey) { | ||
decorate(target, propertyKey, field); | ||
function field(target, property) { | ||
if (property) { | ||
decorate(target, property, field); | ||
} else { | ||
return (target, propertyKey) => { | ||
decorate(target, propertyKey, field); | ||
return (target, property) => { | ||
decorate(target, property, field); | ||
}; | ||
@@ -53,4 +53,4 @@ } | ||
function model(model) { | ||
const decorator = (target, propertyKey) => { | ||
decorate(target, propertyKey, decorator); | ||
const decorator = (target, property) => { | ||
decorate(target, property, decorator); | ||
}; | ||
@@ -62,3 +62,3 @@ | ||
function getSchema(model) { | ||
return model[$schema] || false; | ||
return model[$schema]; | ||
} | ||
@@ -81,31 +81,2 @@ function findOrCreateSchema(model) { | ||
function modelize(model, properties, options) { | ||
const decoratedSchema = getSchema(model); | ||
if (!properties) { | ||
mobx.makeObservable(model); | ||
if (!decoratedSchema) { | ||
return; // no decorators have created schema | ||
} | ||
return; | ||
} | ||
const schema = decoratedSchema || findOrCreateSchema(model); | ||
const mobxAnnotations = {}; | ||
Object.keys(properties).forEach(property => { | ||
if (!schema.recordProperty(property, properties[property])) { | ||
mobxAnnotations[property] = properties[property]; | ||
} | ||
}); | ||
schema.properties.forEach((options, key) => { | ||
if (!options.annotated) { | ||
mobxAnnotations[key] = mobx.observable; | ||
options.annotated = true; | ||
} | ||
}); | ||
mobx.makeObservable(model, mobxAnnotations, options); | ||
} | ||
const Parents = new WeakMap(); | ||
@@ -195,2 +166,85 @@ function recordParentOf(child, parent) { | ||
function configureHasOne(parent, property, desiredModel) { | ||
mobx.intercept(parent, property, change => { | ||
if (change.newValue) { | ||
if (!(change.newValue instanceof desiredModel)) { | ||
change.newValue = hydrate(desiredModel, change.newValue); | ||
} | ||
recordParentOf(change.newValue, parent); | ||
} | ||
return change; | ||
}); | ||
} | ||
function configureHasMany(parent, property, desiredModel) { | ||
const value = mobx.observable.array(); | ||
parent[property] = value; | ||
const toModel = attrs => { | ||
let child; | ||
if (attrs instanceof desiredModel) { | ||
child = attrs; | ||
} else { | ||
child = hydrate(desiredModel, attrs); | ||
} | ||
recordParentOf(child, parent); | ||
return child; | ||
}; | ||
mobx.intercept(value, change => { | ||
if (change.type == 'splice') { | ||
change.added = change.added.map(toModel); | ||
} | ||
return change; | ||
}); | ||
mobx.intercept(parent, property, change => { | ||
parent[property].replace(change.newValue.map(toModel)); | ||
return null; | ||
}); | ||
} | ||
function modelize(model, properties, options) { | ||
const decoratedSchema = getSchema(model); | ||
if (!properties) { | ||
mobx.makeObservable(model); | ||
if (!decoratedSchema) { | ||
return; // no decorators have created schema | ||
} | ||
return; | ||
} | ||
const schema = decoratedSchema || findOrCreateSchema(model); | ||
const mobxAnnotations = {}; | ||
Object.keys(properties).forEach(property => { | ||
if (!schema.recordProperty(property, properties[property])) { | ||
mobxAnnotations[property] = properties[property]; | ||
} | ||
}); | ||
schema.properties.forEach((options, key) => { | ||
if (!options.annotated) { | ||
mobxAnnotations[key] = mobx.observable; | ||
options.annotated = true; | ||
} | ||
}); | ||
mobx.makeObservable(model, mobxAnnotations, options); | ||
Object.keys(mobxAnnotations).forEach(property => { | ||
const options = schema.properties.get(property); | ||
if (options && options.type == 'model') { | ||
if (Array.isArray(model[property])) { | ||
configureHasMany(model, property, options.model); | ||
} else { | ||
configureHasOne(model, property, options.model); | ||
} | ||
} | ||
}); | ||
} | ||
exports.field = field; | ||
@@ -197,0 +251,0 @@ exports.getParentOf = getParentOf; |
@@ -6,5 +6,5 @@ import { Model, ModelSchema as Schema, PropertyOptions, ModelOption } from './types'; | ||
} | ||
export declare function field<T extends object>(target: T, propertyKey?: string): any; | ||
export declare function field<T extends object>(target: T, property?: string): any; | ||
export declare function model(model: Model): { | ||
<T extends object>(target: T, propertyKey: string): void; | ||
<T extends object>(target: T, property: string): void; | ||
model: Model; | ||
@@ -11,0 +11,0 @@ }; |
{ | ||
"name": "modeled-mobx", | ||
"version": "0.3.3", | ||
"version": "0.3.5", | ||
"description": "MobX powered classes with type-coercion and serialization/hydration", | ||
@@ -5,0 +5,0 @@ "author": "nathanstitt", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94990
905