Skip to content

Instantly share code, notes, and snippets.

View victorjonsson's full-sized avatar
🎯
Focusing

Victor Jonsson victorjonsson

🎯
Focusing
View GitHub Profile
@victorjonsson
victorjonsson / StateMachine.cs
Last active October 6, 2024 07:46
Thread safe and generic state machine, that can be made infinite and guards against invalid transisitions
namespace TestingStuff
{
/// <summary>
/// Thread safe and generic state machine, that can be made infinite and guards against invalid state transisitions
///
/// public enum MyStates {Init, Running, Stopped}
/// var finiteStateMachine = new StateMachine<MyStates>();
///
/// // Infinite state machine that can go back to running or init state once stopped
/// var infiniteStateMachine = new StateMachine<MyStates>(new Transition[] {
@victorjonsson
victorjonsson / BackgroundTaskRunner.cs
Last active September 9, 2024 19:12
Syntactic sugar for handling async processes in the background, when you don't need or want to await the results
namespace TestingStuff.Tasks
{
/// <summary>
/// Syntactic sugar for handling running async processes in the background, without await
/// </summary>
/// <example>
/// <code>
/// BackgroundTaskRunner.RunOnce(() => { });
/// BackgroundTaskRunner.Run(async () => { }).EveryMinute();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body class="p-5" onload="renderLog(document.getElementById('the-log').value)">
document.write(
[...document.querySelectorAll('img')]
.filter(node => node.src?.indexOf('qa.jollyroom.se') > -1)
.map(node => node.src.split('?')[0].replace('qa.jollyroom.se', 'qa.jollyroom.shimmercat.cloud'))
.map(shimmerCatSrc => '<img src="'+shimmerCatSrc+'">').join('')
);
var decision = new NavetDecisionBasis()
{
Person = new NavetPerson()
{
Contact = new NavetPersonContact()
{
CellPhone = "",
City = "Umea",
Country = "Sweden",
// Turn our form data into a JSON string
const data ={
...
data: {
...
integrations: [
{type: 'integrationX' ... },
{type: 'integrationY' ... }
]
}
public SomethingController([FromBody] MyDataModel model) {
try {
DoTheStuff(model);
} catch (SpecificExceptionIKnowCanHappen exc) {
return BadRequest(new { errorCode = ErrorCode });
}
// all other exceptions will let the app crash, exception info will be sen to log and app returns status 500 to the client
return Ok();
}
// Controller exempel
public class AccountController : Controller
{
public async Task<IActionResult> kollaStatusPåBankIdInloggningen(string reference) {
// ... massa saker händer...
AuthResult result = authClient.AuthorizeReference(...)
@victorjonsson
victorjonsson / statement-validator.js
Last active May 11, 2017 10:28
statement-validator.js
$.formUtils.addValidator({
name: 'statement',
validatorFunction : function(value, $elem, conf, language, $form, eventContext) {
var statementIsValid = true,
$resultTree = $('<result><node></node></result>'),
$current = $resultTree.children().eq(0),
invokeNext = true;
$.each(value.replace(/\s+/g,' ').split(' '), function (i, str) {
if (str == '||') {
invokeNext = true;
@victorjonsson
victorjonsson / fsm.php
Last active November 11, 2016 15:22
fsm.php
<?php
interface State {
public function getState();
}
abstract class StateAbstract implements State {
const STATE_INIT = 'STATE_INIT';