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

2.0.0 #6

Merged
merged 5 commits into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
document-promises.js
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes to document-promises

### 2.0.0 (October 25, 2016)

- Re-implemented as a ponyfill

### 1.0.0 (October 20, 2016)

- Initial version
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Changelog][log-image]][log-url]
[![Gitter Chat][git-image]][git-url]

[Document Promises] is a polyfill for [document.interactive], [document.contentLoaded], and [document.loaded] which allow you to run code after specific states of the document.
[Document Promises] is a ponyfill for [document.interactive], [document.contentLoaded], and [document.loaded] which allow you to run code after specific states of the document.

```js
fetch('data.json').then(function (data) {
Expand All @@ -28,6 +28,34 @@ fetch('data.json').then(function (data) {

[document.loaded] is a promise that fulfills when the when the document's `readyState` becomes `complete`.

## Usage

[Document Promises] does not attach properties to the document by default, because no browsers have yet implemented this behavior.

Instead, developers may import the features individually.

```js
// Example ES6 import
import contentLoaded from 'document-promises';

// Example CommonJS import
require('document-promises').contentLoaded;
```

Developers may use the ponyfill as-is immediately.

```js
contentLoaded.then(function () {
/* document is ready */
});
```

Developers are strongly advised not to assign these promises to `document`, as the standard may still change substantially, and then such code would be future-incompatible.

```js
document.contentLoaded = contentLoaded;
```

## FAQ

### What’s the difference between these promises and DOMContentLoaded?
Expand All @@ -40,7 +68,7 @@ Using promises for state transitions is much more [developer friendly].

### What’s the catch?

[Document Promises] is [public domain], dependency free, and 195 bytes when minified and gzipped.
[Document Promises] is [public domain], dependency free, and 252 bytes or less when minified and gzipped.

### Any known limitations?

Expand Down
19 changes: 19 additions & 0 deletions document-promises.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let promisify = (type, readyState) => {
return new Promise((resolve) => {
let listener = () => {
if (readyState.test(document.readyState)) {
document.removeEventListener(type, listener);

resolve();
}
};

document.addEventListener(type, listener);

listener();
});
};

export let interactive = promisify('readystatechange', /^(?:interactive|complete)$/);
export let contentLoaded = promisify('DOMContentLoaded', /^(?:interactive|complete)$/);
export let loaded = promisify('readystatechange', /^complete$/);
30 changes: 0 additions & 30 deletions document-promises.js

This file was deleted.

36 changes: 24 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
{
"name": "document-promises",
"version": "1.0.0",
"version": "2.0.0",
"description": "Document loading states as Promises",
"author": "Jonathan Neal <[email protected]> (http://jonathantneal.com)",
"license": "CC0-1.0",
"repository": "jonathantneal/document-promises",
"homepage": "https://github.com/jonathantneal/document-promises#readme",
"bugs": "https://github.com/jonathantneal/document-promises/issues",
"main": "document-promises.js",
"jsnext:main": "document-promises.es6",
"files": [
"document-promises.es6",
"document-promises.js",
"LICENSE.md"
],
"scripts": {
"test": "eslint document-promises.js && jscs document-promises.js"
"prepublish": "babel document-promises.es6 --out-file document-promises.js",
"test": "eslint document-promises.es6 && jscs document-promises.es6"
},
"engines": {
"node": ">=4.0.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-preset-es2015": "^6.18.0",
"eslint": "^3.8.1",
"eslint-config-10up": "github:jonathantneal/eslint-config-10up",
"jscs": "^3.0.7"
"jscs": "^3.0.7",
"jscs-config-10up": "github:jonathantneal/jscs-config-10up"
},
"babel": {
"presets": [
[
"es2015", {
"loose": true,
"modules": "commonjs"
}
]
]
},
"eslintConfig": {
"extends": "10up",
"rules": {
"consistent-this": [
2,
"document"
]
"parserOptions": {
"ecmaVersion": 6,
"impliedStrict": true,
"sourceType": "module"
}
},
"jscsConfig": {
Expand All @@ -44,8 +59,5 @@
"interactive",
"state",
"promise"
],
"dependencies": {
"jscs-config-10up": "github:jonathantneal/jscs-config-10up"
}
]
}