From 9ab012545eb7a5d6cf191248b6d118b8bc299a3c Mon Sep 17 00:00:00 2001 From: Leah Date: Mon, 19 Mar 2018 18:08:44 +0100 Subject: [PATCH] A fix for dates in state + remove yarn log ..... --- src/index.ts | 20 +++++++++++++------- test/model.test.ts | 36 ++++++++++++++++++++++++++++++++++++ yarn-error.log | 7 ++++--- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index b621001..f622be1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,15 +2,15 @@ // import mitt from 'mitt'; export type EventHandler = (event?: any) => void; -export type WildCardEventHandler = (type: string, event?: any) => void; -export type Handler = EventHandler | WildCardEventHandler; +type WildCardEventHandler = (type: string, event?: any) => void; +type Handler = EventHandler | WildCardEventHandler; // A map of event types and their corresponding event handlers. -export type EventHandlerMap = { +type EventHandlerMap = { [type: string]: Handler[]; }; -export interface Emitter { +interface Emitter { on(type: string, handler: Handler): void; off(type: string, handler: Handler): void; emit(type: string, evt: any): void; @@ -20,7 +20,7 @@ export interface Emitter { * @name mitt * @returns {Emitter} */ -export function mitt(all: EventHandlerMap = Object.create(null)): Emitter { +function mitt(all: EventHandlerMap = Object.create(null)): Emitter { return { /** * Register an event handler for the given type. @@ -190,6 +190,9 @@ function cProxy( if (prop === symbol) return true; if (typeof prop === 'symbol') return target[prop]; const res = target[prop]; + // check if there are other built in constructors that need this + if (target instanceof Date && typeof res === 'function') + return res.bind(target); return ( (res != null && typeof res === 'object' && @@ -227,7 +230,10 @@ function subscribe(emitter: Emitter, evt: string, fn: EventHandler) { return () => emitter.off(evt, fn); } -function model(name: string, { initial, actions, views }: ModelArgs) { +function model( + name: string, + { initial, actions, views }: ModelArgs +): (obj?: object | undefined) => Model { if (typeof initial !== 'function') { throw new Error( 'You have to supply a function that returns the initial state' @@ -368,7 +374,7 @@ function model(name: string, { initial, actions, views }: ModelArgs) { } modelMap.set(name, instantiate); - return (obj?: object) => instantiate(obj)[vpc]; + return obj => instantiate(obj)[vpc]; } export default model; diff --git a/test/model.test.ts b/test/model.test.ts index de5ea24..9b4f0b0 100644 --- a/test/model.test.ts +++ b/test/model.test.ts @@ -166,6 +166,42 @@ describe('model()', function() { expect(sub).toBeCalled(); }); + it('should update on array methods', function() { + const Model = model('Model', { + initial: () => ({ + arr: [] as any[], + }), + actions: self => ({ + pushToArr(thing: any) { + self.arr.push(thing); + }, + }), + views: self => ({ + firstElem: () => self.arr[0], + }), + }); + + const instance = Model(); + + const sub = jest.fn(); + instance.onPatch(sub); + instance.pushToArr(2); + expect(sub).toBeCalled(); + expect(instance.firstElem).toBe(2); + }); + + it('should handle dates', function() { + const Model = model('Model', { + initial: () => ({ + date: new Date(), + }), + }); + + const instance = Model(); + instance.getSnapshot(); + instance.date.getDate(); + }); + it('should apply snapshots', function() { const Person = model('Person', { initial: () => ({ diff --git a/yarn-error.log b/yarn-error.log index bc2c6bd..2b606d2 100644 --- a/yarn-error.log +++ b/yarn-error.log @@ -16,18 +16,19 @@ Platform: npm manifest: { "name": "parket", - "version": "0.3.0", + "version": "0.4.0", "description": "A small reactiveâ„¢ library to manage application state, heavily inspired by mobx-state-tree", "source": "src/index.ts", "typings": "dist/index.d.ts", - "module": "dist/parket.es.js", + "module": "dist/es/index.js", "main": "dist/parket.js", "umd:main": "dist/parket.umd.js", "scripts": { "test": "npm run build && npm run jest", "jest": "jest", "build": "npm-run-all --silent -p build:*", - "build:main": "microbundle", + "build:ts": "tsc", + "build:main": "microbundle -f umd,cjs", "build:integrations": "microbundle src/integration/*.js -o x.js -f cjs", "build:devtools": "microbundle src/devtools.js -o devtools.js -f cjs", "prepare": "npm t",