Skip to content

Instantly share code, notes, and snippets.

View noseratio's full-sized avatar

Andrew Nosenko noseratio

View GitHub Profile
@noseratio
noseratio / EventHub.cs
Last active July 16, 2021 12:10
Simple pub/sub event hub implementation
#nullable enable
using System;
namespace conapp
{
/// <summary>
/// Resembling JavaScript's EventTarget
/// </summary>
public class EventTarget<T> where T : EventArgs
{
@noseratio
noseratio / ConsoleApp.cs
Created July 9, 2021 11:37
A simple console loop
using System;
using System.Threading;
using System.Threading.Tasks;
namespace App
{
class Program
{
static async Task<int> Main()
{
@noseratio
noseratio / deferredTest.mjs
Last active July 6, 2021 02:21
Timing various Deferred implementations
void async function() {
// DeferredAsClass
class DeferredAsClass {
resolve;
reject;
promise = new Promise((s, f) => { this.resolve = s; this.reject = f; });
}
// DeferredAsClassWithConstructor
void async function() {
"use strict";
// extending Promise
class PromiseEx extends Promise {
}
const iterations = 500_000;
const cached = Promise.resolve();
@noseratio
noseratio / Deferred.mjs
Created July 1, 2021 05:00
Deriving from Promise class can slow down things
void async function() {
// extending Promise
class PromiseEx extends Promise {
}
// extending Promise as Deferred
class Deferred extends Promise {
resolve;
reject;
@noseratio
noseratio / createDeferred.mjs
Last active June 29, 2021 07:00
Exposing a "thenable" can be ~2 times slower than exposing a promise directly
void async function() {
function createDeferred() {
let resolve, reject;
const promise = new Promise((...args) => [resolve, reject] = args);
return Object.freeze({
resolve,
reject,
promise,
then: promise.then.bind(promise)
});
@noseratio
noseratio / csharp-repl.ps1
Created June 17, 2021 01:08
C# REPL with Microsoft.Net.Compilers.Toolset
# https://www.nuget.org/packages/Microsoft.Net.Compilers.Toolset
md cs-repl | cd
nuget install Microsoft.Net.Compilers.Toolset
# run a CS script, a useful alternative to PowerShell itself
echo 'Console.WriteLine("hello");' > script.csx
. (gci -recurse csi.exe).FullName script.csx
# run in REPL mode
@noseratio
noseratio / delay.cs
Created June 16, 2021 01:51
Trying try.dot.net
using System;
using System.Threading.Tasks;
public class Program
{
public async static Task Main()
{
await Task.Delay(1000);
Console.WriteLine("Hello after a delay!");
}
@noseratio
noseratio / WebView2AppKeys.csproj
Created June 9, 2021 11:30
Hybrid Console + WinForms + AspNetCore app
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<OutputType>Exe</OutputType>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
<UseWinForms>true</UseWinForms>
<UsingMicrosoftNETSdkWeb>true</UsingMicrosoftNETSdkWeb>
@noseratio
noseratio / asyncInterpolated.mjs
Created June 2, 2021 07:49
Awaiting inside JavaScript template strings
// https://dev.to/noseratio/await-inside-javascript-template-strings-3kbj
//
// save this as .mjs or as "node --input-type=module < file.js"
const delay = (ms, result) => new Promise(r => setTimeout(r, ms, result));
const interpolated = `Hello, ${await delay(1000, "World!")} Is this sync or async?`;
console.log(interpolated)