Beispiel #1
0
 /**
  * Apply an event to the entity.
  *
  * Take an event as expected by this library and apply it to the entity. If it
  * is a snapshot event, reset the state to be an empty object.
  *
  * @param {Object} event The event being applied.
  * @param {Entity} target The entity being acted on.
  */
 static apply(event, target) {
   const entity = target;
   let before = entity[es].state;
   if (event.get('name') === 'snapshot') {
     before = Immutable.fromJS({});
   }
   entity[es].version = event.get('version');
   entity[es].state = patch(before, event.get('changeset'));
 }
Beispiel #2
0
 return store => next => action => {
   if (VALUE(action.type) && toState(store.getState()).get('snapshot') && toState(store.getState()).getIn(['firebase', 'connected'])) {
     const server    = fromJS(action.payload || {});
     const current   = toState(store.getState()).get('entities');
     const snapshot  = toState(store.getState()).get('snapshot');
     const diff1 = diff(snapshot, current);
     const diff2 = diff(snapshot, server);
     let final_diff = diff2;
     diff1.forEach(val => {
       const path  = val.get('path');
       let ok = true;
       diff2.forEach(ser => {
         const path_ser = ser.get('path');
         if (path.startsWith(path_ser) || path_ser.startsWith(path)) {
           ok = false;
           return false;
         }
       });
       if (ok) {
         final_diff = final_diff.insert(0, val);
       }
     });
     const final_state = patch(snapshot, final_diff);
     if (diff(server, final_state).count() > 0) {
       toRef(toState(store.getState())).set(final_state.toJS());
       return;
     }
   } else if (VALUE(action.type) && hasJustSignedUp) {
     const firebase  = toRef(toState(store.getState()));
     hasJustSignedUp = false;
     firebase.set(toState(store.getState()).get('entities').toJS());
     return;
   } else if (SIGNED_UP(action.type)) {
     hasJustSignedUp = true;
   }
   const result = next(action);
   const firebase = toRef(toState(store.getState()));
   check(store, firebase);
   return result;
 };
Beispiel #3
0
ipc.on(messages.APP_STATE_CHANGE, (e, action) => {
  appStoreRenderer.state = action.stateDiff
    ? appStoreRenderer.state = patch(appStoreRenderer.state, Immutable.fromJS(action.stateDiff))
    : appStoreRenderer.state = Immutable.fromJS(action.state)
})
export function applyInverseChangeset(state, changeset) {
  return Patch(state, changeset.get('inverseDiff'))
}
export function applyChangeset(state, changeset) {
  return Patch(state, changeset.get('diff'))
}
		it('normalizr readme example', () =>
		{

			const shape = Immutable.fromJS({
				result  : true,
				root: {
					entities: {
						articles: {
							[SHAPE_FILTER_KEY]  : true,
							'editPath'  : '/+root',
							'selectKeys': ['result', [0, 2]],
							'filterKeys': {
								title: true,
							}
						},
						users   : {
							[SHAPE_FILTER_KEY]  : true,
							'editPath'  : '/+root',
							'selectKeys': ['entities', 'articles', ['result', [0, 2]], 'author'],
						}
					}
				}
			});


			const origState = Immutable.fromJS({
				result  : ['1', '2', '3'],
				entities: {
					articles: {
						1: {
							id    : '1',
							title : 'Some Article',
							author: '1'
						},
						2: {
							id    : '2',
							title : 'Other Article',
							author: '1'
						},
						3: {
							id    : '3',
							title : 'Yet Another Article',
							author: '1'
						},
					},
					users   : {
						1: {
							id  : '1',
							name: 'Dan'
						},
						2: {
							id  : '2',
							name: 'Dan'
						}
					}
				}
			});
			
			
			

			const newState = origState.set('visibilityFilter', 'SHOW_COMPLETED')
			                          .update('result', (result) =>
			                          {
				                          result = result.shift();
				                          return result;
			                          })
			                          .setIn(['entities', 'articles', '2', 'author'], '2')
			                          .setIn(['entities', 'articles', '3', 'author'], '3')
			                          .removeIn(['entities', 'articles', '1'])
			                          .setIn(['entities', 'articles', '2', 'title'], 'Changed Title');

			let filteredOrigState = filterMapToShape(origState, shape);
			let filteredNewState = filterMapToShape(newState, shape);
			;

			const diff         = diffWithShape(origState, newState, shape);
			// 			const patchedState = patchFromDiff(origState, diff);
			const patchedState = patch(filteredOrigState, diff);
			
			expect(filteredNewState.toJS()).toEqual(patchedState.toJS())
			
		})