Git Product home page Git Product logo

mutable's Introduction

Mutable

npm version

Mutable state containers in javascript with dirty checking and more (WIP)

What Mutable does

Mutable is a mobx-compatible class system library. Mutable offers a unique runtime schema engine that enforces unidirectional data flow, and formalizes the structure of props and state. Mutable also supports data defaults, and non-nullable types.

Using mutable

Add Mutable to your project by installing it with npm:

npm install mutable --save

Simple code example:

import * as mutable from 'mutable';
import * as mobx from 'mutable';

// define a mutable class by providing a name and a spec (class schema)
const Dude = mutable.define('Dude', {
    spec: ()=>({
        name: Mutable.String.withDefault('Leon'),
        age: Mutable.Number.withDefault(110),
        address: Mutable.String.withDefault('no address')
    })
});
 
// Mutable types accept custom data according to their spec as the first argument of their constructor
const dude = new Dude({name:'Ido'});

mobx.autorun(function () {
    console.log(dude.name + ' ' + dude.age);
});
// prints: Leon 110
dude.name = 'Mike';
// prints: Mike 110

Integrating mutable into React components is up to the user.

how to build and test locally from source

Clone this project locally. Then, at the root folder of the project, run:

npm install
npm test

how to run local continous test feedback

At the root folder of the project, run:

npm start

Then, open your browser at http://localhost:8080/webtest.bundle and see any changes you make in tests or code reflected in the browser

Versioning

Currently Mutable is in alpha mode. As such, it does not respect semver.

License

We use a custom license, see LICENSE.md

Similar Projects

These are examples of the kinds of libraries we would like to model ourselves after.

  • NestedTypes : High-performance model framework, which can be used as drop-in backbonejs replacement.
  • mobx : Simple, scalable state management
  • alt : A library that facilitates the managing of state within your JavaScript applications. It is modeled after flux.
  • immutable.js : Immutable persistent data collections for Javascript which increase efficiency and simplicity
  • cls.js : Easy, dynamic (kind of mixin) javascript classes
  • observable-value: Object representation of mutable value

mutable's People

Contributors

amir-arad avatar avivahl avatar barak007 avatar cijoe1 avatar electricmonk avatar gadiguy avatar idoros avatar jomarton avatar leonfedotov avatar nadavov avatar nadavwix avatar nirbenita avatar qballer avatar snyk-bot avatar talh78 avatar tobich avatar tomrav avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mutable's Issues

TBD defining a field by parent type

should we support defining a field of type BaseType ?

currently it results in

Uncaught Error: Field "a" in the data class "State" is not a descendant of Type.

Reference types

create a mutable instance that instead of wrapping data it refers to a function that computes that data.

the use case is for a user to wrap a mobx @computed function that returns complex data, so that different parts of the result can be destructured and tracked separately.

Also, it can be used as a reference an existing observable data without cloning it.

  • support user-defined types
  • support List
  • support Map
  • add helpful error messages

Reuse type classes with the same definition

This optimization seems necessary for generic types, as a runtime reference creates a new type instance every time.

Most glaring example:

const list = Mutable.List.of(core3.String)();

So every time this code is called (imagine a loop... ๐Ÿ˜ข ), you'll create a new type definition.

perhaps only for derived types (of, nullable, withDefault etc.)

fix and test toJSON()

toJSON() function should return a subset of toJS() only without fields that contain unserializable data (NaN, functions...)

Edit New issue report error context where missing

any place there is a call to MAILBOX.something and an errorContext is defined in the scope it should be part of the message (incl. tests)

found example: List constructor

  if (!errorContext) {
    errorContext = _List.createErrorContext('List constructor error', 'error', options);
  }
  const report = _List.reportDefinitionErrors(options);
  if (report) {
    MAILBOX.error('List constructor: ' + report.message); // <--- no error context!
  }

VoidType needed

Use-cases:

  • generic parameter (e.g. EventHandler<void>)
  • method parameters schema

how does returning void differ from returning undefined?
Or is this for code-sense-foo?

void is a special type that only matches undefined, so when the type signature is void, you expect the runtime value to be undefined.

add "any" type

need to add a dynamic type that can accept any type and be assigned to any type

Efficient identity support

We seem to lack efficient way to say

if(Mutable.isMutableInstance(instance)) {}
if(Mutable.isMutableType(Type)) {}

there are lots of places which try to do it in various ways

redesign revisions API

  • equals() function for data-driven dirtyness (with pure js value or Mutable value as argument)
  • hasChangedSince() function for version-driven dirtyness (just like we do now)
  • advance revision automatically on every change to Mutable property (TBD)

Mutable.Map type validation

Seem like there is no deep validation for Mutable.Map values. That is mean that if value is of some complex type then you could use any object for it. I provide unit tests for this issue. You can find them in maksym/provide-unit-tests-for-map-type-checking branch

assigning a complex value to a union field fails

when assigning a complex value (user-defined object) to a union field that should accept this value, the assignment fails.
the error seems to be in $assignField at object.ts file:

const typedField = isAssignableFrom(MuBase, fieldDef);
// for typed field, validate the type of the value. for untyped field (primitive), just validate the data itself
if ((typedField && fieldDef.validateType(newValue)) || (!typedField && fieldDef.validate(newValue))) {

typedField is false although the union contains a typed type
there are two separate bugs here:

  1. for now union should return true for isAssignableFrom(MuBase, fieldDef)
  2. the check should depend on the value (or completely simplify the validation mechanism)

reported by @talh78

List.map return array and not typed List

I have the following code

function changeCollection(rawData:List<RawData>): List<Data> {
    let filtered:List<RawData> = rawData.filter((value)=>  true/** some condition */)
    console.log('Wo: ',  filtered) // looks good
    let mapped:List<Template> =  filtered.map(rawToData) // returns [] instead of List<Data>
    return mapped;
}
function rawToData(val:RawData):Data{/*Impel*/}

Question: Library Name Inquiry

Hello,

Is this library still active?

My name is Shaun Wallace, I am a PhD in HCI at Brown University.

We are finishing up a new JS library and were hoping to use the "mutable" name for the library. Since Wix's library has not received updates in 3 years, nor any new comments would we be able to use the name "mutable" for our repository? If not no worries. :)

  • our new library is not similar to or takes any code from Wix's mutable library.

Frequent error: applying lodash on Mutable

For example:

// this.props.menuItems: MenuItem[];
// i.e. it is a typorama instance and lodash won't work
var selectedItem = _.find(this.props.menuItems, { id: this.props.selectedItemId });

One does this error very easily and it is not always immediately clear what's wrong. :-(

spoon lodash to throw error when it gets typorama object (just in development?)

rewrite Readme

The project's API has changed since the move to mobx, and also the project's concept got more precise.
We need a new readme file and some API documentations.

Review validation / error mechanism

The whole validation mechanism is starting to be messy. We need:

  • unified API for validate* functions. (There are true/false responses, null or value, Error or value and maybe more).
  • ability to easily chain/extend atomic validations
  • channel for passing errors (which, by default, would throw() 'em, but could be wired to some logger instead)
  • mechanism which makes sure that any validation failure is consumed (we have situations, when failed validation just passes silently, without any message)
  • error/warning/message builder/resource manager to have a single source for all messages

Proposal by @tobich

Unified validation API

Validating function validate(value) returns one of the following

  • the original value (or its replacement, such as wrapped value) if the validation went fine
  • instance of Error object (or its extension) if there was error
  • VALIDATION_SKIPPED object value if the validation couldn't be performed (useful for validation chaining)

Calling validating functions

// module validationUtils.js
export function validate(value, context, validators:Array<Function>, callback:Function) {}

Runs value through the chain of validators. If a validator returns Error, the whole function passes the error to the reporter pipe and returns the error as return value. If VALIDATION_SKIPPED is returned by a validator, it is ignored and next one is called. If all validators have passed or been ignored, the optional callback is called with the value as parameter and its return value is returned.

Error reporting channel

// module errors.js
export function report(err:Error) {}
export function setHandler(handler:Function)

enums not properly handled in stack

@arnonkehat says:
enums not properly handled in stack
I tried use an enum in a dropdown, and could not use its values as the dropdown items. talk to me if you want to see what I was trying to do.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.