Skip to content

Commit

Permalink
-cleaned up nuget packages
Browse files Browse the repository at this point in the history
-adding scrolling ticker
-changed StockTicker.Instance to use Lazy<T>
  • Loading branch information
DamianEdwards committed Nov 10, 2011
1 parent 5623086 commit ee4b759
Show file tree
Hide file tree
Showing 35 changed files with 146 additions and 23,746 deletions.
Binary file added SignalR.StockTicker/.nuget/NuGet.exe
Binary file not shown.
52 changes: 52 additions & 0 deletions SignalR.StockTicker/.nuget/NuGet.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
<NuGetExePath>$(NuGetToolsPath)\nuget.exe</NuGetExePath>
<PackagesConfig>$(ProjectDir)packages.config</PackagesConfig>
<PackagesDir>$(SolutionDir)packages</PackagesDir>
<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>

<!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
<PackageSources>""</PackageSources>

<!-- Enable the restore command to run before builds -->
<RestorePackages Condition="$(RestorePackages) == ''">false</RestorePackages>

<!-- Property that enables building a package from a project -->
<BuildPackage Condition="$(BuildPackage) == ''">false</BuildPackage>

<!-- Commands -->
<RestoreCommand>"$(NuGetExePath)" install "$(PackagesConfig)" -source $(PackageSources) -o "$(PackagesDir)"</RestoreCommand>
<BuildCommand>"$(NuGetExePath)" pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand>

<!-- Make the build depend on restore packages -->
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
RestorePackages;
$(BuildDependsOn);
</BuildDependsOn>

<!-- Make the build depend on restore packages -->
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
$(BuildDependsOn);
BuildPackage;
</BuildDependsOn>
</PropertyGroup>

<Target Name="CheckPrerequisites">
<!-- Raise an error if we're unable to locate nuget.exe -->
<Error Condition="!Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
</Target>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(RestoreCommand)"
LogStandardErrorAsError="true"
Condition="Exists('$(PackagesConfig)')" />
</Target>

<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(BuildCommand)"
LogStandardErrorAsError="true" />
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<AssemblyName>SignalR.StockTicker</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<UseIISExpress>true</UseIISExpress>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -115,6 +117,7 @@
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,57 @@ if (!String.prototype.supplant) {

jQuery.fn.flash = function (color, duration) {
var current = this.css('backgroundColor');
this.animate({ backgroundColor: "rgb(" + color + ")" }, duration / 2);
this.animate({ backgroundColor: current }, duration / 2);
this.animate({ backgroundColor: "rgb(" + color + ")" }, duration / 2)
.animate({ backgroundColor: current }, duration / 2);
}

$(function () {

var ticker = $.connection.stockTicker,
$stockTable = $('#stockTable'),
$stockTableBody = $stockTable.find('tbody'),
rowTemplate = '<tr data-symbol="{Symbol}"><td>{Symbol}</td><td>{Price}</td><td>{DayOpen}</td><td>{DayHigh}</td><td>{DayLow}</td><td>{PercentChange}</td></tr>';
rowTemplate = '<tr data-symbol="{Symbol}"><td>{Symbol}</td><td>{Price}</td><td>{DayOpen}</td><td>{DayHigh}</td><td>{DayLow}</td><td>{PercentChange}</td></tr>',
$stockTicker = $('#stockTicker'),
$stockTickerUl = $stockTicker.find('ul'),
liTemplate = '<li data-symbol="{Symbol}"><span class="symbol">{Symbol}</span> <span class="price">{Price}</span> <span class="change">({PercentChange})</span></li>';

function formatStock(stock) {
return $.extend(stock, {
Price: stock.Price.toFixed(2),
PercentChange: (stock.PercentChange * 100).toFixed(2) + '%'
});
}

ticker.updateStockPrice = function (stock) {
var $row = $(rowTemplate.supplant(stock));
var displayStock = formatStock(stock),
$row = $(rowTemplate.supplant(displayStock)),
$li = $(liTemplate.supplant(displayStock));
$stockTableBody.find('tr[data-symbol=' + stock.Symbol + ']')
.replaceWith($row);
$stockTickerUl.find('li[data-symbol=' + stock.Symbol + ']')
.replaceWith($li);
// TODO: Make it flash red/green depending on whether it went down/up
$row.flash('255,255,0', 1000);
$li.flash('255,255,0', 1000);
};

function scrollTicker() {
var w = $stockTickerUl.width();
$stockTickerUl.css({ marginLeft: w });
$stockTickerUl.animate({ marginLeft: -w }, 15000, 'linear', scrollTicker);
}

$.connection.hub.start(function () {
ticker.getAllStocks()
.done(function (stocks) {
$stockTableBody.empty();
$.each(stocks, function () {
$stockTableBody.append(rowTemplate.supplant(this));
var stock = formatStock(this);
$stockTableBody.append(rowTemplate.supplant(stock));
$stockTickerUl.append(liTemplate.supplant(stock));
});

scrollTicker();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public decimal Price
set
{
_price = value;
if (_price < DayLow)
if (_price < DayLow || DayLow == 0)
{
DayLow = Price;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,42 @@
border-collapse: collapse;
}

#stockTable table th,
#stockTable table td {
#stockTable table th, #stockTable table td {
padding: 2px 6px;
}

#stockTable table td {
text-align: right;
}

#stockTicker {
overflow: hidden;
width: 370px;
height: 24px;
border: 1px solid #999;
}

#stockTicker .inner {
width: 9999px;
}

#stockTicker ul {
display: inline-block;
list-style-type: none;
margin: 0;
padding: 0;
}

#stockTicker li {
display: inline;
margin-right: 8px;
}

/*<li data-symbol="{Symbol}"><span class="symbol">{Symbol}</span><span class="price">{Price}</span><span class="change">{PercentChange}</span></li>*/
#stockTicker .symbol {
font-weight: bold;
}

#stockTicker .change {
font-style: italic;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ namespace SignalR.StockTicker.SignalR.StockTicker
{
public class StockTicker
{
private readonly static object _instanceLock = new object();
private static StockTicker _instance;
public readonly static Lazy<StockTicker> _instance = new Lazy<StockTicker>(() => new StockTicker());
private readonly Dictionary<string, Stock> _stocks = new Dictionary<string, Stock>();
private readonly double _rangePercent = .01;
private readonly int _updateInterval = 5000;
private readonly double _rangePercent = .008;
private readonly int _updateInterval = 500; //ms
private Timer _timer;
private readonly object _updateStockPricesLock = new object();
private bool _updatingStockPrices = false;
Expand All @@ -29,17 +28,7 @@ public static StockTicker Instance
{
get
{
if (_instance == null)
{
lock (_instanceLock)
{
if (_instance == null)
{
_instance = new StockTicker();
}
}
}
return _instance;
return _instance.Value;
}
}

Expand All @@ -52,9 +41,9 @@ private void LoadDefaultStocks()
{
new List<Stock>
{
new Stock { Symbol = "MSFT", Price = 26.31m, DayOpen = 26.34m, DayHigh = 26.84m, DayLow = 26.28m },
new Stock { Symbol = "APPL", Price = 404.18m, DayOpen = 400.06m, DayHigh = 404.18m, DayLow = 395.62m },
new Stock { Symbol = "GOOG", Price = 596.30m, DayOpen = 598.40m, DayHigh = 608.97m, DayLow = 593.87m }
new Stock { Symbol = "MSFT", Price = 26.31m, DayOpen = 26.31m },
new Stock { Symbol = "APPL", Price = 404.18m, DayOpen = 404.18m },
new Stock { Symbol = "GOOG", Price = 596.30m, DayOpen = 596.30m }
}.ForEach(stock => _stocks.Add(stock.Symbol, stock));
}

Expand Down Expand Up @@ -102,16 +91,16 @@ private bool UpdateStockPrice(Stock stock)
{
// Randomly choose whether to udpate this stock or not
var r = _updateOrNotRandom.NextDouble();
if (r > .25)
if (r > .1)
{
return false;
}

// Update the stock price by a random factor of the range percent
var random = new Random((int)Math.Floor(stock.Price));
var percentChange = random.NextDouble() * _rangePercent;
var pos = random.NextDouble() > .5;
var change = Math.Round(stock.Price * (decimal)percentChange, 3);
var pos = random.NextDouble() > .4;
var change = Math.Round(stock.Price * (decimal)percentChange, 2);
change = pos ? change : -change;

stock.Price += change;
Expand All @@ -123,5 +112,13 @@ private void BroadcastStockPrice(Stock stock)
var clients = Hub.GetClients<StockTickerHub>();
clients.updateStockPrice(stock);
}

~StockTicker()
{
if (_timer != null)
{
_timer.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ <h2>Live Stock Table</h2>
</div>

<h2>Live Stock Ticker</h2>
<div id="stockTicker"></div>
<div id="stockTicker">
<div class="inner">
<ul></ul>
</div>
</div>

<script src="../Scripts/jquery-1.6.4.js"></script>
<script src="../Scripts/jquery.signalR.js"></script>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit ee4b759

Please sign in to comment.