Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
c73a1fb5
Commit
c73a1fb5
authored
7 years ago
by
masarakki
Committed by
Eugen Rochko
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
reusable-streaming (#5709)
parent
f6bc6399
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/javascript/mastodon/actions/streaming.js
+8
-49
8 additions, 49 deletions
app/javascript/mastodon/actions/streaming.js
app/javascript/mastodon/stream.js
+61
-0
61 additions, 0 deletions
app/javascript/mastodon/stream.js
with
69 additions
and
49 deletions
app/javascript/mastodon/actions/streaming.js
+
8
−
49
View file @
c73a1fb5
import
create
Stream
from
'
../stream
'
;
import
{
connect
Stream
}
from
'
../stream
'
;
import
{
updateTimeline
,
deleteFromTimelines
,
...
...
@@ -12,42 +12,19 @@ import { getLocale } from '../locales';
const
{
messages
}
=
getLocale
();
export
function
connectTimelineStream
(
timelineId
,
path
,
pollingRefresh
=
null
)
{
return
(
dispatch
,
getState
)
=>
{
const
streamingAPIBaseURL
=
getState
().
getIn
([
'
meta
'
,
'
streaming_api_base_url
'
]);
const
accessToken
=
getState
().
getIn
([
'
meta
'
,
'
access_token
'
]);
const
locale
=
getState
().
getIn
([
'
meta
'
,
'
locale
'
]);
let
polling
=
null
;
const
setupPolling
=
()
=>
{
polling
=
setInterval
(()
=>
{
pollingRefresh
(
dispatch
);
},
20000
);
};
const
clearPolling
=
()
=>
{
if
(
polling
)
{
clearInterval
(
polling
);
polling
=
null
;
}
};
const
subscription
=
createStream
(
streamingAPIBaseURL
,
accessToken
,
path
,
{
connect
ed
()
{
if
(
pollingRefresh
)
{
clearPolling
();
}
return
connect
Stream
(
path
,
pollingRefresh
,
(
dispatch
,
getState
)
=>
{
const
locale
=
getState
().
getIn
([
'
meta
'
,
'
locale
'
]);
return
{
onConnect
()
{
dispatch
(
connectTimeline
(
timelineId
));
},
disconnected
()
{
if
(
pollingRefresh
)
{
setupPolling
();
}
onDisconnect
()
{
dispatch
(
disconnectTimeline
(
timelineId
));
},
r
eceive
d
(
data
)
{
onR
eceive
(
data
)
{
switch
(
data
.
event
)
{
case
'
update
'
:
dispatch
(
updateTimeline
(
timelineId
,
JSON
.
parse
(
data
.
payload
)));
...
...
@@ -60,26 +37,8 @@ export function connectTimelineStream (timelineId, path, pollingRefresh = null)
break
;
}
},
reconnected
()
{
if
(
pollingRefresh
)
{
clearPolling
();
pollingRefresh
(
dispatch
);
}
dispatch
(
connectTimeline
(
timelineId
));
},
});
const
disconnect
=
()
=>
{
if
(
subscription
)
{
subscription
.
close
();
}
clearPolling
();
};
return
disconnect
;
};
});
}
function
refreshHomeTimelineAndNotification
(
dispatch
)
{
...
...
This diff is collapsed.
Click to expand it.
app/javascript/mastodon/stream.js
+
61
−
0
View file @
c73a1fb5
import
WebSocketClient
from
'
websocket.js
'
;
export
function
connectStream
(
path
,
pollingRefresh
=
null
,
callbacks
=
()
=>
({
onConnect
()
{},
onDisconnect
()
{},
onReceive
()
{}
}))
{
return
(
dispatch
,
getState
)
=>
{
const
streamingAPIBaseURL
=
getState
().
getIn
([
'
meta
'
,
'
streaming_api_base_url
'
]);
const
accessToken
=
getState
().
getIn
([
'
meta
'
,
'
access_token
'
]);
const
{
onConnect
,
onDisconnect
,
onReceive
}
=
callbacks
(
dispatch
,
getState
);
let
polling
=
null
;
const
setupPolling
=
()
=>
{
polling
=
setInterval
(()
=>
{
pollingRefresh
(
dispatch
);
},
20000
);
};
const
clearPolling
=
()
=>
{
if
(
polling
)
{
clearInterval
(
polling
);
polling
=
null
;
}
};
const
subscription
=
getStream
(
streamingAPIBaseURL
,
accessToken
,
path
,
{
connected
()
{
if
(
pollingRefresh
)
{
clearPolling
();
}
onConnect
();
},
disconnected
()
{
if
(
pollingRefresh
)
{
setupPolling
();
}
onDisconnect
();
},
received
(
data
)
{
onReceive
(
data
);
},
reconnected
()
{
if
(
pollingRefresh
)
{
clearPolling
();
pollingRefresh
(
dispatch
);
}
onConnect
();
},
});
const
disconnect
=
()
=>
{
if
(
subscription
)
{
subscription
.
close
();
}
clearPolling
();
};
return
disconnect
;
};
}
export
default
function
getStream
(
streamingAPIBaseURL
,
accessToken
,
stream
,
{
connected
,
received
,
disconnected
,
reconnected
})
{
const
ws
=
new
WebSocketClient
(
`
${
streamingAPIBaseURL
}
/api/v1/streaming/?access_token=
${
accessToken
}
&stream=
${
stream
}
`
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment