Skip to content

Commit

Permalink
fix: extra check on counter creation & windows compability (thanks @s…
Browse files Browse the repository at this point in the history
…p3c1)

Signed-off-by: Bartlomiej Specjalny <bspecjalny@gmail.com>
  • Loading branch information
sp3c1 authored and nodkz committed Feb 20, 2018
1 parent d2f4809 commit f4807c9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build-flow.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done
2 changes: 2 additions & 0 deletions build-ts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
find ./src -name '*.d.ts' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`; done
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@
]
},
"scripts": {
"build": "npm run build-cjs && npm run build-flow && npm run build-ts",
"build": "npm run build-cjs && npm run build-flow-sh && npm run build-ts-sh",
"build-cjs": "rimraf lib && babel src --ignore __tests__,__mocks__ -d lib",
"build-flow": "find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done",
"build-flow-sh": "sh build-flow.sh",
"build-ts": "find ./src -name '*.d.ts' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`; done",
"build-ts-sh": "sh build-ts.sh",
"watch": "jest --watch",
"coverage": "jest --coverage --maxWorkers 2",
"lint": "npm run eslint && npm run tslint && npm run tscheck",
Expand Down
19 changes: 17 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,22 @@ async function createCounterIfNotExist(
count: settings.startAt - settings.incrementBy,
}): any);

await existedCounter.save();
try {
// this might fail if invoked in parallel
await existedCounter.save();
} catch (e) {
if (isMongoDuplicateError(e)) {
// just to be consistent with the method return type
// but to tell the truth it could return boolean at this point
existedCounter = (await IC.findOne({
model: settings.model,
field: settings.field,
groupingField,
}).exec(): any);
} else {
throw e; // other unhandled errors
}
}
}

return (existedCounter: any);
Expand Down Expand Up @@ -177,7 +192,7 @@ async function preSave(
doc.set(settings.field, count);

// $FlowFixMe
doc.__maiRanOnce = true; // eslint-disable-line
doc.__maiRanOnce = true; // eslint-disable-line
}

next();
Expand Down

0 comments on commit f4807c9

Please sign in to comment.