Skip to content

Instantly share code, notes, and snippets.

class v extends i.Component {
constructor(...e) {
super(...e),
(this._unmounted = !1),
(this._inPTR = !1),
(this._pullHeight = 0),
(this.state = {
pull: 0,
pullDistance: 0,
}),
const odiff = require("odiff");
/**
* changeText uses odiff to write a minimal string diff to an automerge.Text.
*/
const changeText = (base, newValue) => {
const diff = odiff([...base.toString()], [...newValue]);
for (const patch of diff) {
switch (patch.type) {
case "add":
const Automerge = require("@automerge/automerge");
const fs = require("fs");
const bytes = fs.readFileSync(process.argv[2]);
let doc = Automerge.load(bytes);
let msg;
let peer = Automerge.init();
let docSync = Automerge.initSyncState();
@stephen
stephen / repair.cjs
Created July 13, 2023 17:50
repair a damaged automerge doc
/* eslint-disable no-loop-func */
const Automerge = require("@automerge/automerge");
const assert = require("node:assert");
const fs = require("fs");
function verify(document, unchecked = false) {
return Automerge.load(Automerge.save(document), { unchecked });
}
const brokenDoc = process.argv[2];
@stephen
stephen / hotkeys_dialog_provider.tsx
Last active June 10, 2023 11:55
blueprint hotkeys provider without unnecessary re-renders
/*
* Copyright 2021 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
static load(token) {
return new Promise((resolve, reject) => {
this._createQuery().get(token).then((data) => {
resolve(new this(data));
}, (err) => {
if (err.code === 101) {
resolve(null); // model with token does not exist.
} else {
reject(new Error(err));
}
static createProxyFromModel(model) {
return new Proxy(model, {
get: function(target, name) {
if (name in model.definition) {
return model.get(name);
} else {
return target[name];
}
},
set: function(target, name, value) {
class Session extends BaseModel {
get definition() {
return {
user: 'string',
expirationTime: 'date'
};
}
// "instance methods"
var Session = mongoose.model('Session', {
user: String,
expirationTime: Date
});
// "instance methods"
Session.methods.expire = function() { ... };
Session.methods.renew = function(date) { ... };
// "class methods"
var session = yield Session.generate({ user: 'stephen' });
var token = session.token;
session.expirationTime = Date.now();
yield session.save();
// re-fetch
var fetchedSession = yield Session.load(token);