Class: Mbeditor::EditorChannel
- Inherits:
-
ActionCable::Channel::Base
- Object
- ActionCable::Channel::Base
- Mbeditor::EditorChannel
- Defined in:
- app/channels/mbeditor/editor_channel.rb
Constant Summary collapse
- STATE_MAX_BYTES =
1 * 1024 * 1024
- SAFE_BRANCH_NAME =
/\A[a-zA-Z0-9._\-\/]+\z/
Instance Method Summary collapse
-
#save_branch_state(data) ⇒ Object
Called via WebSocketService.perform(‘save_branch_state’, { branch: …, state: … }).
-
#save_state(data) ⇒ Object
Called via WebSocketService.perform(‘save_state’, { state: … }).
- #subscribed ⇒ Object
- #unsubscribed ⇒ Object
Instance Method Details
#save_branch_state(data) ⇒ Object
Called via WebSocketService.perform(‘save_branch_state’, { branch: …, state: … })
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/channels/mbeditor/editor_channel.rb', line 40 def save_branch_state(data) Rails.logger.silence do branch = data["branch"].to_s.strip return unless branch.match?(SAFE_BRANCH_NAME) state_data = data["state"] payload_json = state_data.to_json return if payload_json.bytesize > STATE_MAX_BYTES root = workspace_root path = root.join("tmp", "mbeditor_branch_states.json") FileUtils.mkdir_p(root.join("tmp")) File.open(path, File::RDWR | File::CREAT) do |f| f.flock(File::LOCK_EX) existing = f.size > 0 ? JSON.parse(f.read) : {} existing[branch] = state_data f.truncate(0) f.rewind f.write(existing.to_json) end end rescue StandardError # Never let a state-save failure crash the WebSocket connection end |
#save_state(data) ⇒ Object
Called via WebSocketService.perform(‘save_state’, { state: … })
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/channels/mbeditor/editor_channel.rb', line 20 def save_state(data) Rails.logger.silence do payload = (data["state"] || data).to_json return if payload.bytesize > STATE_MAX_BYTES root = workspace_root path = root.join("tmp", "mbeditor_workspace.json") FileUtils.mkdir_p(root.join("tmp")) File.open(path, File::RDWR | File::CREAT) do |f| f.flock(File::LOCK_EX) f.truncate(0) f.rewind f.write(payload) end end rescue StandardError # Never let a state-save failure crash the WebSocket connection end |
#subscribed ⇒ Object
11 12 13 |
# File 'app/channels/mbeditor/editor_channel.rb', line 11 def subscribed stream_from "mbeditor_editor" end |
#unsubscribed ⇒ Object
15 16 17 |
# File 'app/channels/mbeditor/editor_channel.rb', line 15 def unsubscribed # no-op end |