Skip to content

Commit

Permalink
A fix for dates in state
Browse files Browse the repository at this point in the history
+ remove yarn log .....
  • Loading branch information
ForsakenHarmony committed Mar 19, 2018
1 parent 29fddff commit 9ab0125
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -190,6 +190,9 @@ function cProxy<S>(
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' &&
Expand Down Expand Up @@ -227,7 +230,10 @@ function subscribe(emitter: Emitter, evt: string, fn: EventHandler) {
return () => emitter.off(evt, fn);
}

function model<S>(name: string, { initial, actions, views }: ModelArgs<S>) {
function model<S>(
name: string,
{ initial, actions, views }: ModelArgs<S>
): (obj?: object | undefined) => Model<S> {
if (typeof initial !== 'function') {
throw new Error(
'You have to supply a function that returns the initial state'
Expand Down Expand Up @@ -368,7 +374,7 @@ function model<S>(name: string, { initial, actions, views }: ModelArgs<S>) {
}

modelMap.set(name, instantiate);
return (obj?: object) => instantiate(obj)[vpc];
return obj => instantiate(obj)[vpc];
}

export default model;
Expand Down
36 changes: 36 additions & 0 deletions test/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => ({
Expand Down
7 changes: 4 additions & 3 deletions yarn-error.log
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9ab0125

Please sign in to comment.