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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d41e94774f23353b426f53e23eb343783826c244d9bbad793759e5eea9fcd534
4
- data.tar.gz: c2c5b21df4f25117d12e8c8af2e8c17113489346087cdd80a2d5b6b0dbd920d9
3
+ metadata.gz: c0dda7cfa299aaa995ebe540f947ab24a4f2d0f3e8dab116e064f0af1d545075
4
+ data.tar.gz: 1ea180ab2040f3b5b7ae191fa4f731376e366b7cf658de422d2289d929078de6
5
5
  SHA512:
6
- metadata.gz: 0445eecaf41965ab7aea620f3d2886da5b2e76a5850678fcb30b60e0410aafe6d5f7d4180348564f43833b07476f2bacac00dc447a38229528ff8884ad67374e
7
- data.tar.gz: f75a5afa75d4d98ad71c4a014c00ffec9b62368d1995eafe67265c05ee72e0cfd43afcc9a43374dda74b2261753348c0b34adfe672bc9d09f3566899f661e69f
6
+ metadata.gz: d2bc8606510afc7f5dd935bd46f3c2bb7613b9ba8a3301cb290f27b50aa5c14b65c670e09bf5a109fd41e0688542169997a7200295c64bfa9378ecf2bd685c9d
7
+ data.tar.gz: 327e99802f36ec9f354e5ad4195850e0e0ee7bfae7a9372b38f4442841500e7e94d80a13869096797e093da5ba8f5d37f04811dd801e854fb3f10e83127d9866
checksums.yaml.gz.sig CHANGED
Binary file
@@ -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:
@@ -5,5 +5,5 @@
5
5
 
6
6
  # @namespace
7
7
  module Lively
8
- VERSION = "0.17.0"
8
+ VERSION = "0.17.1"
9
9
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lively
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
metadata.gz.sig CHANGED
Binary file