-
Notifications
You must be signed in to change notification settings - Fork 0
Added unsubscribing of listeners on component unmount #7
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| export interface FieldListenerRepository { | ||
| subscribe(field: string, callback: (value: any) => void): Listener; | ||
| unsubscribe(toBeUnsubscribedListener: Listener); | ||
| trigger(field: string, value: any); | ||
| } | ||
|
|
||
| export interface Listener { | ||
| id: number; | ||
| field: string; | ||
| callback: (value: any) => void; | ||
| } | ||
|
|
||
| export class FieldListenerRepositoryImpl implements FieldListenerRepository { | ||
|
|
||
| private listeners: { [field: string]: Listener[] } = {}; | ||
|
|
||
| private index = 1; | ||
|
|
||
| public subscribe(field: string, callback: (value: any) => void): Listener { | ||
| if (typeof this.listeners[field] === 'undefined') { | ||
| this.listeners[field] = []; | ||
| } | ||
| const listener = { | ||
| id: this.index++, | ||
| field: field, | ||
| callback: callback | ||
| }; | ||
| this.listeners[field].push(listener); | ||
|
|
||
| return listener; | ||
| } | ||
|
|
||
| public unsubscribe(toBeUnsubscribedListener: Listener) { | ||
| if (typeof this.listeners[toBeUnsubscribedListener.field] === 'undefined') { | ||
| throw new Error('Trying to unregister a listener for a field that is not registered'); | ||
| } | ||
| this.listeners[toBeUnsubscribedListener.field] = this | ||
| .listeners[toBeUnsubscribedListener.field] | ||
| .filter(listener => listener.id !== toBeUnsubscribedListener.id); | ||
| } | ||
|
|
||
| public trigger(field: string, value: any) { | ||
| if (typeof this.listeners[field] !== 'undefined') { | ||
| this.listeners[field].forEach(listener => listener.callback(value)); | ||
| } | ||
| } | ||
|
|
||
| public getFieldListenersForFieldName(field: string): Array<(value: any) => void> | undefined { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is a |
||
| const listeners = this.listeners[field]; | ||
| if (typeof listeners !== 'undefined') { | ||
| return listeners.map(fieldListener => fieldListener.callback); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| import {FieldListenerRepository, FieldListenerRepositoryImpl, Listener} from './field_listener_repository'; | ||
| export class Form { | ||
|
|
||
| private values: { [key: string]: any } = {}; | ||
|
|
||
| private validationErrors: { [fieldName: string]: any } = {}; | ||
|
|
||
| private fieldListeners: { [fieldName: string]: Array<(value: any) => void> } = {}; | ||
| private _fieldListenerRepository: FieldListenerRepository; | ||
|
|
||
| constructor(fieldListenerRepository: FieldListenerRepository | null = null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why a |
||
| this._fieldListenerRepository = fieldListenerRepository !== null | ||
| ? fieldListenerRepository | ||
| : new FieldListenerRepositoryImpl(); | ||
| } | ||
|
|
||
| public getFieldValue(fieldName: string): any { | ||
| return this.values[fieldName] || ''; | ||
|
|
@@ -17,44 +23,37 @@ export class Form { | |
| delete this.validationErrors[fieldName]; | ||
| }); | ||
|
|
||
| this.triggerMultipleFieldListeners(fieldNames); | ||
| this.triggerMany(fieldNames); | ||
| } | ||
|
|
||
| public setFieldValue(fieldName: string, value: any) { | ||
| this.values[fieldName] = value; | ||
| delete this.validationErrors[fieldName]; | ||
|
|
||
| this.triggerFieldListeners(fieldName); | ||
| this._fieldListenerRepository.trigger(fieldName, value); | ||
| } | ||
|
|
||
| public setValidationErrors(errors: { [fieldName: string]: string }) { | ||
| this.validationErrors = errors; | ||
|
|
||
| const fieldNames = Object.keys(this.validationErrors); | ||
| this.triggerMultipleFieldListeners(fieldNames); | ||
| this.triggerMany(fieldNames); | ||
| } | ||
|
|
||
| public getValidationError(fieldName: string): string | undefined { | ||
| return this.validationErrors[fieldName]; | ||
| } | ||
|
|
||
| public listenForFieldChange(fieldName: string, callback: (value: any) => void) { | ||
| if (typeof this.fieldListeners[fieldName] === 'undefined') { | ||
| this.fieldListeners[fieldName] = []; | ||
| } | ||
| this.fieldListeners[fieldName].push(callback); | ||
| public subscribe(fieldName: string, callback: (value: any) => void): Listener { | ||
| return this._fieldListenerRepository.subscribe(fieldName, callback); | ||
| } | ||
|
|
||
| private triggerMultipleFieldListeners(fieldNames: string[]) { | ||
| fieldNames.forEach((fieldName) => { | ||
| this.triggerFieldListeners(fieldName); | ||
| }); | ||
| public unsubscribe(listener: Listener) { | ||
| this._fieldListenerRepository.unsubscribe(listener); | ||
| } | ||
|
|
||
| private triggerFieldListeners(fieldName: string) { | ||
| if (typeof this.fieldListeners[fieldName] !== 'undefined') { | ||
| this.fieldListeners[fieldName].forEach((callback) => callback(this.getFieldValue(fieldName))); | ||
| } | ||
| private triggerMany(fieldNames: string[]) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe wanna move this to the repo? Seems like a method the repo would want ownership of, if in the future it's behavior gotta change. (like we had before) |
||
| fieldNames.forEach(fieldName => this._fieldListenerRepository.trigger(fieldName, this.getFieldValue(fieldName))); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont wanna use a
Symbol?