Skip to content

Commit

Permalink
Add property to check if instance has been destroyed.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmacarthur committed Apr 11, 2018
1 parent 7655aab commit 29d5bc3
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 38 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ if(instance.isComplete) {
}
```

### Check If Instance Has Been Destroyed
You can also, at any time, check if the instance has been destroyed using the `destroy()` method.

```js
var instance = new TypeIt('#element', { /* options... */ });

if(instance.hasBeenDestroyed) {
//-- Do something.
}
```

## Options
You can modify the options for the plugin by passing in JSON upon instantiation. It'll look something like this:

Expand Down
10 changes: 7 additions & 3 deletions dist/typeit.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* typeit - The most versatile animated typing utility on the planet.
* Author: Alex MacArthur <[email protected]> (https://macarthur.me)
* Version: v5.6.1
* Version: v5.7.0
* URL: https://typeitjs.com
* License: GPL-2.0
*
Expand Down Expand Up @@ -343,13 +343,15 @@ var Instance = function () {

var that = this;

window.addEventListener("scroll", function checkForStart(event) {
function checkForStart(event) {
if (isVisible(that.element) && !that.hasStarted) {
that.hasStarted = true;
that.next();
event.currentTarget.removeEventListener(event.type, checkForStart);
}
});
}

window.addEventListener("scroll", checkForStart);
}
}, {
key: "cursor",
Expand Down Expand Up @@ -682,6 +684,7 @@ var TypeIt = function () {
this.instances = [];
this.elements = [];
this.args = args;
this.hasBeenDestroyed = false;

if ((typeof element === "undefined" ? "undefined" : _typeof(element)) === "object") {
//-- There's only one!
Expand Down Expand Up @@ -809,6 +812,7 @@ var TypeIt = function () {
}
});

this.hasBeenDestroyed = true;
this.instances = [];
}
}, {
Expand Down
10 changes: 7 additions & 3 deletions dist/typeit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* typeit - The most versatile animated typing utility on the planet.
* Author: Alex MacArthur <[email protected]> (https://macarthur.me)
* Version: v5.6.1
* Version: v5.7.0
* URL: https://typeitjs.com
* License: GPL-2.0
*
Expand Down Expand Up @@ -349,13 +349,15 @@

var that = this;

window.addEventListener("scroll", function checkForStart(event) {
function checkForStart(event) {
if (isVisible(that.element) && !that.hasStarted) {
that.hasStarted = true;
that.next();
event.currentTarget.removeEventListener(event.type, checkForStart);
}
});
}

window.addEventListener("scroll", checkForStart);
}
}, {
key: "cursor",
Expand Down Expand Up @@ -688,6 +690,7 @@
this.instances = [];
this.elements = [];
this.args = args;
this.hasBeenDestroyed = false;

if ((typeof element === "undefined" ? "undefined" : _typeof(element)) === "object") {
//-- There's only one!
Expand Down Expand Up @@ -815,6 +818,7 @@
}
});

this.hasBeenDestroyed = true;
this.instances = [];
}
}, {
Expand Down
4 changes: 2 additions & 2 deletions dist/typeit.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typeit",
"version": "5.6.1",
"version": "5.7.0",
"description": "The most versatile animated typing utility on the planet.",
"author": "Alex MacArthur <[email protected]> (https://macarthur.me)",
"license": "GPL-2.0",
Expand Down
6 changes: 4 additions & 2 deletions src/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,15 @@ export default class Instance {

let that = this;

window.addEventListener("scroll", function checkForStart(event) {
function checkForStart(event) {
if (isVisible(that.element) && !that.hasStarted) {
that.hasStarted = true;
that.next();
event.currentTarget.removeEventListener(event.type, checkForStart);
}
});
}

window.addEventListener("scroll", checkForStart);
}

cursor() {
Expand Down
2 changes: 2 additions & 0 deletions src/typeit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class TypeIt {
this.instances = [];
this.elements = [];
this.args = args;
this.hasBeenDestroyed = false;

if (typeof element === "object") {
//-- There's only one!
Expand Down Expand Up @@ -122,6 +123,7 @@ export default class TypeIt {
}
});

this.hasBeenDestroyed = true;
this.instances = [];
}

Expand Down
5 changes: 4 additions & 1 deletion tests/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test("Returns an object with base properties.", () => {
const instance = new TypeIt("#element", {});

expect(Object.keys(instance).sort()).toEqual(
["elements", "id", "instances", "args"].sort()
["elements", "id", "instances", "args", "hasBeenDestroyed"].sort()
);
});

Expand All @@ -59,9 +59,12 @@ test("Destroys instances successfully.", () => {

jest.runAllTimers();

expect(instance.hasBeenDestroyed).toBe(false);

instance.destroy();

expect(instance.instances).toHaveLength(0);
expect(instance.hasBeenDestroyed).toBe(true);
expect(document.body.querySelector(".ti-cursor")).toEqual(null);
});

Expand Down
51 changes: 25 additions & 26 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ aws-sign2@~0.7.0:
resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.7.0.tgz#b46e890934a9591f2d2f6f86d7e6a9f1b3fe76a8"

aws4@^1.2.1, aws4@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e"
version "1.7.0"
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.7.0.tgz#d4d0e9b9dbfca77bf08eeb0a8a471550fe39e289"

babel-code-frame@^6.26.0:
version "6.26.0"
Expand Down Expand Up @@ -426,12 +426,13 @@ babel-plugin-external-helpers@^6.22.0:
babel-runtime "^6.22.0"

babel-plugin-istanbul@^4.0.0, babel-plugin-istanbul@^4.1.5:
version "4.1.5"
resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.5.tgz#6760cdd977f411d3e175bb064f2bc327d99b2b6e"
version "4.1.6"
resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz#36c59b2192efce81c5b378321b74175add1c9a45"
dependencies:
babel-plugin-syntax-object-rest-spread "^6.13.0"
find-up "^2.1.0"
istanbul-lib-instrument "^1.7.5"
test-exclude "^4.1.1"
istanbul-lib-instrument "^1.10.1"
test-exclude "^4.2.1"

babel-plugin-jest-hoist@^21.2.0:
version "21.2.0"
Expand Down Expand Up @@ -836,16 +837,14 @@ braces@^1.8.2:
repeat-element "^1.1.2"

braces@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/braces/-/braces-2.3.1.tgz#7086c913b4e5a08dbe37ac0ee6a2500c4ba691bb"
version "2.3.2"
resolved "https://registry.yarnpkg.com/braces/-/braces-2.3.2.tgz#5979fd3f14cd531565e5fa2df1abfff1dfaee729"
dependencies:
arr-flatten "^1.1.0"
array-unique "^0.3.2"
define-property "^1.0.0"
extend-shallow "^2.0.1"
fill-range "^4.0.0"
isobject "^3.0.1"
kind-of "^6.0.2"
repeat-element "^1.1.2"
snapdragon "^0.8.1"
snapdragon-node "^2.0.1"
Expand Down Expand Up @@ -910,8 +909,8 @@ camelcase@^4.1.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd"

caniuse-lite@^1.0.30000792:
version "1.0.30000821"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000821.tgz#0f3223f1e048ed96451c56ca6cf197058c42cb93"
version "1.0.30000827"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000827.tgz#2dad2354e4810c3c9bb1cfc57f655c270c25fa52"

caseless@~0.12.0:
version "0.12.0"
Expand Down Expand Up @@ -1086,8 +1085,8 @@ copy-descriptor@^0.1.0:
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"

core-js@^2.4.0, core-js@^2.5.0:
version "2.5.4"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.4.tgz#f2c8bf181f2a80b92f360121429ce63a2f0aeae0"
version "2.5.5"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.5.tgz#b14dde936c640c0579a6b50cabcc132dd6127e3b"

[email protected], core-util-is@~1.0.0:
version "1.0.2"
Expand Down Expand Up @@ -1262,8 +1261,8 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"

electron-to-chromium@^1.3.30:
version "1.3.41"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.41.tgz#7e33643e00cd85edfd17e04194f6d00e73737235"
version "1.3.42"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.42.tgz#95c33bf01d0cc405556aec899fe61fd4d76ea0f9"

encodeurl@~1.0.2:
version "1.0.2"
Expand Down Expand Up @@ -2162,7 +2161,7 @@ istanbul-lib-hook@^1.2.0:
dependencies:
append-transform "^0.4.0"

istanbul-lib-instrument@^1.10.1, istanbul-lib-instrument@^1.4.2, istanbul-lib-instrument@^1.7.5, istanbul-lib-instrument@^1.8.0:
istanbul-lib-instrument@^1.10.1, istanbul-lib-instrument@^1.4.2, istanbul-lib-instrument@^1.8.0:
version "1.10.1"
resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-1.10.1.tgz#724b4b6caceba8692d3f1f9d0727e279c401af7b"
dependencies:
Expand Down Expand Up @@ -2831,8 +2830,8 @@ lcid@^1.0.0:
invert-kv "^1.0.0"

left-pad@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.2.0.tgz#d30a73c6b8201d8f7d8e7956ba9616087a68e0ee"
version "1.3.0"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"

leven@^2.1.0:
version "2.1.0"
Expand Down Expand Up @@ -3512,15 +3511,15 @@ read-pkg@^2.0.0:
path-type "^2.0.0"

readable-stream@^2.0.1, readable-stream@^2.0.6, readable-stream@^2.1.4:
version "2.3.5"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.5.tgz#b4f85003a938cbb6ecbce2a124fb1012bd1a838d"
version "2.3.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.3"
isarray "~1.0.0"
process-nextick-args "~2.0.0"
safe-buffer "~5.1.1"
string_decoder "~1.0.3"
string_decoder "~1.1.1"
util-deprecate "~1.0.1"

realpath-native@^1.0.0:
Expand Down Expand Up @@ -4043,9 +4042,9 @@ string-width@^2.0.0, string-width@^2.1.1:
is-fullwidth-code-point "^2.0.0"
strip-ansi "^4.0.0"

string_decoder@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab"
string_decoder@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8"
dependencies:
safe-buffer "~5.1.0"

Expand Down Expand Up @@ -4134,7 +4133,7 @@ tar@^2.2.1:
fstream "^1.0.2"
inherits "2"

test-exclude@^4.1.1:
test-exclude@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-4.2.1.tgz#dfa222f03480bca69207ca728b37d74b45f724fa"
dependencies:
Expand Down

0 comments on commit 29d5bc3

Please sign in to comment.