Skip to content

Instantly share code, notes, and snippets.

@jjrv
Last active October 24, 2017 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjrv/3e6048d8df1a5b1ac5dbfab083a889e6 to your computer and use it in GitHub Desktop.
Save jjrv/3e6048d8df1a5b1ac5dbfab083a889e6 to your computer and use it in GitHub Desktop.
import { IModelType } from 'mobx-state-tree';
/** Fake complete, generic implementation of IModelType. */
export declare const AbstractModel: new() => IModelType<any, any>;
/** Fake complete, specialized implementation of IModelType.
* This allows interfaces to include all of its static and dynamic members. */
export declare abstract class ModelClass<S, T> extends AbstractModel implements IModelType<S, T> {}
/** Interface with all IModelType static and dynamic members,
* callable to construct a specialized instance. */
export interface ModelInterface<S, T> extends IModelType<S, T> {
new(): ModelClass<S, T> & T
}
/** Force TypeScript to accept an MST model as a superclass.
* @param model Model (MST tree node)
*/
export function shim<S, T>(model: IModelType<S, T>, parent?: any): ModelInterface<S, T> {
if(parent && parent.prototype) (model as any).prototype = parent.prototype;
return(model as any);
}
/** Decorator for actions. By default the mst function treats methods as views. */
export function action(target: { [key: string]: any }, key: string) {
(target.$actions || (target.$actions = {}))[key] = target[key];
target[key] = null;
}
/** Add methods from an ES6 class into an MST model.
* @param code Class with methods to add as views (the default)
* and actions (if decorated).
* @param data MST model with properties. */
export function mst<S, T, U>(code: new() => U, data: IModelType<S, T>): IModelType<S, U> {
function bindMethods(self: any, defs: { [name: string]: any }) {
const result: { [name: string]: any } = {};
for(let name of Object.keys(defs)) {
if(name != 'constructor' && name != '$actions') {
const method = defs[name];
result[name] = function() { return(method.apply(self, arguments)); }
}
}
return(result);
}
const model = data.views(
(self) => bindMethods(self, code.prototype)
).actions(
(self) => bindMethods(self, code.prototype.$actions || [])
) as any;
model.prototype = code.prototype;
return(model);
}
The MIT License (MIT)
Copyright (c) 2017 BusFaster Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment