Skip to content

Hoang Nguyen's Blog

Lazy initialize object with Proxy Pattern

Sometimes, you want to lazy intialize an object such as websocket to make it’s only connected when needed, (virtual) proxy pattern can help.

Instead of initializing directly:

const socket = new Websocket(url);

Using the Proxy:

// The proxy controls a web socket instance
class WebSocketProxy {
constructor(options) {
this.options = options;
}
connect() {
if (this.socket) {
return this.socket;
}
this.socket = new Websocket(this.options.url);
return this.socket;
}
}
const socket = new WebSocketProxy({ url: 'wss://www.example.com/socketserver' });
socket.connect();

The differences between observer, pubsub, and mediator pattern

Understanding the basic differences between the 3 patterns.

Observer: the publisher must know and manage observers, observers will be attached to the specific publisher:

const observer = function(data) {
console.log('received event');
};
Publisher.addObserver(observer);
Publisher.notify('data');

Pubsub: the publisher can publish an event without knowing who are the listeners, publisher and listeners communicate through the event channel:

const subscriber = function(data) {
console.log('received event');
};
Pubsub.on('event', subscriber);
// Publisher publishes event through Pubsub object
Pubsub.publish('event', 'data');

Mediator: groups logic of domain that has indirect relationship between modules. We can use Pubsub (not required, can use other patterns) to manage the workflow :

const Mediator = {
init: function() {
Pubsub.on('event', doSomething.bind(this));
},
doSomething: function(data) {
// do other actions, like calling Ajax and notifying another event.
Ajax.get('path', data)
.then(function(res) {
Pubsub.publish('another event', res);
});
}
};

Daily git commands

Git commands that I use everyday.

  • stage files:

    • all files: git add .
    • specific files: git add <file1> <file2>
  • unstage files:

    • all files: git reset
    • specific files: git reset <file1> <file2>
  • discard unstaged files:

    • all files: git checkout -f

    • specific files: git checkout -- <file1> <file2>

  • checkout: git checkout -b <new-branch> (-b for new branch only)

  • remove branch: git branch -d <branch-name> (-D for forcing delete branch)

  • rename branch: git branch -m <old-branch-name> <new-branch-name>

  • commit with message: git commit -m "title message" -m "description message"

  • commit and rewrite using last commit: git commit --amend --no-edit

  • push new branch: git push -u origin HEAD

  • push: git push (add --force to completely overwrite the remote branch)

  • pull: git pull

  • reset commit (rewrite history): git reset HEAD~1

  • merge: git merge <branch-name>

  • rebase: git rebase <branch-name>

  • cherry pick commit: git cherry-pick <commmit-id>

  • stash save: git stash save

  • stash apply: git stash apply

  • log:

    • normal: git log
    • details: git log -p
    • one line: git log --oneline
    • graph: git log --graph