Skip to content

Instantly share code, notes, and snippets.

function useDidChange(cb: (previous: any[]) => void, deps: any[]) {
const isMountedRef = React.useRef(false);
const cbRef = React.useRef(cb);
React.useEffect(() => {
cbRef.current = cb;
}, [cb]);
const previousRef = React.useRef(deps);
React.useEffect(() => {
if (isMountedRef.current) {
@akonwi
akonwi / echo.ts
Created April 22, 2019 15:58
minimal event emitter for single value type
interface Listener<T> {
(value: T): void;
}
class Echo<T> implements Disposable {
private listeners = new Set<Listener<T>>();
listen(listener: (value: T) => void) {
this.listeners.add(listener);
return new Disposable(() => {
atom.packages.onDidActivateInitialPackages (initialPackages) ->
atom.commands.add 'atom-text-editor', 'akonwi:rename-file', (e) ->
atom.commands.dispatch e.target, 'tree-view:rename'
if gitPlus = atom.packages.getActivePackage('git-plus')?.mainModule.provideService()
gitPlus.registerCommand 'atom-text-editor', 'akonwi:update-last-commit', ->
gitPlus.getRepo() # If there are multiple repos in the project, you will be prompted to select which to use
.then (repo) -> gitPlus.run repo, 'commit --all --amend --no-edit'
gitPlus.registerCommand 'atom-text-editor', 'akonwi:unstage-last-commit', ->
module.exports = (func) ->
xs = []
curry = (x) ->
if x
xs.push x
curry
else
xs = [0] if xs.length is 0
func.apply null, xs
git.add(repo).then -> GitCommit(repo, andPush: true)
gitAddAllCommitAndPush = (repo) ->
git.add repo,
exit: ->
new GitCommit(repo, andPush: true)
git.cmd = (args, options={}) ->
new Promise (resolve, reject) ->
output = ''
try
new BufferedProcess
command: atom.config.get('git-plus.gitPath') ? 'git'
args: args
options: options
stdout: (data) -> output += data.toString()
stderr: (data) -> reject data.toString()
gitStagedFiles = (repo, stdout) ->
files = []
gitCmd
args: ['diff-index', '--cached', 'HEAD', '--name-status', '-z']
cwd: repo.getWorkingDirectory()
stdout: (data) ->
files = _prettify(data)
stderr: (data) ->
# edge case of no HEAD at initial commit
if data.toString().includes "ambiguous argument 'HEAD'"
# Public: Execute a git command.
#
# options - An {Object} with the following keys:
# :args - The {Array} containing the arguments to pass.
# :cwd - Current working directory as {String}.
# :options - The {Object} with options to pass.
# :stdout - The {Function} to pass the stdout to.
# :exit - The {Function} to pass the exit code to.
#
# Returns nothing.
$('#export').on('click', e => {
e.preventDefault();
tableToExcel('table', 'Servers');
})
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function (s) { return window.btoa(window['unescape'](encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }