Skip to content

Instantly share code, notes, and snippets.

View amix's full-sized avatar

Amir Salihefendic amix

View GitHub Profile
@amix
amix / gist:9507950
Last active August 29, 2015 13:57
Observer pattern in Go - with channel specific locking
import "sync"
type Channel struct {
...
messages []*Message
listeners []chan *Message
sync.Mutex
}
...
type Account struct {
balance float64
...
sync.Mutex
}
func (a *Account) Deposit(amount float64) {
a.Lock()
defer a.Unlock()
select {
case <- messageListener:
newData = c.GetMessages(offset)
listener <- newData
case <- time.After(ChannelTimeoutSec * time.Second):
listener <- ReturnDataEmpty
}
@amix
amix / gist:c797168dd2ba56988886
Created July 21, 2014 23:08
Python gibberish
def main():
import aksfjaslkdfj
aksfjaslkdfj.blahblah
blah + blah2
blah3()
@amix
amix / highdef_canvas.coffee
Last active August 29, 2015 14:07
Configure a canvas element for high-def screens (retina)
# Configure a canvas element for high-def screens (retina)
highDefCanvas = (canvas, context) ->
pixel_ratio = window.devicePixelRatio
if pixel_ratio
width = canvas.width
height = canvas.height
canvas.width = width * pixel_ratio
canvas.height = height * pixel_ratio
canvas.style.width = "#{width}px"
canvas.style.height = "#{height}px"
def add_job(self, job_type, job_id=None, on_duplicate='overwrite',
timestamp=None, distribute=None, **data):
"""
Add a new job to execute
:param job_type: string with the name of the job
:param job_id: job identifier, if not set, then a new random unique id
will be generated
:param on_duplicate: action on identifier duplicate, can be "overwrite"
or "discard"
@amix
amix / gist:f15508ac6a8b534c3290
Created January 31, 2015 13:01
fixedlist implemented in Lua
-- List functions
--
--
LIST_DELIM = "~"
function list_add(key, value)
--- Determine the limit by inspecting if | is in the key
local limit = 200
if string.find(key, '|') then
@amix
amix / gist:2b492d7e778da9ee4231
Created February 9, 2015 22:04
Model View Controller Example (Cocoa inspired)
class ModelCounter
constructor: (@value=1) ->
increaseValue: (delta) =>
@value += delta
class ControllerCounter
constructor: (opts) ->
@amix
amix / gist:88cc073049c1aba5c6af
Created February 9, 2015 22:41
Flux + React example
# Store
class CounterStore extends EventEmitter
constructor: ->
@count = 0
@dispatchToken = @registerToDispatcher()
increaseValue: (delta) ->
@count += 1
@amix
amix / script_communicator_new.js
Created February 2, 2012 18:51
Implementation of script communication that can be used to do long polling (comet) and JSONP communication. Uses Cross-Origin Resource Sharing
// For more information about this check: http://amix.dk/blog/post/19677
ScriptCommunicator = {
sourceJavaScript: function(uri, on_success, on_error) {
var xhr = ScriptCommunicator.createCORSRequest('GET', uri);
if(xhr) {
xhr.onload = function() {
eval(xhr.responseText);