-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement Virus property It allows the developer to make custom transformations to the genotype before being parsed by Cell * (wip) Fail when a mutation does not adhere to the API * Add feature as a plugin mechanism * Add type by default after virus infection This fixes the problem that the node was generated with a type but the virus tried to override it to a div implicitly * Fix missing semicolon * Move virus infection up in the pipeline Infecting in Genotype.build may be too late, as the $node is already generated. This caused issues when writing viruses that changed the $type of cells initially declared with "$type: 'input'". This is also cleaner, as viruses act like dumb pre-processors or macro expansions. * Add Virus documentation * Update VIRUS.md
- Loading branch information
1 parent
cdb2fa8
commit 6ed18a0
Showing
5 changed files
with
252 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Virus | ||
|
||
## Concept | ||
Viruses are a plug-in system that makes it able to include custom transformations to the cells, extending them as far as wanted. | ||
|
||
To use it, you should "infect" a Gene with a Virus, which is a function that takes a Gene-like object and returns another Gene-like object. This returned object _could_ be a real Gene or an object to be transformed by another Virus (pretty much like a Pipeline). | ||
|
||
## Examples | ||
The following virus propagates the $update method down the inheritance tree when a variable is changed: | ||
|
||
``` | ||
function update_propagating_virus(component){ | ||
let recursive_update = (node) => { | ||
for(let n of node.children){ | ||
n.$update && n.$update() | ||
recursive_update(n) | ||
} | ||
} | ||
let old_update = component.$update | ||
component.$update = function(){ | ||
old_update && old_update.call(this) | ||
recursive_update(this) | ||
} | ||
return component | ||
} | ||
window.c = { | ||
$cell: true, | ||
$type: 'ul', | ||
_name: '', | ||
$virus: update_propagating_virus, | ||
$components: [ | ||
{ $type: 'li', | ||
$components: [ | ||
{ $type: 'p', | ||
$text: '', | ||
$update: function(){ | ||
this.$text = this._name; | ||
} | ||
}, | ||
{ $type: 'p', $text: 'other' } | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
As the virus can be any function that expects a component, you can also send other arguments to it. | ||
|
||
The following example shows how to make a virus to call a method every X seconds: | ||
|
||
``` | ||
var Tickable = function(timer, trigger){ | ||
return function(gene) { | ||
gene.$init = function() { | ||
var self = this; | ||
setInterval(function() { | ||
self[trigger](); | ||
}, timer); | ||
} | ||
return gene; | ||
} | ||
} | ||
... | ||
{ | ||
$virus: [Tickable(500, '_mutate')] | ||
} | ||
``` | ||
|
||
Refer to the [tests](test/integration.js) for more examples and details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters