-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Sticky components experiment * Call will_mount before render * Differed init + update + lifecycle 'hooks' (#71) * Differred init + update + lifecycle 'hooks' * make initialize call update by default * Remove callbacks per Dr. Ian Malcolm * Swap back to the render class method This commit also adds a warning if you use .new instead of .render * Make sure to always render when using instances * Add sticky wrapper * Switch to MemoizedComponent * Fix spec by passing a real Renderable * Remove test knowledge of BlackBoxNode::Renderable * Remove a bunch of WIP experiments
- Loading branch information
Showing
6 changed files
with
93 additions
and
7 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
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 |
---|---|---|
|
@@ -13,7 +13,6 @@ def should_render? _ | |
end | ||
|
||
def key | ||
`undefined` | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'clearwater/component' | ||
require 'clearwater/black_box_node' | ||
|
||
module Clearwater | ||
class MemoizedComponent | ||
include Clearwater::Component | ||
|
||
def self.memoize *args, &block | ||
Placeholder.new(self, args, block) | ||
end | ||
|
||
def self.[] key | ||
memoize[key] | ||
end | ||
|
||
def update | ||
end | ||
|
||
def destroy | ||
end | ||
|
||
class Placeholder | ||
include Clearwater::BlackBoxNode | ||
|
||
attr_reader :klass, :key, :vdom | ||
|
||
def initialize klass, args, block | ||
@klass = klass | ||
@args = args | ||
@block = block | ||
end | ||
|
||
def memoize *args, &block | ||
initialize @klass, args, block | ||
self | ||
end | ||
|
||
def [] key | ||
@key = key.to_s | ||
self | ||
end | ||
|
||
def component | ||
@component ||= @klass.new(*@args, &@block) | ||
end | ||
|
||
def node | ||
@node ||= Clearwater::Component.sanitize_content(component) | ||
end | ||
|
||
def mount element | ||
@vdom = VirtualDOM::Document.new(element) | ||
|
||
# TODO: add a public interface to generate a pre-initialized VDOM::Doc | ||
`#@vdom.tree = #{element.to_n}` | ||
`#@vdom.node = #{node}` | ||
`#@vdom.rendered = true` | ||
end | ||
|
||
def update previous | ||
@vdom = previous.vdom | ||
@component = previous.component | ||
|
||
if component.should_update?(*@args, &@block) | ||
component.update(*@args, &@block) | ||
@vdom.render component.render | ||
end | ||
end | ||
|
||
def unmount | ||
component.destroy | ||
end | ||
end | ||
end | ||
end |
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