typescript(monadic): refactor: moved the component stuff in a new file
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
		
					parent
					
						
							
								4ef6660fce
							
						
					
				
			
			
				commit
				
					
						db5b68592c
					
				
			
		
					 2 changed files with 26 additions and 25 deletions
				
			
		typescript/monadic/src
							
								
								
									
										25
									
								
								typescript/monadic/src/component.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								typescript/monadic/src/component.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,25 @@ | |||
| import { IterableEmitter } from './iterableEmitter'; | ||||
| 
 | ||||
| export type Component<T, S, A> = { | ||||
|   render: (state: S, dispatch: (a: A) => () => void) => T; | ||||
|   handleActions: (action: A, state: S) => S; | ||||
| }; | ||||
| 
 | ||||
| export async function* runComponent<T, S, A>( | ||||
|   component: Component<T, S, A>, | ||||
|   initialState: S | ||||
| ): AsyncGenerator<T> { | ||||
|   const emitter = new IterableEmitter(initialState); | ||||
| 
 | ||||
|   const dispatch = (state: S) => (action: A) => { | ||||
|     const newState = component.handleActions(action, state); | ||||
| 
 | ||||
|     return () => { | ||||
|       emitter.next(newState); | ||||
|     }; | ||||
|   }; | ||||
| 
 | ||||
|   for await (const state of emitter) { | ||||
|     yield component.render(state, dispatch(state)); | ||||
|   } | ||||
| } | ||||
|  | @ -1,4 +1,4 @@ | |||
| import { IterableEmitter } from './iterableEmitter'; | ||||
| import { Component, runComponent } from './component'; | ||||
| 
 | ||||
| export type EnvConfig<T, S, A> = { | ||||
|   render: (template: T, parent: HTMLElement) => void; | ||||
|  | @ -7,30 +7,6 @@ export type EnvConfig<T, S, A> = { | |||
|   initialState: S; | ||||
| }; | ||||
| 
 | ||||
| export type Component<T, S, A> = { | ||||
|   render: (state: S, dispatch: (a: A) => () => void) => T; | ||||
|   handleActions: (action: A, state: S) => S; | ||||
| }; | ||||
| 
 | ||||
| async function* runComponent<T, S, A>( | ||||
|   component: Component<T, S, A>, | ||||
|   initialState: S | ||||
| ): AsyncGenerator<T> { | ||||
|   const emitter = new IterableEmitter(initialState); | ||||
| 
 | ||||
|   const dispatch = (state: S) => (action: A) => { | ||||
|     const newState = component.handleActions(action, state); | ||||
| 
 | ||||
|     return () => { | ||||
|       emitter.next(newState); | ||||
|     }; | ||||
|   }; | ||||
| 
 | ||||
|   for await (const state of emitter) { | ||||
|     yield component.render(state, dispatch(state)); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| export const runUi = async <T, S, A>( | ||||
|   config: EnvConfig<T, S, A> | ||||
| ): Promise<void> => { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Matei Adriel
				Matei Adriel