-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
203 lines (176 loc) · 5.54 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var app = require('app');
var ipc = require('ipc');
var BrowserWindow = require('browser-window');
// Node modules
var fs = require('fs');
var _ = require('underscore');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var DDPClient = require('ddp');
// App config file
var Config = require('./config.js');
// ===================================
// Bootstrap folders
// ===================================
// Determine directory names.
// Desktop/Todos, Desktop/Todos/Private
var desktopDir = app.getPath('userDesktop');
var appRootDir = desktopDir + '/Todos';
// Create directory
mkdirp(appRootDir);
// ===================================
// Bootstrap Electron
// ===================================
// Report crashes to our server.
require('crash-reporter').start();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform != 'darwin') {
app.quit();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the Meteor app
mainWindow.loadUrl(Config.appUrl);
// Emitted when the window is closed.
mainWindow.on('closed', function() {
mainWindow = null;
});
});
// ===================================
// DDP handling
// ===================================
// Create DDP Client
var ddpClient = new DDPClient(Config.ddpOptions);
// On Meteor login event
ipc.on('logged-in', function (event, loginToken) {
// Use token to login with Node DDP client
connectAndLogin(loginToken, function (error, userInfo) {
if (error) console.log(error);
else {
// Set up observer for Lists + Todos
setupListsObservers(ddpClient);
setupTodosObservers(ddpClient);
// Subscribe to Lists + Todos
subscribeToPrivateLists();
}
});
});
function connectAndLogin(loginToken, cb) {
// Connect DDP client
ddpClient.connect(function (error, wasReconnect) {
if (error) console.log(error);
// No error - call DDP login method with token
else ddpClient.call('login', [{resume: loginToken}], cb);
});
}
// ===================================
// DDP Observers
// ===================================
// Set up observer on DDP events for 'myFiles' collection and act on each event
function setupListsObservers(ddpClient) {
// Start new observer
ddpClient.listsObserver = ddpClient.observe('lists');
// Added
ddpClient.listsObserver.added = function (id) {
// Write initial list
writeList(id);
// Subscribe to todos for list
subscribeToTodos(id);
};
// Changed
ddpClient.listsObserver.changed = function (id, oldFields, clearedFields, newFields) {
// Update list name if changes
if (newFields.name) {
// Get old and new path for rename
var oldPath = getListPath(oldFields.name);
var newPath = getListPath(newFields.name);
fs.rename(oldPath, newPath, function (err) {
if (err) console.log(err);
});
}
};
// Removed
ddpClient.listsObserver.removed = function (id, toRemove) {
// Remove list
var listPath = getListPath(toRemove.name);
fs.unlink(listPath, function (err) {
if (err) console.log(err);
});
};
}
// Set up observer on DDP events for 'myFiles' collection and act on each event
function setupTodosObservers(ddpClient) {
// Start new observer
ddpClient.todosObserver = ddpClient.observe('todos');
// Added
ddpClient.todosObserver.added = function (id) {
// Get todo doc
var todoDoc = ddpClient.collections.todos[id];
// Write list
writeList(todoDoc.listId);
};
// Changed
ddpClient.todosObserver.changed = function (id, oldFields, clearedFields, newFields) {
// Rewrite list
var todoDoc = ddpClient.collections.todos[id];
writeList(todoDoc.listId);
};
// Removed
ddpClient.todosObserver.removed = function (id, toRemove) {
// Rewrite list
writeList(toRemove.listId);
};
}
// ===================================
// DDP Subscriptions
// ===================================
function subscribeToPrivateLists() {
// Subscribe to private lists
ddpClient.subscribe('privateLists', [], function () {
console.log('private lists subscription complete.');
});
}
function subscribeToTodos(listId) {
// Subscribe to todos for a list
ddpClient.subscribe('todos', [listId], function () {
console.log('todo subscription to list ' + listId + ' complete.');
});
}
// ===================================
// File writing
// ===================================
function writeList(listId) {
// Get doc
var listDoc = ddpClient.collections.lists[listId];
// Get all todos for doc
var todos = ddpClient.collections.todos || [];
var todoText = '';
_.each(todos, function (td) {
if (td.listId === listId) {
todoText += td.text + '\n';
}
});
// Create file
fs.writeFile(getListPath(listDoc.name), todoText, function(err) {
if(err) return console.log(err);
});
}
// ===================================
// Helpers
// ===================================
function getListPath(listName) {
return appRootDir + '/' + listName + '.txt';
}
ipc.on('getLocalPath', function (event, args) {
event.returnValue = getListPath(args.listName);
});