Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported to CoffeeScript 2 #268

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Revision history

#### NEXT

* Package updated to CoffeeScript 2. Now is required to create an instance
of the class with `new JobCollection`, just calling `JobCollection` does
not work anymore.

#### 1.5.2

* Fixed an issue under some circumstances when `job.cancel()` was run on a job with option `{ dependents: false }`. Thanks @gbhrdt.
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ on such jobs.
// Server
if (Meteor.isServer) {

var myJobs = JobCollection('myJobQueue');
var myJobs = new JobCollection('myJobQueue');
myJobs.allow({
// Grant full permission to any authenticated user
admin: function (userId, method, params) {
Expand Down Expand Up @@ -94,7 +94,7 @@ Alright, the server is set-up and running, now let's add some client code to cre
// Client
if (Meteor.isClient) {

var myJobs = JobCollection('myJobQueue');
var myJobs = new JobCollection('myJobQueue');

Meteor.startup(function () {
Meteor.subscribe('allJobs');
Expand Down Expand Up @@ -348,8 +348,8 @@ methods, which may be secured using allow/deny rules specific to `JobCollection`
documentation for `jc.allow()` and `jc.deny()` for more information.

```javascript
// the "new" is optional
jc = JobCollection('defaultJobCollection');
// the "new" is required
jc = new JobCollection('defaultJobCollection');
```

### jc.setLogStream(writeStream) - *Server only*
Expand Down
6 changes: 4 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Package.describe({

Package.onUse(function(api) {
api.use('mrt:[email protected]', ['server','client']);
api.use('[email protected]_1', ['server','client']);
api.use('[email protected]_3', ['server','client']);
api.use('[email protected]', ['server','client']);
api.use('[email protected]', ['server','client']);
api.use('[email protected]', ['server','client']);
api.addFiles('job/src/job_class.coffee', ['server','client']);
Expand All @@ -29,7 +30,8 @@ Package.onUse(function(api) {
Package.onTest(function (api) {
api.use('vsivsi:job-collection@' + currentVersion, ['server','client']);
api.use('mrt:[email protected]', ['server','client']);
api.use('[email protected]_1', ['server','client']);
api.use('[email protected]_3', ['server','client']);
api.use('[email protected]', ['server','client']);
api.use('[email protected]', ['server','client']);
api.use('[email protected]', ['server','client']);
api.use('[email protected]', ['server','client']);
Expand Down
3 changes: 0 additions & 3 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ if Meteor.isClient
class JobCollection extends share.JobCollectionBase

constructor: (root = 'queue', options = {}) ->
unless @ instanceof JobCollection
return new JobCollection(root, options)

# Call super's constructor
super root, options

Expand Down
5 changes: 1 addition & 4 deletions src/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ if Meteor.isServer
class JobCollection extends share.JobCollectionBase

constructor: (root = 'queue', options = {}) ->
unless @ instanceof JobCollection
return new JobCollection(root, options)

# Call super's constructor
super root, options

Expand All @@ -43,7 +40,7 @@ if Meteor.isServer
@stopped = true

# No client mutators allowed
share.JobCollectionBase.__super__.deny.bind(@)
Meteor.Collection.prototype.deny.bind(@)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to hardcode this because I have no idea how to get to __super__ equivalent in new CoffeeScript/JavaScript.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update: () => true
insert: () => true
remove: () => true
Expand Down
45 changes: 27 additions & 18 deletions src/shared.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,22 @@ _validJobDoc = () ->
repeatWait: Match.OneOf(Match.Where(_validIntGTEZero), Match.Where(_validLaterJSObj))
created: Date

class JobCollectionBase extends Mongo.Collection

constructor: (@root = 'queue', options = {}) ->
unless @ instanceof JobCollectionBase
return new JobCollectionBase(@root, options)
_getAllProperties = (obj) ->
names = new Set()
properties = []
while obj
for name in Object.getOwnPropertyNames(obj) when name not in names
properties.push([name, obj[name]])
names.add(name)
obj = Object.getPrototypeOf(obj)
properties

unless @ instanceof Mongo.Collection
throw new Meteor.Error 'The global definition of Mongo.Collection has changed since the job-collection package was loaded. Please ensure that any packages that redefine Mongo.Collection are loaded before job-collection.'

unless Mongo.Collection is Mongo.Collection.prototype.constructor
throw new Meteor.Error 'The global definition of Mongo.Collection has been patched by another package, and the prototype constructor has been left in an inconsistent state. Please see this link for a workaround: https://github.com/vsivsi/meteor-file-sample-app/issues/2#issuecomment-120780592'

@later = later # later object, for convenience
class JobCollectionBase extends Mongo.Collection

constructor: (root = 'queue', options = {}) ->
options.noCollectionSuffix ?= false

collectionName = @root
collectionName = root

unless options.noCollectionSuffix
collectionName += '.jobs'
Expand All @@ -103,7 +102,20 @@ class JobCollectionBase extends Mongo.Collection
# calling Mongo.Collection constructor
delete options.noCollectionSuffix

Job.setDDP(options.connection, @root)
Job.setDDP(options.connection, root)

# Call super's constructor
super collectionName, options

unless @ instanceof Mongo.Collection
throw new Meteor.Error 'The global definition of Mongo.Collection has changed since the job-collection package was loaded. Please ensure that any packages that redefine Mongo.Collection are loaded before job-collection.'

unless Mongo.Collection is Mongo.Collection.prototype.constructor
throw new Meteor.Error 'The global definition of Mongo.Collection has been patched by another package, and the prototype constructor has been left in an inconsistent state. Please see this link for a workaround: https://github.com/vsivsi/meteor-file-sample-app/issues/2#issuecomment-120780592'

@root = root

@later = later # later object, for convenience

@_createLogEntry = (message = '', runId = null, level = 'info', time = new Date(), data = null) ->
l = { time: time, runId: runId, message: message, level: level }
Expand All @@ -128,9 +140,6 @@ class JobCollectionBase extends Mongo.Collection
level = if fatal then 'danger' else 'warning'
@_createLogEntry msg, runId, level).bind(@)

# Call super's constructor
super collectionName, options
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No access to @ is allowed before calling super in the constructor. This is a JavaScript limitation.


_validNumGTEZero: _validNumGTEZero
_validNumGTZero: _validNumGTZero
_validNumGTEOne: _validNumGTEOne
Expand Down Expand Up @@ -222,7 +231,7 @@ class JobCollectionBase extends Mongo.Collection
_generateMethods: () ->
methodsOut = {}
methodPrefix = '_DDPMethod_'
for methodName, methodFunc of @ when methodName[0...methodPrefix.length] is methodPrefix
for [methodName, methodFunc] in _getAllProperties(@) when methodName[0...methodPrefix.length] is methodPrefix
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baseMethodName = methodName[methodPrefix.length..]
methodsOut["#{@root}_#{baseMethodName}"] = @_methodWrapper(baseMethodName, methodFunc.bind(@))
return methodsOut
Expand Down