-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.js
1606 lines (1262 loc) · 54.6 KB
/
store.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
*
*/
// @module
// * @summary
// * @description
// * @todo
export { Graph, Script, Datum }
// data type for use in Datum
class PointerOut {
constructor ( _okey ) {
this.okey = _okey
this.gets = new EventLog
return this
}
}
// data type for use in Datum
class PointerIn {
constructor ( _ikey ) {
this.ikey = _ikey
this.gets = new EventLog
return this
}
}
// data type for use in Datum
// performance.now() logs in milliseconds
class EventLog {
constructor () {
this.book = []
return this
}
note ( preferATimeStampBoxedValue ) {
this.dispatch ( preferATimeStampBoxedValue )
this.actuallyNote ( preferATimeStampBoxedValue )
//console.log ( this.book[ this.book.length -1 ] )
}
// (graph) is optional
actuallyNote ( boxedValue ) {
this.book.push( boxedValue )
}
async dispatch ( boxedValue ) {}
// Interpreter should inline this out of existence unless
// emit actually does something in a subclass
static time () { return performance.now() }
static timeStampBox ( value ) {
return {
time : EventLog.time(),
value : value
}
}
}
// DOM already has an EventTarget class.
class AsyncDispatcher extends EventLog {
constructor () {
super()
//this.queue = []
// .push() to add on the right
// .shift() to remove on the left
//console.error (`WIP here - get reactive Scripts running.`)
// Task class?
this.tasks = { }
// Don't put (new Promise)s here, as their executors will start
// running immediately. Only put functions which return (new
// Promise)s in order that Promise executors are run only when the
// function is applied.
// EXAMPLE TASK:
// (key) is not yet used.
//'1TEST' : args => new Promise ( ( fulfill, reject ) => {
//function primeFactorsTo(max) { var store = [], i, j, primes = []; for (i = 2; i <= max; ++i) { if (!store [i]) { primes.push(i); for (j = i << 1; j <= max; j += i) { store[j] = true; } } } return primes; }
//setTimeout( ()=>{ fulfill ( [ args, primeFactorsTo ( Math.pow ( 8, 7 ) ) ] ) }, 3000 )
// } )
return this
}
// overwrites parent class method:
//
// 1. See below.
// 2. In order to be able to use 'await', we need to make (dispatch) an
// AsyncFunction. The functional difference is, while (dispatch) will
// asynchronously 'await' task execution, (dispatch's calling function)
// will proceed synchronously without waiting for (dispatch).
async dispatch( boxedValue ) {
let resolvedPromises = await Promise.all (
Object.values ( this.tasks ) .map ( t => t( boxedValue ) )
)
this.actuallyNote ( {
time : EventLog.time(),
task_resolution : resolvedPromises
} )
// logs own events to self!
}
}
//
// Arities:
//
// 1 : Argument is typechecked.
//
// String ? datum.key <= argument,
//
// Object ? datum.key <= object's first key,
// datum.value <= object's first value,
//
// ... to be implement: object's other key/values as inputs...
//
// Array ? ... to be implemented as lists of the above...
///
class Datum {
toString () { return {
'Datum.toString/0 returned:' : {
'a shallow copy of enumerable properties, { ... this }': {
... this
},
'Object.getOwnPropertyDescriptors ( this )':
Object.getOwnPropertyDescriptors ( this )
}
} }
constructor ( ...args ) {
//super()
// initialisers
Object.defineProperties ( this, {
// This is used to enable proxied application
proxyTarget : {
configurable: true,
enumerable : false,
value : Object.defineProperty(
()=>{},
'datum',
{'value' : this }
),
writable : true
},
// This is used as the id.
key : {
configurable: true,
enumerable : false,
value : undefined,
writable : true
},
// This is used both by Datum and its subclass Fun.
// In Fun, it is used to store the return value of Scripts.
value : {
configurable: true,
enumerable : false,
value : undefined,
writable : true
},
// These are used to store edges or pointers between data.
pointers : {
configurable: true,
enumerable : false,
value : {
in : { // variousTypeKeys: [ PointerIn ]
},
out : { // variousTypeKeys: [ PointerOut ]
}
},
writable : true
},
// This is used to mark a stale cache.
stale : {
configurable: true,
enumerable : false,
value : false,
writable : true
},
log : {
configurable: true,
enumerable : false,
value : {
gets : {
hits : new EventLog,
misses : new AsyncDispatcher
},
sets : new AsyncDispatcher,
setsPointerIn : new EventLog,
setsPointerOut : new EventLog,
getsPointerIn : new EventLog,
getsPointerOut : new EventLog,
// Should we log cache invalidations?
},
writable : true
},
} )
switch ( args.length )
{
case 0 :
// Allow this through, for Graph subclass
return
case 1 :
switch ( typeof args[0] )
{
case 'string':
this.key = args[0]
return this
case 'object':
this.key = Object.keys( args[0] )[0]
this.value = args[0][this.key]
return this
default:
throw Error (`Datum::constructor/1 called on n, where
(typeof n) is not 'string' or 'object'; branch undefined`)
}
default:
throw Error (`datum.constructor/n called, branch for this arity is undefined.`)
}
}
}
// Generally, when defining a Script:
//
// DEFAULT (UNSAFE_COMPUTED_SOURCE pattern):
//
// { hasSources : TRUE, // T: can read from graph
// exclusiveGets : FALSE, // T: monopolises source reads
// firmSources : FALSE, // T: blocks source deletion
//
// hasSinks : TRUE, // T: can write to graph
// exclusiveSets : FALSE, // T: monopolises sink writes
// firmSinks : FALSE, // T: blocks sink deletion
//
// cached : TRUE, // T: lazy updates
// reactive : FALSE, // T: active updates, on source changes
//
// setHandler : FALSE, // T: code run by proxyHandler.set
// getHandler : TRUE, // T: code run by proxyHandler.get
//
// }
//
// SAFE_COMPUTED_SOURCE pattern, differences from default:
//
// { hasSinks : FALSE
// firmSources : TRUE }
//
// UNSAFE_COMPUTED_SINK pattern, difference from defaults:
//
// { setHandler : TRUE
// getHandler : FALSE }
//
// PRIVILEGED_READER pattern, differences from default:
//
// { exclusiveGets : TRUE
// firmSources : TRUE }
//
// PRIVILEGED_WRITER pattern, differences from default:
//
// { exclusiveSets : TRUE
// firmSinks : TRUE }
//
// CHANGE_HANDLER pattern, differences from default:
//
// { reactive : TRUE
// firmSources : TRUE }
//
// REPORTER pattern, differences from default:
//
// { reactive : TRUE
// firmSinks : TRUE }
//
// ACTIVE_WORKER pattern, differences from default:
//
// { firmSources : TRUE
// firmSinks : TRUE
// cached : FALSE
// reactive : TRUE }
//
// PRIVILEGED_WORKER pattern, differences from default:
//
// { firmSources : TRUE
// firmSinks : TRUE
// exclusiveGets : TRUE
// exclusiveSets : TRUE
// cached : FALSE
// reactive : TRUE }
//
//
// reactive : TRUE,
// }
//
// { hasSources : true,
// exclusiveGets : false,
// firmSources : false,
//
// hasSinks : true,
// exclusiveSets : false,
// firmSinks : false,
//
// cached : true,
// reactive : TRUE,
// }
//
////////////////////////////////////////////////////////////////////////////////
// Discussion: I originally named the class "Algo", which is nice and short.
// But I realised later the code stored here is not always algorithmic
// (algorithms must end in a finite number of steps). These are more open-ended
// anonymous functions. Factors to consider in naming:
//
// - Fewest characters to type
// - Easy to pronounce, and does not clash with common Javascript morphemes
// - Accurately describes what the thing is: code stored as data
//
// Some candidates for discussion:
//
// - "P roc" "P rocedure"
// - "F un" "P rog" "P rogram"
// - "S ub" "R outine" "S ubroutine"
// - Probably too conflicty: "S ervice" "E xec"
//
// Concluding:
//
// Suggestions received:
// - Procedure StoredProcedure
// - ScriptNode Script
// - Clodes Stodes
// - MetaData
// - Snippets
// - Trigger
// - Fun
// - LambdaFunction Lambda
// - pick a unicode character like λ
//
// Next thoughts, I'm still rather preferential towards any name of four characters
// or less. Looking at the intersection of <five character morphemes which are also
// semantically accurate, and easy to speak, and easy to write, and un-confusable
// with existing Javascript ontology...
//
// ... first up, I'm thinking that perhaps the class should never be extended in
// the system just so that users can extend it and use any morpheme that they
// want...
//
// ... that being said, it still needs a canonical name. Here's the shortlist:
//
// - Script (probably semantically unsound, as it'd get mixed up with modules)
//
// ... here's what's left:
//
// - O p : not an opcode by a long shot
// - F un : too close to native morpheme
// - C ode - "a code" : spoken clumsily
// - L ambda : too close to native morpheme
// - A non
//
// On one hand, Script fits with the rule of naming brandy things with harsh
// syllables. On the other, Lambda and Script are much more specific about what we're
// actually parking at those nodes. Script is cute, but seems a bit distracting.
// Will continue to ruminate on the shortlist for a bit.
//
// ---
// Someone suggested NoFun for optimal confusion. Another wanted CoLaDa.
// I think we're down to a single-syllable shootout, sorted by minimalism:
//
// - O p
// - F un (typing F is leftier)
// - L am (typing L is rightier)
// - C ode (the only unabbreviated remainer)
//
// ---
// Further rumination brings us back to:
//
// - S cript : morphology is too common
// - A non : easier to type; but not actually anonymous!
//
////////////////////////////////////////////////////////////////////////////////
//
// args[0] must be function
// args[0] must be an object of traits
class Script extends Datum {
toString () {
return { 'Fun.toString/0 returned:' :
super.toString()
}
}
constructor ( ... args ) {
super()
switch ( args.length ) {
case 0:
throw Error (`Fun.constructor/0 : we require more arguments`)
case 1:
// allowed!
case 2:
// allowed!
break
default:
throw Error (`Fun.constructor/${args.length} called, branch for this arity is undefined.`)
}
if ( typeof args[0] !== 'function' ) {
throw Error (`Fun.constructor : first argument must be a 'function'`)
}
Object.defineProperty ( this, 'lambda', {
configurable: true,
enumerable : false,
value : args[0],
writable : true
} )
Object.defineProperty ( this, 'traits', {
configurable: false,
enumerable : false,
value : {
// UNIMPLEMENTED FEATURES:
hasSources : true,
//exclusiveGets : false,
//firmSources : false,
hasSinks : true,
//exclusiveSets : false,
//firmSinks : false,
cached : true,
//reactive : false,
//setHandler : false,
getHandler : true,
// DEFAULT VALUES are overwritten by ...
... args[1] // the second argument
},
writable : false
} )
return this
}
}
class Graph extends Datum {
toString () {
return { 'Graph.toString/0 returned:':
super.toString()
}
}
constructor ( ... args ) {
super()
// configuration (initialised by super())
this.key = ''
this.value = {}
this.log.canon = new AsyncDispatcher
// an alias
Object.defineProperty ( this.proxyTarget, 'graph', {
enumerable : false,
value : this.proxyTarget.datum,
} )
// further initialisers
Object.defineProperty ( this, 'handlers', {
enumerable : false,
value : this.handlers(),
} )
Object.defineProperty ( this, 'datumHandler', {
enumerable : false,
value : {
apply : this.handlers.datumHandlerApply,
deleteProperty : this.handlers.datumHandlerDeleteProperty,
get : this.handlers.datumHandlerGet,
set : this.handlers.datumHandlerSet
}
} )
Object.defineProperty ( this, 'graphHandler', {
enumerable : false,
value : {
... this.datumHandler,
apply : this.handlers.graphHandlerApply,
} // overwrites datumHandlerApply
} )
Object.defineProperty ( this, 'proxy', {
enumerable : false,
value : new Proxy ( this.proxyTarget, this.graphHandler )
} )
Object.defineProperty ( this, 'proxyGetOnly', {
enumerable : false,
value : new Proxy ( this.proxyTarget,
{ get : this.handlers.datumHandlerGet } )
} )
Object.defineProperty ( this, 'proxySetOnly', {
enumerable : false,
value : new Proxy ( this.proxyTarget,
{ set : this.handlers.datumHandlerSet } )
} )
Object.defineProperty ( this, 'proxySetGetOnly', {
enumerable : false,
value : new Proxy ( this.proxyTarget,
{ set : this.handlers.datumHandlerSet,
get : this.handlers.datumHandlerGet } )
} )
/*
if ( ! ( node instanceof Serl.Node ) ) {
// throw Error ( `Graph::constructor() called, first argument was not an instance of Serl.Node.` )
node = new Serl.Node ( 'node created by Graph::constructor()' )
}
*/
switch ( args.length ) {
case 0:
return { //serlNode : node,
graph : this,
store : this.proxy }
case 1:
switch ( args[0] ) {
case 'store':
return this.proxy
case 'proxyTarget':
return this.proxyTarget // () => this
case 'graph':
return this
default:
throw Error (`Graph.constructor/1 called, the argument
was not understood.`)
}
break
default:
throw Error (`Graph.constructor/n called, where no branch was
defined for arity-n.`)
}
} // Graph.constructor
logFormat ( typeString, vertexObject, time ) {
return {
time : time,
datum : vertexObject,
type : typeString
}
}
scriptRunAndSetValue ( datum ) {
//console.log (`graph.vertexGetTyped/1 will now return datum.stale : `,
//datum.stale, 'datum.value', datum.value, 'datum.key', datum.key )
let result
if ( datum.stale || ! datum.traits.cached ) {
// !stale && !cached
// stale && !cached
// stale && cached
// Is there a more efficient way to do this?
let proxy
= datum.traits.hasSources
? ( datum.traits.hasSinks
? this.proxySetGetOnly
: this.proxyGetOnly
)
: ( datum.traits.hasSinks
? this.proxySetOnly
: this
)
result = datum.value = datum.lambda ( proxy )
datum.stale = false
// LOGGING - CACHE MISS(-THEN-SET-AND-THEN-HIT)
let timeStampBoxedValue = EventLog.timeStampBox ( result )
datum.log.gets.misses.note ( timeStampBoxedValue )
this.log.canon.note ( this.logFormat (
'get_vertex_miss_scriptRunAndSetValue',
datum,
timeStampBoxedValue.time
) )
}
else {
// !stale && cached
result = datum.value
// cache hit, scenario 1 of 3; a Script
datum.stale = false // for general coherence
// LOGGING - CACHE HIT - more scenarios in vertexGetTyped; more
// scenarios in graphHandlerApply, datumHandlerApply
let timeStampBoxedValue = EventLog.timeStampBox ( result )
datum.log.gets.hits.note ( timeStampBoxedValue )
this.log.canon.note ( this.logFormat (
'get_vertex_hit_scriptRunAndSetValue',
datum,
timeStampBoxedValue.time
) )
}
return result
}
vertexDelete ( key ) {
if ( ! ( key in this.value ) ) { return true }
if ( ( typeof this.value[ key ]('datum').value == 'object' ) )
{
if ( ! this.vertexPrune ( key ) ) { return false }
}
let deletedDatum = this.value[ key ]('unproxy').datum
delete this.value[ key ]
let success = ! ( key in this.value )
// LOGGING
if ( success ) {
let timeStampBoxedValue = EventLog.timeStampBox ( deletedDatum )
this.log.canon.note ( this.logFormat (
'delete_vertex_vertexDelete',
deletedDatum,
timeStampBoxedValue.time
) )
// Only time stamp is needed; refactoring may make this more
// efficient later. FIXME
}
return success
}
vertexGet ( key ) {
if ( ! ( key in this.value ) )
{
//console.log(this)
//console.log (`graph.vertexGet/1 could not find the key (${key}) in
//graph.value`)
return undefined
}
//console.log ( `vertexGet/1, BEFORE getting Datum`,key )
let datum = this.value[ key ]('datum')
//console.log ( `vertexGet/1, AFTER getting Datum`, key )
//console.log ( `graph.vertexGet/1 will get graph.value[ '${key
//}' ]() : `, this.value [ key ](), datum.traits )
return this.vertexGetTyped ( datum )
}
// Lower-level call, used by
// vertexGet
// datumHandlerApply
// graphHandlerApply
vertexGetTyped ( datum ) {
//console.log ( `vertexGetTyped`, datum )
let result
if ( datum instanceof Script && datum.traits.getHandler ) {
return this.scriptRunAndSetValue ( datum )
}
else
if ( typeof datum.value == 'object' ) { // ( 'function's are not sprouted )
// Wherein. if we find that the user has previously set an
// object as the value, we try to intercept the call to that
// object's properties...
//console.log (`graph.vertexGet/1 : found that datum.value is
//an object, so will return graph.value ['${key}'] `)
result = this.value[ datum.key ]
// cache hit, scenario 1 of 2; not a Script, no cache
}
else { result = datum.value
// cache hit, scenario 2 of 2; not a Script, no cache
}
// LOGGING - 2 cache hit scenarios in vertexGetTyped; more scenarios in
// graphHandlerApply, datumHandlerApply, runFun
//console.log(datum, this.value[key])
let timeStampBoxedValue = EventLog.timeStampBox ( result )
datum.log.gets.hits.note ( timeStampBoxedValue )
this.log.canon.note ( this.logFormat (
'get_vertex_hit_vertexGetTyped',
datum,
timeStampBoxedValue.time
) )
return result
}
// Deletes all child vertices.
// Prune is to Sprout, what Delete is to Update.
vertexPrune ( key )
{
for ( const loopKey in this.value ) {
if ( loopKey.startsWith ( key + '.' ) ) {
if ( ! this.vertexDelete ( loopKey ) ) { return false }
}
}
return true
}
vertexSet ( ... args )
{
//console.log(`graph.vertexSet/n START`)
let datumToSet
switch ( args.length )
{
case 0:
throw Error (`graph.vertexSet/0 called; unsupported arity.`)
case 1:
//console.warn (`graph.vertexSet/1 : rewrite & test for this branch`)
let keyToSet = args[0]
datumToSet = new Datum ( keyToSet )
this.value [ datumToSet.key ]
= new Proxy ( datumToSet.proxyTarget, this.datumHandler )
break
}
// HEREON: args.length > 1
//console.log(`graph.vertexSet/[n>1] checked`)
let valueToSetType = typeof args[1]
let keyToSet = args[0]
let valueToSet = args[1]
//console.log (`graph.vertexSet/[n>1], BEGIN, key:`, keyToSet,
//'value:', valueToSet)
//console.log ( `graph.vertexSet/[n>1], initial value : `,
//this.value[keyToSet], `update value:`, valueToSet )
let proxiedOldDatum
// If the node/vertex does not yet exist ...
if ( ! ( proxiedOldDatum = this.value[ keyToSet ] ) ) {
datumToSet = new Datum ( { [keyToSet] : valueToSet } )
}
else { // ... the node/vertex already exists...
// ... then check its datum;
let oldDatum = proxiedOldDatum('unproxy').datum
//console.log (oldDatum)
// ... and if the datum's value is an object, then prune the graph;
if ( typeof oldDatum.value == 'object' ) {
if ( ! this.vertexPrune ( keyToSet ) ) { return false }
}
// ... and finally update datum.value, but not datum's other
// properties (pointers, logs, cache);
datumToSet = oldDatum
datumToSet.value = valueToSet
}
//console.log(`graph.vertexSet/[n>1] datumToSet has been defined.`)
////////////////////////////////////////////////////////////////////////////////
// datumToSet MUST BE DEFINED BY THIS POINT...
////////////////////////////////////////////////////////////////////////////////
if ( datumToSet.value instanceof Script )
{
//console.warn (`graph.vertexSet/[n>1] : value instanceof Script `)
// Assign all old Datum's own properties except (those listed below) to Fun.
delete datumToSet.lambda
delete datumToSet.value
delete datumToSet.proxyTarget
let scriptToSet
= Object.defineProperties (
valueToSet,
Object.getOwnPropertyDescriptors ( datumToSet ) )
scriptToSet.stale = true
// Script will not run until the next get (no gets here)
//
// Whether Fun.traits.cached is true or not, the Fun.stale
// property will be defined. Because it is defined for all
// Datum, and Script extends Datum.
this.value [ keyToSet ]
= new Proxy ( scriptToSet.proxyTarget, this.datumHandler )
//console.log( `graph.vertexSet/[n>1], Fun, AFTER SET,
//keyToSet:`, keyToSet,
// 'value which was set:', this.value [ keyToSet ]('datum'),
// 'success check components :', this.value [ keyToSet ]('datum'),'==', args[1] ,
// 'result:', this.value [ keyToSet ]('datum') == args[1] )
let result = this.value [ keyToSet ]('datum') == args[1]
// console.log (`graph.vertexSet/[n>1], Fun, result obtained: result`)
// console.log (this.value [ keyToSet ]('datum').traits)
if ( result ) {
// LOGGING - 1 scenario (1 of 2 in vertexSet/n)
let timeStampBoxedValue = EventLog.timeStampBox ( {
'Script instance' : scriptToSet ,
'FIXME' : `FIXME Placeholder log format for Script, because
Script.toString/n doesn't handle circular
objects yet.`
} )
datumToSet.log.sets.note ( timeStampBoxedValue )
this.log.canon.note ( this.logFormat (
'set_vertex_Script_vertexSet',
scriptToSet,
timeStampBoxedValue.time
) )
}
////////////////////////////////////////////////////////////////////////////////
// Detect sinks and sources, and plant pointers.
//
// WARNING: filtered for uniqueness.
// https://stackoverflow.com/a/14438954/1378390
let sourceKeys = ( scriptToSet.traits.hasSources
? this.handlers.sniffSourceKeys ( scriptToSet.lambda )
: [] ).filter((v, i, a) => a.indexOf(v) === i)
//console.warn ( `hasSinks`, scriptToSet.traits.hasSinks )
let sinkKeys = ( scriptToSet.traits.hasSinks
? this.handlers.sniffSinkKeys ( scriptToSet.lambda )
: [] ).filter((v, i, a) => a.indexOf(v) === i)
//console.warn ( `sinkKeys`, sinkKeys )
sourceKeys.forEach ( key => {
this.handlers.setSourcePointer ( scriptToSet, key )
} )
sinkKeys.forEach ( key => {
this.handlers.setSinkPointer ( scriptToSet, key )
} )
////////////////////////////////////////////////////////////////////////////////
return result
}
else
{ // If datumToSet.value is NOT a Script, then complete the assignment.
//console.log (`graph.vertexSet/[n>1] : value NOT instanceof Script `)
// If valuetoset is an object ...
if ( valueToSetType == 'object' ) // ( 'function's are not sprouted )
{
//console.log (`graph.vertexSet/[n>1] : value is an object `)
// ... then set all of its child vertices;
if ( ! this.vertexSprout ( keyToSet, valueToSet ) )
{ return false }
}
//console.log (`graph.vertexSet/[n>1] : value is NOT an object `,
//datumToSet)
this.value [ datumToSet.key ]
= new Proxy ( datumToSet.proxyTarget, this.datumHandler )
//console.log( `graph.vertexSet/[n>1], END, key:`, keyToSet,
// 'value:', this.value [ keyToSet ],
// 'success check components :', this.value [ keyToSet ],'==', args[1] )
let result = this.value [ keyToSet ]('unproxy').datum.value == args[1]
if ( result ) {
// LOGGING - 1 scenario (2 of 2 in vertexSet/n)
let timeStampBoxedValue = EventLog.timeStampBox ( args[1] )
datumToSet.log.sets.note ( timeStampBoxedValue )
this.log.canon.note ( this.logFormat (
'set_vertex_vertexSet',
datumToSet,
timeStampBoxedValue.time
) )
}
return result
} // End of block where: (value instanceof Fun)
}
// Updates all child vertices.
// Prune is to Sprout, what Delete is to Update.
vertexSprout ( key, value )
{
for ( const subKey in value ) {
let compoundKey = key + '.' + subKey
if ( ! this.vertexSet ( compoundKey, value[ subKey ] ) )
{ return false }
}
return true
}
handlers () { return {
'datumHandlerDeleteProperty': ( targ, prop ) =>
{
// (targ) is a (datum instance).proxytarget.
//
// because (datum instance).proxytarget.datum refers to the
// instance's (this), therefore, (targ.datum) refers to the underlying
// instance.
//
// because this is an aef in a method on graph, (this) here refers to
// the instance of graph.