-
Notifications
You must be signed in to change notification settings - Fork 1
/
liquid.reb
3645 lines (3044 loc) · 105 KB
/
liquid.reb
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
REBOL [
; -- basic rebol header --
file: %liquid.r
version: 2.0.0
date: 2013-06-14
purpose: "Dataflow processing kernel. Supports many computing modes and lazy programming.."
author: "Maxim Olivier-Adlhoch"
copyright: "Copyright © 2004-2013 Maxim Olivier-Adlhoch"
web: http://www.moliad.net/modules/liquid/
;-- slim parameters --
slim-name: 'liquid
slim-prefix: none
slim-version: 1.0.2
;-- Licensing details --
copyright: "Copyright © 2013 Maxim Oliver-Adlhoch"
license-type: "Apache License v2.0"
license: {Copyright © 2012 Maxim Olivier-Adlhoch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.}
;-- Documentaton --
notes: "You need slim to use this library (get it at: www.moliad.net/modules/slim/)"
details: {
Liquid is a very low-level library which implements dataflow driven programming in REBOL.
It supports advanced features like lazyness and on-the-fly mutation.
The basic !plug implements the complete reference design and will actually allow you to
switch computation methods on the fly. This means that the same plug implements dependency
bridging, containment, hold and modify, and data sharing.
The online docs are where you should go to get all the details about using and
customizing liquid plugs.
}
to-do: {
-----------------
NOTE - to do list is rarely up to date,
-----------------
-add more return values to functions like link, fill and connect, to allow interactive reactions to failed operations...
-make ALL func tails (returns) GC friendly
-once piped, a plug should not propagate dirty states, unless its pipe is dirty. It should ignore any dirty
calls comming from its (temporarily useless) subordinates.
-support label being present in subordinate list more than once (complements exclusive wrt labeled linking mode)
-when attaching two piped plugs, add a callback to allow a pipe server to de-allocate itself safely when it
detects it has no more pipe clients.
** v2 proposals **
-PUSH-MODE PROPAGATION OF PARAMETERS to all observers.
doesn't actually start processing, but distributes a set of values within a graph.
-PROPAGATE ANY STATE, not just dirty. (ex: error), a subset of above
-PARAMETER & CONTAINER INDEXED CACHING, caching of liquid is based on a key set of parameters, allowing us to
reload past processing!
-CLASS-WIDE LINKS! all instances share links (allows VERY fast manipulation of global state without needing to
link each instance separatly)
-SUBORDINATE CONTAINMENT (like a fill, directly stored in subordinate block.) allows us to simulate
a node which is connected to several subordinate containers, without the need to actually allocate any of them.
}
history: {
v0.0.1 - 0.5.0
-several years of prototyping different dataflow systems.
-eventually combined into one codebase
-eventually merged into A SINGLE base class (out from 4 different & incompatible base classes)
-adding of caching, low-level linking, propagation, processing levels.
-cycle detection algorythm testing
-several stub functions now officially part of api.
v0.5.1
-finalise new piping and mud mechanism
v0.5.2
-link()/exlusive implementation
v0.5.3
-add /as to liquify
v0.5.4
-add stats method to valve.
-fully test and enable labeled mode for linking
-add valve/links/labels
-add valve/links/labeled
-finish block optimisation in instigate returning all subordinate values directly.
v0.5.5 - 24-Apr-2006/13:13:13
-added disregard
-rebuilt unlink using disregard (cause v0.5.4 didn't unlink observers! MAJOR BUG)
-link() should return an error when trying to link a pipe server using /label (not likely, but for advanced users this must be explicitely disallowed)
-support multiple plugs per label
v0.5.6 - 26-Apr-2006/11:14:34
-added /fill refinement to liquify
v0.5.7 - 27-Apr-2006/3:35:39
-removed all double comments (;;) from code
-improved comments on valve/setup and explicitely state that we can call link at that point.
-added valve/sub()
-cycle? now officially part of miscelaneous methods
-added /link refinement to liquify
v0.5.8 - 29-Apr-2006/14:31:40
-verbose is now off by default, from now on.
v0.5.9 - 29-Apr-2006/15:02:55
-fix dirty? state return valur of purify.
-fixed propagate as a result of above fix
v0.5.10 - 24-May-2006/17:07:43
-added category to valve. helps in classification.
-removed init as default startup state, easy to forget and invalidates dirtyness by default..
better let the user toggle it on knowingly :-)
v0.5.11 - 5-Oct-2006/2:27:14
-renamed plug! to !plug (no clash with real types)
-officially added !node alias to !plug (to ease adoption)
-liquify/link properly support single plug! spec (it used to support only a block of plugs)
-added linked-container? attribute to !plug
-fill now creates a container (simple pipe) rather than a pipe (linked pipe) by default.
-added word! clash protection on link labels, so that instigate code can separate labels from actual word! type data (not 100% fault tolerant, but with documentation, it becomes easy to prevent).
v0.5.12 - 17-Oct-2006/11:58:09
-license switch to MIT
v0.5.13 - 20-Oct-2006/5:31:44
-attach() pipe linking method
-detach() pipe unlinking method
-added support for a block! of subordinates in link utility func.
v0.6.0 - 1-Nov-2006/11:19:29 (MOA)
-quick release cleanup.
-version major
v0.6.1 - 27-Nov-2006/15:01:40 (MOA)
-added pipe function, with optional /with refinement. makes piping more explicit.
-fixed linked-container? mode handling here and there.
v0.6.2 - 12-Dec-2006/22:35:25 (MOA)
-fixed a rare linking setup, when you supply none! as the subordinate to create orphaned links.
v0.6.3 - 12-Dec-2006/22:55:18 (MOA)
v0.6.4 - 4-Feb-2007/7:01:06 (MOA)
-adding the commit feature. vastly simplifying many plug uses.
-not yet implemented commit, but layed out the design for future compatibility.
v0.6.5 - 19-Feb-2007/22:35:54 (MOA)
v0.6.5 - 13-Apr-2007/19:02:42 (MOA)
v0.6.6 - 13-Apr-2007/19:02:58 (MOA)
v0.6.7 - 20-Apr-2007/2:59:58 (Unknown)
v0.6.8 - 29-Apr-2007/7:55:52 (Unknown)
v0.6.9 - 30-Apr-2007/16:56:30 (Unknown)
-added insubordinate method.
-added /with refinement to liquid
v0.6.10 - 30-Apr-2007/22:07:40 (Unknown)
-now clears sid in destroy (extra mem cleanup)
v0.6.11 - 11-May-2007/0:46:56 (Unknown)
-added reset refinement to link and valve/link
v0.6.14 - 11-Jul-2007/19:46:17 (MOA)
v0.6.15 - 15-Jul-2007/0:58:46 (Unknown)
v0.6.17 - 16-Jul-2007/22:57:02 (MOA)
v0.7.0 - 7-Mar-2009/00:54:04(MOA)
-added PIPE-SERVER-CLASS functionality to ease custom pipe management
-fixed minor fill bug related to binding in rebol sub-objects
v0.7.1 - 8-Mar-2009/05:54:04(MOA)
-fixed regression in link() where a word was deleted from the code for some unknown reason, breaking the function on un-piped nodes.
-added FROZEN? and related functionality. can be set to a function for powerfull dynamic node freezing.
v0.7.2 - 8-Mar-2009/21:25:55(MOA)
-officially deprecated and REMOVED SHARED-STATES from the whole module
-ON-LINK()
v0.8.0 - 15-Mar-2009/00:00:00(MOA)
-adding stream engine for propagation-style inter-node messaging.
-STREAM() added for look-ahead messaging (ask observers to react to us)
-ON-STREAM() added to support callbacks when a stream message arrives at a plug.
v0.8.1 - 28-Mar-2009/00:00:00(MOA)
-PROPAGATE?() added to valve - allows us to optimise lazyness in some advanced plugs
-LINK?() regression found and fixed... cycle?() was not being used anymore!
v0.8.2 - 02/04/2009/11:13PM(MOA)
-the index? of returned instigate() block is now OFFICIALLY the insert position of the linked-container, if any.
this used to be an undefined aspect of liquid, but must now be explicitely handled in any
custom cleanup function. This was added, since some plugs will need this at the head, while others
will need it at the tail, for processing consistency.
v0.8.3 - 04/04/2009/3:10AM (MOA)
-fixed bug in CLEANUP() which didn't use path to access the instigate method.
-fixed CYCLE?() bug which tripped over labeled links.
-added optional PLUG/PREVIOUS-MUD attribute handling to fill, allows other funcs to detect if fill actually
changed value.
-PLUG/PREVIOUS-MUD now set within CLEANUP(), so that multiple fills do not trigger events, until they have been used.
-LINKED?() little improvements.
v0.8.4 - 27/04/2009/10:37PM (MOA)
-plug/mud is now an official store of pre-allocated series, which you do not want to reset (re-allocate) at each process()
no code change, but now an official specification. so plug/mud is not changed by any function except fill() and related.
v0.8.5 - 03/05/2009/8:15AM (MOA)
-added UNLINK() stub function
v0.8.6 - 15/09/2009 6:27PM (MOA)
-fixed ATTACH() regression which didn't set pipe? on pipe clients ... strange
-added 'LIQUID-ERROR-ON-CYCLE? attribute to lib. this tells liquid to raise an error if
the LINK?() method detects a cycle?(). prevents unlinked plugs from being silently ignored
and causing frustrating errors (and hard to understand/debug).
-Liquid now sets 'LIQUID-ERROR-ON-CYCLE? to true by default, so connection errors will result
in thrown errors... beware.
v1.0.0 01/10/2010 (MOA)
-removed ALL vprint-based debugging to dramatically increase performance. (only !plug/valve/stats remains with vprint).
-as I am using this library in a publicly-released application for the first time, and because I've
been actively using liquid successfully in most projects for YEARS, I've decided to sign-off
on an official reference v1 release.
v1.0.1 17/04/2010 (MOA)
-optimised propagate so its ignored when a node is already dirty.
-fixed cycle? so it starts at observer, instead of subordinate... fixes erronous cycle detections.
v1.0.2 27/04/2010 (MOA)
-bridge pipe server mode work started.
-completely rebuilt attach and link functions. they are faster and add bridge.
-replace linked-container? by resolve-links? attribute. any pipe or container can now process as well as store.
-fixed little shortcomings in destroy() and linked?()
-half of the library is rewritten with optimisation for speed and bridge support throughout.
-empiric unit-testing shows that changes have had a VERY positive effect on various aspects of liquid (alloc, process,etc).
-added support for CHECK-CYCLE-ON-LINK?. this is EXTREMELY advanced control of liquid, but makes an as extreme difference on allocation.
use it within controled systems which can clearly and properly manage the linkeage. when building user guis or readable scripts,
make sure to use cycle? before linking, if cycle-check is off.
-unit-testing & profiling
v1.0.3 30/04/2010 (MOA)
-bridge support testing and fixing.
v1.0.4 12/05/2010 (MOA)
-CHECK-CYCLE-ON-LINK? is now OFF BY DEFAULT
-bridge mode finished
-bug fixes in CLEANUP()
-added /TO refinement to ATTACH() stub
-added REINDEX-PLUG()
-added FREEZE() and THAW() stubs
v1.0.5 2010-06-23 (MOA)
-added PROCESS() stub
-added BRIDGE() stub
-added PLUG?() to determine if something is a liquid plug
-added NOTIFY() valve method and stub
-added /ONLY to valve/DETACH() and stub
-added /PRESERVE to ATTACH() stub
-removed most vprinting to accelerate library
v1.1.0 2012-02-07 (MOA)
-added concept of !default-plug.
v1.1.1 2012-09-12 (MOA)
-re-instated a few vprints
-fixed PLUG/VALVE/FILL() when used on pipe clients... they don't get propagated back (it never gets dirty).
v1.2.0 2012-11-07 (MOA)
-Added FORMULATE()
-Tweaked PROCESSOR(), which now uses FORMULATE()
-Started to add unit tests using SLUT library.
v1.2.1 2012-11-14 (MOA)
-we can now control HOW link resolving is merged the plug's mud (pipe or container). See resolve-links? documentation for more details.
v2.0.0 2013-06-14 (MOA)
-first steps to make liquid R3 compatible.
-changed license to Apache v2
-possible divergence wrt R2 version.
}
;- /history end
]
;--------------------------------------
; unit testing
;--------------------------------------
;
; test-enter-slim 'liquid
;
; test-init [ print "44" ]
; test-init [ print "55" ]
;
;
;
;
;
;
; test-preamble 'values [ value-a: liquify/fill !plug 3 value-b: liquify/fill !plug 4 value-c: liquify/fill !plug 5]
; test-preamble 'sum-plug <[
; !sum-plug: make !plug [
; valve: make valve [
; process: funcl [plug data][
; i: 0 foreach val data [ i: i + val ] ; testing a comment within the testing engine
; ; another comment test
; plug/liquid: i
; ]
; ]
; ]
; ]>
;
;
;
; test 'tprin [ print "test tprin" true] ; this test has no preamble requirements
; test [add-3-7 sum-plug liquid] [sum-plug values] [ plug: liquify/link !sum-plug [value-a value-b] probe content plug] ; this test requires preamble 'sum-plug and 'values
; test [add-4-5 sum-plug liquid] [sum-plug values] [ plug: liquify/link !sum-plug [value-a value-b] probe content plug] ; this test requires preamble 'sum-plug and 'values
; test 'string-err-msg <[
; print "a"
; print "b"
; "tadam"
; ]>
; test 'unset-test [print "fofo"]
; test 'zdiv-error-test <[
; 0 / 0
; ]>
;
;
;----
; use following line to determine real code size without comments.
;
; currently around 55 kb ! :-) which means there is as much documentation and structure information within the source!!
;----
; write %stripped-liquid.reb entab replace/all "^^-" "^-"mold load/header %liquid.reb
slim/register [
verbose: false
; next sid to assign to any liquid plug.
; and also tells you how many plugs have been registered so far.
;- liquid-sid-count:
liquid-sid-count: 0
;--------------------------
;- plug-list:
plug-list: make map! []
;--------------------------
;- total-links:
total-links: 0
;--------------------------
;- liquid-error-on-cycle?:
liquid-error-on-cycle?: true
;--------------------------
;- !default-plug:
;
; this plus is used as the default by all api functions.
;
; you may override this in order to add custom functionality in all of your app.
; note that you MUST NEVER overide the actual !plug word.
;
; always use !plug derivatives.
;--------------------------
!default-plug: none
;--------------------------
;- check-cycle-on-link?:
; this is used to control the cycle? check which is VERY slow on large networks.
;
; this should usually be set to true when debugging and developping, but once your code is robust and you can
; guarantee cycle? coherence, then it should be set to false, it will vastly improve linking speed.
;
; as such, cycle checks are the most demanding operations you can perform on a network of plugs.
;
; when set to false, you can still call cycle? manually on any plug. which is a good thing for user-controled
; linking verification. but for the vast majority of links, where a program is handling the connections,
; the cycle check isn't really needed.
check-cycle-on-link?: false
;-
;------------------------------------------------------------------------------------------------------------------------------------------
;- UTIL FUNCTIONS
;------------------------------------------------------------------------------------------------------------------------------------------
; ;--------------------------
; ;- extract-set-words()
; ;--------------------------
; ; purpose: finds set-words within a block of code, hierarchically.
; ;
; ; inputs: block!
; ;
; ; returns: the list of words in set or normal word notation
; ;
; ; notes: none-transparent
; ; (DEPRECATED using the version in slim)
; ;--------------------------
; extract-set-words: func [
; blk [block! none!]
; /only "returns values as set-words, not ordinary words. Useful for creating object specs."
; /local words rule word
; ][
; vin "extract-set-words()"
; words: make block! 12
; parse blk =rule=: [any [
; set word set-word! (append words either only [word][to-word word]) |
; hash! | into =rule= | skip
; ]]
;
; vout
; words
; ]
; vin: vout: none
;--------------------------------------------------------------------------------------------------
;- INTERNAL FUNCTIONS
;--------------------------------------------------------------------------------------------------
;-----------------------------------------
;- alloc-sid()
;-----------------------------------------
; currently the sid is a simple number, but
; could become something a bit stronger in time,
; so this allows us to eventually change the system without
; need to change any plug generating code.
;-----------------------------------------
alloc-sid: func [][
liquid-sid-count: liquid-sid-count + 1
]
;--------------------
;- retrieve-plug()
;--------------------
retrieve-plug: select-plug: func [
"return the plug related to an sid stored in the global plug-list"
sid
][
select plug-list sid
]
;--------------------
;- reindex-plug()
; If a plug's object was modified, after allocation, the index still points to the old object.
;
; this allows us to do the following AFTER a call to liquify:
; plug: make plug [...]
;
; why? some toolkits will allocate nodes pre-emptively and then allow an api to
; modify the allocated node directly, usually to add new values to the plug itself.
;
; if the plug isn't re-indexed, any call to retrieve-plug will still return the old
; object, and the changes will seem to have vanished!
;
; note: It is an ERROR to call reindex-plug on a plug/sid which doesn't exist,
; either cause it was not yet intialized or was destroyed.
;--------------------
reindex-plug: select-plug: func [
"replaces the object stored in the plug-list with this new one."
new-plug
/local old-plug list
][
either old-plug: select plug-list new-plug/sid [
plug-list/(new-plug/sid): new-plug
old-plug ; new in v2
][
to-error "LIQUID/reindex-plug() called on an unallocated plug."
]
]
;-----------------
;- freeze()
;-----------------
freeze: func [
plug
][
vin [{freeze()}]
plug/frozen?: true
vout
]
;-----------------
;- thaw()
;-----------------
thaw: func [
plug
][
vin [{thaw()}]
plug/frozen?: false
plug/valve/notify plug
vout
]
;-
;------------------------------------------------------------------------------------------------------------------------------------------
;- EXTERNAL API !
;------------------------------------------------------------------------------------------------------------------------------------------
;-----------------------
;- liquify()
;-----------------------
liquify: func [
type [object!] "Plug class object."
/with spec "Attributes you wish to add to the new plug ctx."
/as valve-type "shorthand to derive valve as an indepent from supplied type, this sets type/valve/type"
/fill data "shortcut, will immediately fill the liquid right after its initialisation"
/piped "since fill now makes containers by default, this will tell the engine to make it a pipe beforehand."
/pipe "same as piped"
/link plugs [block! object!]
/label lbl [word!] "specify label to link to (no use unless /link is also provided)"
/local plug
][
vin ["liquify(" type/valve/type ")"]
spec: either none? spec [[]][spec]
; unify plugs datatype
; plugs: compose [(plugs)]
if object? plugs [
plugs: compose [(plugs)]
]
if as [
spec: append copy spec compose/deep [valve: make valve [type: (to-lit-word valve-type)]]
]
plug: make type spec
;plug/shared-states: type/shared-states
plug/valve/init plug
if any [piped pipe][
plug/valve/new-pipe plug
]
if fill [
plug/valve/fill plug data
]
if link [
;print "#################################"
link*/label plug plugs lbl
; forall plugs [
; either lbl [
; plug/valve/link/label plug first plugs lbl
; ][
; plug/valve/link plug first plugs
; ]
; ]
]
vout
first reduce [plug plug: plugs: data: none] ; clean GC returnf
]
;--------------------------
;- build()
;--------------------------
; purpose: replaces make for context which copies functions from one object to another.
;
; inputs:
;
; returns:
;
; notes: -care must be taken, because from there on, binding to self is impossible.
; -you MUST provide attributes and values in pair within the spec... its not
; arbitrary code.
;
; tests:
;--------------------------
; build: funcl [
; base-object [object!]
; spec [block!]
; ][
; vin "build()"
; sw: none
;
; parse spec [
; some [
; set sw set-word!
; [
; set-word! (
; to-error "liquid/build() cannot use chained set-words in spec (ex: [this: that: 66] is invalid)"
; )
; | skip
; ]
; ]
; ]
;
; vout
; ]
;
;-----------------
;- destroy()
;-----------------
destroy: func [
plug
][
plug/valve/destroy plug
]
;-----------------------------------------
;- true?()
;-----------------------------------------
true?: func [value][value = true]
;------------------------------
;- count()
;---
while*: get in system/contexts/user 'while
count: func [;
series [series!]
value
/while wend
/until uend
/within min "we must find at least one value before hitting this index, or else we return 0"
/local counter i item
][
counter: 0
i: 0
while* [
(not tail? series)
][
i: i + 1
if find item: copy/part series 1 value [
counter: counter + 1
]
; check if we hit the end condition once we started counting value
if all [while counter > 0] [
if not find item wend [
series: tail series
]
]
; check if we hit the end condition once we started counting value
if all [until counter > 0] [
if find item uend [
series: tail series
]
]
; are we past minimum search success range?
if all [
within
counter = 0
i >= min
][
series: tail series
]
series: next series
]
counter
];
;-----------------------------------------
;- fill()
;-----------------------------------------
fill: func [
"shortcut for a plug's fill method"
plug [object!]
value
/channel ch
][
either channel [
plug/valve/fill/channel plug value ch
][
plug/valve/fill plug value
]
]
;-----------------------------------------
;- pipe()
;-----------------------------------------
pipe: func [
"converts a plug into a pipe"
plug [object!]
/with val
][
vin "pipe()"
unless with [
val: plug/valve/content plug
]
vprobe type? :val
plug/valve/new-pipe plug val
val: none
vout
plug
]
;-----------------------------------------
;- content/cleanup()
;-----------------------------------------
content: funcl [
"shortcut for a plug's content method"
plug [object!]
/channel ch [word!]
][
vin"content()"
r: either channel [
plug/valve/cleanup/channel plug ch
][
plug/valve/cleanup plug
]
vout
r
]
cleanup: :content
;-----------------
;- dirty()
;-----------------
dirty: func [
plug
][
plug/valve/dirty plug
]
;-----------------
;- notify()
;-----------------
dirty: func [
plug
][
plug/valve/notify plug
]
;-----------------------------------------
;- link()
;-----------------------------------------
link: func [
"shortcut for a plug's link method"
observer [object!]
subordinate [object! block!]
/label lbl [word! none!]
/reset "will call reset on the link method (clears pipe or container constraints, if observer is piped)"
/exclusive "Only allow one link per label or whole unlabled plug"
/local blk val
][
;probe first subordinate
;probe mold/all head subordinate
either block? subordinate [
;probe subordinate
;vprobe reduce ["linking a block of plugs: " extract subordinate 2]
;probe length? subordinate
forall subordinate [
val: pick subordinate 1
either any [
set-word? :val
lit-word? :val
][
change subordinate to-word val
][
;print ["APPLYING : type?: " type? val]
change subordinate do val
;probe type? pick subordinate -1
]
]
blk: subordinate: head subordinate
][
blk: subordinate: compose [(subordinate)]
]
foreach subordinate blk [
; we can now specify the label directly within the block, so we can spec a whole labeled
; link setup in one call to link
either word? subordinate [
lbl: subordinate
][
any [
all [lbl reset observer/valve/link/label/reset observer subordinate lbl]
all [lbl exclusive observer/valve/link/label/exclusive observer subordinate lbl]
all [lbl observer/valve/link/label observer subordinate lbl]
all [reset (reset: none true) observer/valve/link/reset observer subordinate ]
all [exclusive observer/valve/link/exclusive observer subordinate ]
observer/valve/link observer subordinate
]
]
]
]
;-----------------
;- unlink()
;-----------------
unlink: func [
observer
/detach
/only subordinate [object!] "unlink only specified subordinate from observer, silently ingnores invalid subordinate"
][
if detach [
observer/valve/detach observer
]
either only [
observer/valve/unlink/only observer subordinate
][
observer/valve/unlink observer
]
]
;-----------------
;- attach()
;-----------------
attach: func [
observer
pipe
/to channel
/preserve "our value is kept when attaching, so that the pipe will immediately use our value(we fill pipe)"
/local val
][
if preserve [
val: content observer
]
either to [
observer/valve/attach/to observer pipe channel
][
observer/valve/attach observer pipe
]
if preserve [
fill observer val
]
]
;-----------------
;- detach()
;-----------------
detach: func [
plug
/only
][
either only [
plug/valve/detach/only plug
][
plug/valve/detach plug
]
]
; just memorise so we can use this enhanced version within liquify
link*: :link
;--------------------
;- objectify()
;--------------------
objectify: func [
"takes a process func data input and groups them into an object."
;plug [object!]
data [block!]
/local blk here plugs
][
blk: compose/only [unlabeled: (plugs: copy [])]
parse data [
any [
[here: word! (append blk to-set-word pick here 1 append/only blk plugs: copy [])]
|
[here: skip (append plugs pick here 1)]
]
]
; parse plug/subordinates
context blk
]
;--------------------
;- is?()
;--------------------
is?: func [
"tries to find a word within the valve definition which matches qualifier: '*qualifier*"
plug [object!]
qualifier [word!]
][
;print "IS?"
found? in plug/valve to-word rejoin ["*" qualifier "*"]
]
;-----------------
;- dirty?()
;-----------------
dirty?: func [
plug [object!]
][
true = plug/dirty?
]
;-----------------
;- plug?()
;-----------------
plug?: func [
plug "returns true if object is based on a liquid plug"
][
all [
object? plug
in plug 'liquid
in plug 'valve
in plug 'dirty?
object? plug/valve