Skip to content

Instantly share code, notes, and snippets.

View sb8244's full-sized avatar

Stephen Bussey sb8244

View GitHub Profile
public class ImmutableCounter {
private final int counter;
public ImmutableCounter(int num) {
this.counter = num;
}
public int getCount() {
return this.counter;
}
@sb8244
sb8244 / ImmutableDemo.java
Created September 12, 2023 05:17
Immutability Lecture (Activity 1)
import java.util.HashMap;
// This class is broken, follow the TODOs
public class ImmutableDemo {
// TODO: How to prevent external actors from accessing the map directly?
public HashMap<String,String> map;
public ImmutableDemo() {
this.map = new HashMap<String, String>();
this.map.put("Important", "OKAY");
defmodule Super.RepoTest do
use Super.DataCase, async: true
require Logger
@skipped_schemas [UserService.User]
defp tenant_schemas do
{:ok, mods} = :application.get_key(:super, :modules)
Enum.map(mods, fn mod ->
var links = document.getElementsByTagName('a');
var params = new URLSearchParams(window.location.search);
var utmParams = {};
params.forEach(function (val, name) {
if (name.startsWith('utm_')) {
utmParams[name] = val;
}
});
export const SpaceLayoutEditor = {
// Not important for the integration, just something for my specific use case
recentItems: [],
mounted() {
// IMPORTANT LINE: LiveView -> React data flow
this.handleEvent("react.update_items", ({ items }: { items: string }) => {
const newItems: SpaceItem[] = JSON.parse(items)
this.mountEditor(newItems)
})
@sb8244
sb8244 / 1_hooks.ts
Last active April 13, 2022 17:52
Example hooks + React mounter for LiveView application
export const SpaceLayoutEditor = {
mounted() {
// I use webpack chunks to reduce LiveView entry file size
import(/* webpackChunkName: "space-layout-editor-lv" */ '../entry/space-layout-editor-lv').then((mounter) => {
this.unmountComponent = mounter.default(this.el.id, {
editorOpts: this.editorOpts()
})
}).catch(console.error)
},
/**
* This example is just copy/pasted from my code base. The gist of it is that `this.pushEventTo` on the hook
* will send an event over the LiveView channel that is processed by the component/LiveView that's mounted at
* that element.
*
* I recommend using pushEventTo instead of pushEvent because I've run into situations where the event gets sent
* to the incorrect component or LiveView.
*/
const GeneratePDFButton = {
mounted() {
@sb8244
sb8244 / notion.ex
Created December 22, 2021 07:35
Example of fetching pages and blocks from Notion's public API in Elixir
defmodule Clove.Connections.Client.Notion do
# There is not a way to filter last_edited_time, so consumer should check if it needs to fetch the blocks or not
# This will help for deletion detection, though
def retrieve_pages(connection) do
request = %{
method: :post,
url: "#{base_url()}/v1/search",
params: %{
"query" => "",
"sort" => %{
@sb8244
sb8244 / header.html
Last active February 18, 2022 21:42
Clove IFrame Embed Code — View more at https://hub.clovecx.com/hc/a/embed-your-customer-hub
<script type="text/javascript">
if (window.top !== window.self) {
document.body.classList.add('in-iframe')
}
</script>
@sb8244
sb8244 / chartkick.ex
Created May 16, 2021 16:10
This allows chartkick to be embedded in LiveViews
defmodule Chartkick do
@moduledoc """
Adapted from https://github.com/buren/chartkick-ex/blob/master/lib/chartkick.ex to work with LiveView.
Works in conjunction with Phoenix LiveView hooks to render charts using Chartkick.js library.
"""
require EEx
gen_chart_fn = fn chart_type ->