Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Oct 24, 2015
0 parents commit ec12061
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
@@ -0,0 +1,4 @@
{
"optional": ["runtime"],
"stage": 0
}
13 changes: 13 additions & 0 deletions .editorconfig
@@ -0,0 +1,13 @@
; editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.json]
indent_size = 2
10 changes: 10 additions & 0 deletions .eslintrc
@@ -0,0 +1,10 @@
{
"extends": "standard",
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true,
"mocha": true,
"es6": true
}
}
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
coverage
lib
node_modules

npm-debug.log
16 changes: 16 additions & 0 deletions .travis.yml
@@ -0,0 +1,16 @@
language: node_js
node_js:
- "0.10"
- "0.11"
- "0.12"
- "4"
before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
install:
- npm install
env:
- TEST_SUITE=coveralls
- TEST_SUITE=lint
- TEST_SUITE=test
script: "npm run $TEST_SUITE"
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Kirill Fomichev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 66 additions & 0 deletions README.md
@@ -0,0 +1,66 @@
# sync-mixin

[![build status](https://img.shields.io/travis/fanatid/sync-mixin.svg?branch=master&style=flat-square)](http://travis-ci.org/fanatid/sync-mixin)
[![Coverage Status](https://img.shields.io/coveralls/fanatid/sync-mixin.svg?style=flat-square)](https://coveralls.io/r/fanatid/sync-mixin)
[![Dependency status](https://img.shields.io/david/fanatid/sync-mixin.svg?style=flat-square)](https://david-dm.org/fanatid/sync-mixin#info=dependencies)
[![Dev Dependency status](https://img.shields.io/david/fanatid/sync-mixin.svg?style=flat-square)](https://david-dm.org/fanatid/sync-mixin#info=devDependencies)

[![NPM](https://nodei.co/npm/sync-mixin.png?downloads=true)](https://www.npmjs.com/package/sync-mixin)
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

## Installation

```
npm install sync-mixin
```

## API

### _getSyncCount()

**return**: `number`

### _setSyncCount()

* `number` value

### _syncEnter()

### _syncExit()

### _withSync()

* `function` fn

**return**: `Promise`

### _syncExitAll()

### isSyncing()

## Examples

###

```js
import { mixin } from 'core-decorators'
import SyncMixin from 'sync-mixin'

@mixin(SyncMixin)
class Process {
run () {
return this._withSync(() => {
// some code that require time for execution
})
}
}

let process = new Process()
process.on('syncStart', () => console.log('Syncronization started!'))
process.on('syncStop', () => console.log('Syncronization finished!'))
setInterval(::process.run, 60 * 1000)
```

## License

Code released under [the MIT license](LICENSE).
40 changes: 40 additions & 0 deletions karma.conf.js
@@ -0,0 +1,40 @@
module.exports = function (config) {
config.set({
frameworks: ['browserify', 'detectBrowsers', 'mocha'],
files: [
'node_modules/babel-core/browser-polyfill.js',
'test/*.js'
],
preprocessors: {
'test/*.js': ['browserify']
},
singleRun: true,
plugins: [
'karma-browserify',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-detect-browsers',
'karma-mocha'
],
browserify: {
debug: true,
transform: [
['babelify']
]
},
detectBrowsers: {
enabled: true,
usePhantomJS: false,
postDetection: function (availableBrowser) {
if (process.env.TRAVIS) {
return ['Firefox']
}

var browsers = ['Chrome', 'Firefox']
return browsers.filter(function (browser) {
return availableBrowser.indexOf(browser) !== -1
})
}
}
})
}
63 changes: 63 additions & 0 deletions package.json
@@ -0,0 +1,63 @@
{
"name": "sync-mixin",
"version": "1.0.0",
"description": "Emit events in classes about sync",
"keywords": [
"syncing",
"mixin"
],
"bugs": {
"url": "https://github.com/fanatid/sync-mixin/issues"
},
"license": "MIT",
"author": {
"name": "Kirill Fomichev",
"email": "fanatid@ya.ru"
},
"files": [
"lib",
"src",
"LICENSE",
"README.md"
],
"main": "./lib/index.js",
"repository": {
"type": "git",
"url": "https://github.com/fanatid/sync-mixin.git"
},
"scripts": {
"prepublish": "npm run clean && npm run compile",
"compile": "babel -d lib src",
"compile:watch": "babel -d lib src -w",
"coverage": "istanbul cover _mocha -- --compilers js:babel/register test/*.js",
"coveralls": "npm run coverage && coveralls <coverage/lcov.info",
"clean": "rm -f lib/*",
"lint": "eslint src test",
"test": "npm run test:node && npm run test:browser",
"test:browser": "karma start karma.conf.js",
"test:node": "istanbul test mocha -- --compilers js:babel/register --reporter spec test/*.js"
},
"dependencies": {
"babel-runtime": "^5.8.25"
},
"devDependencies": {
"babel": "^5.8.23",
"babel-core": "^5.8.25",
"babel-eslint": "^4.1.3",
"babelify": "^6.4.0",
"chai": "^3.4.0",
"core-decorators": "^0.8.1",
"coveralls": "^2.11.4",
"eslint": "^1.7.3",
"eslint-config-standard": "^4.4.0",
"eslint-plugin-standard": "^1.2.0",
"istanbul": "^0.4.0",
"karma": "^0.13.14",
"karma-browserify": "^4.4.0",
"karma-chrome-launcher": "^0.2.1",
"karma-detect-browsers": "^2.0.2",
"karma-firefox-launcher": "^0.1.6",
"karma-mocha": "^0.2.0",
"mocha": "^2.3.3"
}
}
46 changes: 46 additions & 0 deletions src/index.js
@@ -0,0 +1,46 @@
export default {
_getSyncCount () {
if (this._syncCount === undefined) {
this._syncCount = 0
}

return this._syncCount
},

_setSyncCount (value) {
this._syncCount = value

if (value === 0) {
return this.emit('syncStop')
}

if (value === 1) {
return this.emit('syncStart')
}
},

_syncEnter () {
this._setSyncCount(this._getSyncCount() + 1)
},

_syncExit () {
this._setSyncCount(this._getSyncCount() - 1)
},

async _withSync (fn) {
this._syncEnter()
try {
return await fn()
} finally {
this._syncExit()
}
},

_syncExitAll () {
this._setSyncCount(0)
},

isSyncing () {
return this._getSyncCount() > 0
}
}

0 comments on commit ec12061

Please sign in to comment.