lively 0.17.0 → 0.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/context/getting-started.md +74 -0
- data/lib/lively/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c0dda7cfa299aaa995ebe540f947ab24a4f2d0f3e8dab116e064f0af1d545075
|
|
4
|
+
data.tar.gz: 1ea180ab2040f3b5b7ae191fa4f731376e366b7cf658de422d2289d929078de6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d2bc8606510afc7f5dd935bd46f3c2bb7613b9ba8a3301cb290f27b50aa5c14b65c670e09bf5a109fd41e0688542169997a7200295c64bfa9378ecf2bd685c9d
|
|
7
|
+
data.tar.gz: 327e99802f36ec9f354e5ad4195850e0e0ee7bfae7a9372b38f4442841500e7e94d80a13869096797e093da5ba8f5d37f04811dd801e854fb3f10e83127d9866
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/context/getting-started.md
CHANGED
|
@@ -62,6 +62,80 @@ $ ./application.rb
|
|
|
62
62
|
|
|
63
63
|
You should see "Hello World!" displayed in your browser.
|
|
64
64
|
|
|
65
|
+
## Shared State
|
|
66
|
+
|
|
67
|
+
When multiple browser windows need to share state — for example, a multiplayer game or a collaborative tool — you can pass shared state to `Application[]`. The state is passed as keyword arguments to all views, both during the initial page render and when browsers reconnect via WebSocket.
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
#!/usr/bin/env lively
|
|
71
|
+
|
|
72
|
+
class GameState
|
|
73
|
+
def initialize
|
|
74
|
+
@players = []
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
attr :players
|
|
78
|
+
|
|
79
|
+
def add_player(player)
|
|
80
|
+
@players << player
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
class GameView < Live::View
|
|
85
|
+
def initialize(id = self.class.unique_id, data = {}, game_state: nil)
|
|
86
|
+
super(id, data)
|
|
87
|
+
@game_state = game_state
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def render(builder)
|
|
91
|
+
builder.tag(:p) do
|
|
92
|
+
builder.text("Players: #{@game_state.players.length}")
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
Application = Lively::Application[GameView, game_state: GameState.new]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The `game_state:` keyword is passed to every `GameView` instance — whether created by the initial page load or by a WebSocket reconnection. This means all connected browsers share the same `GameState` object.
|
|
101
|
+
|
|
102
|
+
For more complex applications, subclass {ruby Lively::Application} and override `#state`, `#allowed_views`, and `#body`:
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
class Application < Lively::Application
|
|
106
|
+
def allowed_views
|
|
107
|
+
[DisplayView, ControlView]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def state
|
|
111
|
+
{controller: @controller}
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def initialize(delegate)
|
|
115
|
+
@controller = MyController.new
|
|
116
|
+
super
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def body(request)
|
|
120
|
+
case request.path
|
|
121
|
+
when "/"
|
|
122
|
+
DisplayView.new(**state)
|
|
123
|
+
when "/control"
|
|
124
|
+
ControlView.new(**state)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def handle(request)
|
|
129
|
+
if body = self.body(request)
|
|
130
|
+
page = Lively::Pages::Index.new(title: "My App", body: body)
|
|
131
|
+
Protocol::HTTP::Response[200, [], [page.call]]
|
|
132
|
+
else
|
|
133
|
+
Protocol::HTTP::Response[404, [], ["Not Found"]]
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
```
|
|
138
|
+
|
|
65
139
|
## Live Reloading
|
|
66
140
|
|
|
67
141
|
To enable live reloading, add the `io-watch` gem to your `gems.rb` file:
|
data/lib/lively/version.rb
CHANGED
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
|
Binary file
|