/
observable.js
236 lines (222 loc) · 8.91 KB
/
observable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import {emptyListMap, removeByKey} from "../listMap/listMap.js";
import {push, forEach} from "../stack/stack.js";
import {pair, showPair, snd, fst} from "../lambda-calculus-library/lambda-calculus.js";
import {generateRandomKey} from "./observableExamples/observableUtilities.js";
export {Observable, addListener, setValue, getValue, removeListenerByKey, removeListener, logListenersToConsole, listenerLogToConsole, newListener, setListenerKey, getListenerKey, newListenerWithCustomKey, listenerNewValueToDomElementTextContent, listenerOldValueToDomElementTextContent, listenerNewValueLengthToElementTextContent, listenerNewValueToElement}
/**
* Generic Types
* @typedef {function} observable
* @typedef {function} listener
* @typedef {function} listMap
* @typedef {function} observableBody
*/
/**
* listeners -> value -> observableFunction -> observableFunction ;
* observableBody is the Body-Observable-Construct for the observableFunctions.
* observableFunctions are: {@link addListener}, {@link removeListener}, {@link removeListenerByKey}, {@link setValue}
*
* @haskell observableBody :: [a] -> b -> c -> c
* @function
* @param {listMap} listeners
* @return {function(value:*): function(obsFn:function): function(obsFn:function)} a Observable-Function
*/
const observableBody = listeners => value => obsFn =>
obsFn(listeners)(value)
/**
* initialValue -> observableBody ;
* Observable - create new Observable incl. the initial-value
*
* @haskell Observable :: a -> Observable
* @function
* @param {number|churchNumber|string} initialValue
* @return {observableBody} - a Observable with an emptyListMap & the InitialValue
* @example
* const obsExample = Observable(0)
* (addListener)( listenerLogToConsole )
* (addListener)( listenerNewValueToElement )
*/
const Observable = initialValue =>
observableBody(emptyListMap)(initialValue)(setValue)(initialValue);
/**
* listeners -> oldValue -> newValue -> Observable ;
* set the new value and notify all listeners
*
* @extends observableBody
* @haskell setValue :: [a] -> b -> b -> Observable
* @function
* @param {listMap} listeners
* @return {function(oldValue:*): function(newValue:*): observableBody} {@link observableBody}
* @example
* let obsExample = Observable(0)
* testObs(getValue) === 0
* testObs = testObs(setValue)(42)
* testObs(getValue) === 42
*/
const setValue = listeners => oldValue => newValue => {
forEach(listeners)((listener, _) => (listener(snd))(newValue)(oldValue));
return observableBody(listeners)(newValue);
}
/**
* listeners -> value -> value ;
* get the value of Observable
*
* @extends observableBody
* @haskell getValue :: [a] -> b -> b
* @function
* @param {listMap} listeners
* @return {function(value:*): function(value:*)}
* @example
* let obsExample = Observable(0)
* testObs(getValue) === 0
*
* testObs = testObs(setValue)(42)
* testObs(getValue) === 42
*/
const getValue = listeners => value => value;
/**
* listeners -> value -> newListener -> Observable ;
* Add a new listener to the observable and the current observable value is immediately sendet to the listener.
*
* @haskell addListener :: [a] -> b -> [a] -> Observable
* @function
* @param {listMap} listeners
* @return {function(value:*): function(newListener:listMap): observableBody} {@link observableBody}
* @example
* const obsExample = Observable(0)
* (addListener)( listenerLogToConsole )
* (addListener)( listenerNewValueToElement )
*/
const addListener = listeners => value => newListener => {
newListener(snd)(value)(value)
return observableBody(push(listeners)(newListener))(value);
}
/**
* listeners -> value -> listenerKey ;
* Remove a Listener by his key
*
* @extends observableBody
* @haskell removeListenerByKey :: [a] -> b -> c
* @function
* @param {listMap} listeners
* @return {function(value:*): function(listenerKey:*)} {@link observableBody}
* @example
* let observedObject = {};
* const listenerValue = newListenerWithCustomKey( 42 )( listenerNewValueToElement (valueHolder) );
*
* let obsExample = Observable(0)
* (addListener)(listenerValue)
*
* observedObject.value === 0 // variable "observedObject" get updated from InitialValue
* obsExample = obsExample(setValue)(11)
* observedObject.value === 11 // variable "observedObject" get updated
*
* obsExample = obsExample(removeListenerByKey)(42) // 'listenerValue' is removed as listener
*
* obsExample = obsExample(setValue)(66)
* observedObject.value === 11 // variable "observedObject" getting no updates anymore
*/
const removeListenerByKey = listeners => value => listenerKey =>
observableBody(removeByKey(listeners)(listenerKey))(value);
/**
* listeners -> value -> givenListener ;
* Remove a Listener by Listener
*
* @extends observableBody
* @haskell removeListenerByKey :: [a] -> b -> c
* @function
* @param {listMap} listeners
* @return {function(value:*): function(givenListener:listener)} {@link observableBody}
* @example
* let observedObject = {};
* const listenerValue = newListener( listenerNewValueToElement (observedObject) );
*
* let obsExample = Observable(0)
* (addListener)(listenerValue)
*
* observedObject.value === 0 // variable "observedObject" get updated from InitialValue
*
* obsExample = obsExample(setValue)(11)
*
* observedObject.value === 11 // variable "observedObject" get updated
*
* obsExample = obsExample(removeListener)(listenerValue)
*
* obsExample = obsExample(setValue)(66)
*
* observedObject.value === 11 // variable "observedObject" getting no updates anymore
*/
const removeListener = listeners => value => givenListener =>
observableBody(removeByKey(listeners)(givenListener(fst)))(value);
/**
* Syntactic sugar for creating a pair of Key and Value for the new Listener.
* The key could be anything that can be comparable. (Hint: Functions are not comparable except they have a notation like n1, n2, id, pair ... etc.)
* The listenerFn takes two arguments "newValue" and "oldValue" from the the observable. Some Listener-Function are available and ready to use.
*
* @function
* @param {*} key
* @return {function(listenerFn:function) : listener} new listener with custom key for the observable
* @example
* const listenerLog = newListenerWithCustomKey(42)(listenerLogToConsole);
*/
const newListenerWithCustomKey = key => listenerFn => pair(key)(listenerFn);
/**
* Syntactic sugar for creating a pair of Key and Value for the new Listener.
* The key could be anything that can be comparable. The 'generateRandomKey' generate String with the length of six with random Letters (up-/lowercase) & Numbers.
* The listenerFn takes two arguments "newValue" and "oldValue" from the the observable. Some Listener-Function are available and ready to use.
*
* @function
* @param {function} listenerFn
* @return {listener} new listener with generated key for the observable
* @example
* let listenerLogTest = newListener(listenerLogToConsole);
*
* listenerTest = setListenerKey(42)(listenerTest)
*
* getListenerKey(listenerTest) === 42)
*/
const newListener = listenerFn => pair(generateRandomKey())(listenerFn);
/**
* Set a new Key for the listener.
*
* @function
* @param {*} newKey
* @return {function(listener:function) : listener} listener with the key
* @example
* let listenerLogTest = newListener(listenerLogToConsole);
*
* listenerTest = setListenerKey(42)(listenerTest)
*
* getListenerKey(listenerTest) === 42)
*/
const setListenerKey = newKey => listener => pair(newKey)(listener(snd));
/**
* Get the key of a listener
*
* @function
* @param {listener} listener
* @return {*} key
* @example
* let listenerLogTest = newListener(listenerLogToConsole);
*
* listenerTest = setListenerKey(42)(listenerTest)
*
* getListenerKey(listenerTest) === 42)
*/
const getListenerKey = listener => listener(fst);
/**
* The logListenersToConsole function is passed as a parameter of an observable and performs a side effect.
* The site effect logs all listeners of the observable with their keys and values on the console. * @sideeffect
*
* @extends observableBody
* @function
*/
const logListenersToConsole = listeners => _ =>
forEach(listeners)((element, index) => console.log( 'element at: ' + index + ': ' + showPair(typeof (element) === 'object' ? JSON.stringify(element) : element) ));
/*
Listener-Functions
*/
const listenerLogToConsole = nVal => oVal => console.log(`Value: new = ${nVal}, old = ${oVal}`);
const listenerNewValueToElement = element => nVal => oVal => element.value = nVal;
const listenerNewValueToDomElementTextContent = element => nVal => oVal => element.textContent = nVal;
const listenerOldValueToDomElementTextContent = element => nVal => oVal => element.textContent = oVal;
const listenerNewValueLengthToElementTextContent = element => nVal => oVal => element.textContent = nVal.length;