Skip to content

Instantly share code, notes, and snippets.

View olivier-spinelli's full-sized avatar

Olivier Spinelli olivier-spinelli

View GitHub Profile
@olivier-spinelli
olivier-spinelli / KindOfSoftBreakpoint.cs
Created October 1, 2023 07:52
A "breakpoint" that let other threads run.
// Implemented as an extension method (on IMonitorTestHelper that provides logging) but this
// dependency can easily ve removed.
public static class MonitorTestHelperExtension
{
sealed class Resumer
{
internal readonly TaskCompletionSource _tcs;
readonly Timer _timer;
readonly Func<bool, bool> _resume;
bool _reentrant;
@olivier-spinelli
olivier-spinelli / gist:029251012c7ca671cc0bd841af60bfd4
Created September 29, 2023 08:21
TypeScript nominal typing for class
/*
// We need an index to generate the "brand" field.
// This correctly handles the inheritance.
class Duck {
private _0: undefined;
constructor(public name: string) {
}
}
class Dog extends Duck {
@olivier-spinelli
olivier-spinelli / gist:7785f4f2af33aa8193729f6abf9ee17a
Last active December 11, 2022 14:21
How to use a Utf8JsonReader on a Stream.
/// <summary>
/// Provides a reading buffer on a Utf8 stream of bytes.
/// The Utf8 Byte Order Mask (BOM: 0xEF, 0xBB, 0xBF) that may exist at the start is
/// kindly handled.
/// <para>
/// The buffer comes from the <see cref="ArrayPool{T}.Shared"/> of bytes: this
/// reader MUST be disposed to return the current buffer to the pool.
/// </para>
/// <para>
/// The pattern to use it is to replace all <c>Read()</c> calls with:
@olivier-spinelli
olivier-spinelli / TcpEchoServer.cs
Created July 29, 2022 09:44
A minimalist local echo server.
class EchoTcpServer
{
readonly int _port;
public EchoTcpServer( int port )
{
_port = port;
}
public async Task<Exception?> Run( CancellationToken cancel )
using System;
// notnull constraint cannot handle both in and Nullable<T>.
public interface IWriter<T> where T : notnull
{
void Write( in T o );
void WriteNullable( in T? o );
}
public class ValueTypeWriter<T> : IWriter<T> where T : struct
{
using System;
using System.Runtime.CompilerServices;
public ref struct ROSpanMatcher
{
public ReadOnlySpan<char> Head;
public int _le;
public ROSpanMatcher( ReadOnlySpan<char> text )
{
Head = text;
function memorize( f ) { return f; }
function MyObject( name ) {
this.name = name;
}
MyObject.prototype.show = function( x, y ) {
console.log( this.name, x, y );
}
var o1 = new MyObject( 'o1' );
var o2 = new MyObject( 'o2' );
function Animal( objectName ) {
this._name = objectName;
}
Animal.prototype._name = "Animal’s prototype.";
Animal.prototype.makeNoise = function () { console.log( this._name + ' is silent.' ); };
Animal.prototype.run = function () { console.log( this._name + ' runs.' ); };
var a = new Animal( "Basic Animal." );
a.makeNoise();
@olivier-spinelli
olivier-spinelli / CallingCommandTests.cs
Last active August 8, 2016 13:43
Command/Result pattern for Sql Server with CK-Database
[TestFixture]
public class CommandDemoTests
{
[Test]
public async Task command_pattern_just_work_with_poco_result()
{
var cmd = new CmdDemo()
{
ActorId = 878,
CompanyName = "Invenietis",
if OBJECT_ID( 'EG.TestWithTable' ) is not null drop procedure EG.TestWithTable;
if TYPE_ID( 'EG.TestWithTableData' ) is not null drop type EG.TestWithTableData;
GO
create type EG.TestWithTableData as table
(
Value int,
Name nvarchar(3),
Doc xml,
Point Geometry,
CName as Name + ' (' + cast(Value as nvarchar) + ')',