Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
I may be slow to respond.

Michel Weststrate mweststrate

💭
I may be slow to respond.
View GitHub Profile
@mweststrate
mweststrate / index.html
Created Mar 25, 2021
electron crash v2
View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<link rel="stylesheet" type="text/css" href="./styles.css">
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
@mweststrate
mweststrate / computed-views-with-args.js
Last active May 25, 2022
React AMA - Michel Weststrate / MobX
View computed-views-with-args.js
// Parameterized computed views:
// Create computed's and store them in a cache
import { observable, computed } from "mobx"
class Todos {
@observable todos = []
private todosByUserCache = new Map()
getAllTodosByUser(userId) {
@mweststrate
mweststrate / action.js
Last active Feb 21, 2019
Decorator usage in mobx
View action.js
// case 2: manipulates the value of the descriptor, returns fresh descriptor
class MyClass {
@action someMethod() {
}
}
function action(target, key, descriptor) {
const baseValue = descriptor.value
return {
View onStopDrag.ts
onStopDrag = (id, { dx, dy }) => {
// we capture this outside the propose closure!
const baseBox = this.client.currentState.boxes.find(b => b.id === id);
this.client.propose(draft => {
const box = draft.boxes.find(b => b.id === id);
if (box.x !== baseBox.x || box.y !== baseBox.y)
throw "Somebody else already dragged this box!";
box.x += dx;
box.y += dy;
});
View server.ts
export interface Change {
base: string;
changeId: string;
patches: Patch[];
}
export class Server {
clients: Client[] = [];
@mweststrate
mweststrate / onReceive.ts
Last active Aug 31, 2018
Rebase actions after receiving changes by the server
View onReceive.ts
onReceive(change: Change) {
this.confirmedState = {
id: change.changeId,
state: applyPatches(this.confirmedState.state, change.patches)
};
// reapply all the pending actions, filter out the ones that can't be applied anymore
const { pendingActions } = this;
// reset the pending changes collection. We will fill it again by replaying all actions.
this.pendingActions = [];
this.currentState = this.confirmedState.state;
@mweststrate
mweststrate / distributeFirstChange.ts
Created Aug 31, 2018
Sending changes to the server
View distributeFirstChange.ts
async distributeFirstChange() {
const p = this.pendingActions[0];
const response = await this.server.propose(this, {
base: this.confirmedState.id,
patches: p.patches,
changeId: p.id
});
if (response === "NOPE") {
console.warn(this.name + ": Change rejected; outdated");
// we keep the actions for now, and we will try to replay them after receiving the next change
View Client-state.ts
interface PendingAction {
id: string;
fn: (draft: any) => void;
patches: Patch[];
}
export class Client {
pendingActions: PendingAction[] = [];
confirmedState!: {
id: string;
View onStopDrag.js
onStopDrag = (id, { dx, dy }) => {
// dx and dy are captured in the closure!
this.client.propose(draft => {
const box = draft.boxes.find(b => b.id === id);
box.x += dx;
box.y += dy;
if (box.x < 0 || box.x > 400 || box.y < 0 || box.y > 400)
// throwing here is safe; as original state will be untouched.
// This can happen when actions are being replayed
throw "Invalid drop coordinates: " + JSON.stringify(box);
@mweststrate
mweststrate / a_old-react-api.js
Last active Jul 21, 2018
React api difference
View a_old-react-api.js
// Example component: render the current value of a stream. Support switching streams during the lifecycle of the component
// N.B. code is untested
// Old:
class RenderStream extends Component {
subscription
componentWillMount() {
this.subscribeToStream(this.props.stream)
}